I tried Mono – it creates serializers in 1 millisecond vs 60 by .NET 4.0. May be somebody ported Mono serializers generator as reusable lib? Or can give me exact list of Mono assemblies to use if I will try to port?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace serialization
{
[Serializable]
public sealed class UserCredentials1
{
public string Username { get; set; }
public string Password { get; set; }
public override string ToString()
{
return string.Format("Username: {0}, Password: {1}", Username, Password);
}
}
[Serializable]
public sealed class UserCredentials2
{
public string Username { get; set; }
public string Password { get; set; }
public override string ToString()
{
return string.Format("Username: {0}, Password: {1}", Username, Password);
}
}
//.NET 4.0
//native=60.757
//compiled=2.2602
//Username: CTTTOM, Password: WoEIPX6Qqf11j9vKn01bAA==
//MONO:
//mono serialization.exe
//native=0.1589
//compiled=0.1337
//Username: CTTTOM, Password: WoEIPX6Qqf11j9vKn01bAA==
class Program
{
static void Main(string[] args)
{
string xml1 = @" " +
@" CTTTOM" +
@" WoEIPX6Qqf11j9vKn01bAA==" +
@"";
string xml2 = @"" +
@" CTTTOM" +
@" WoEIPX6Qqf11j9vKn01bAA==" +
@"";
//warm up
Type targetType1 = typeof(UserCredentials1);
XmlSerializer nativeSerializer1 = new XmlSerializer(targetType1);
Type targetType2 = typeof(UserCredentials2);
nativeSerializer1.Deserialize(new XmlTextReader(new StringReader(xml1)));
var native = new Stopwatch();
native.Start();
XmlSerializer nativeSerializer2 = new XmlSerializer(targetType2);
native.Stop();
Console.WriteLine("native=" + native.Elapsed.TotalMilliseconds);
var compiled = new Stopwatch();
compiled.Start();
var de = nativeSerializer2.Deserialize(new XmlTextReader(new StringReader(xml2)));
compiled.Stop();
Console.WriteLine("compiled=" + compiled.Elapsed.TotalMilliseconds);
Console.Write(de.ToString());
Console.ReadKey();
}
}
}
EDIT
I made first step to migration, see https://github.com/asd-and-Rizzo/mono .
Done test with generic list of objects using “mono serialization.exe”, mouse click run with .NET serialization and ported Mono serialization. Ported version gives ~10 times faster default serializers generation then .NET one.
EDIT
Found in MSDN regarding XML serialization configuration .NET 4.5 (http://msdn.microsoft.com/en-us/library/ms229754.aspx):
useLegacySerializationGeneration
Specifies whether the XmlSerializer uses legacy serialization generation which generates assemblies by writing C# code to a file and then compiling it to an assembly. The default is false.
Made port which can be build and used against .NET 3.5.
https://github.com/asd-and-Rizzo/mono/blob/master/mcs/class/System.XML/Mono.Xml.Serialization.sln
Mono does not compiles serializers.
Could be interesting to try managed Mono.CSharp.dll to compile serializers.
EDIT
I added several tests could go to Mono trunk. All my changes related only to port are under EXTEND_EMBRACE_XMLSER compilation symbol.
Our product tests noted that Mono serializes and deserializes what .NET does not. It is even better for our case, bat can be problem for others if error expected. All tests are commited to System.Xml tests.
EDIT2
When to serialize one object of type A and try do deserialize other object of type B from that string then: