I experience a really strong loss in HUGE performance when calling a simple XmlSerializer.Deserizlize () in a class with many fields.
NOTE I am writing code without Visual Studio at home, so it may have some errors.
My serializable class is flat and contains hundreds of fields:
[Serializable] class Foo { public Foo() { } [XmlElement(ElementName = "Field1")] public string Field1;
My application deserializes the input string (even a small one):
StringReader sr = new StringReader(@"<Foo><Field1>foo</Field1></Foo>"); XmlSerializer serializer = new XmlSerializer(typeof(Foo)); object o = serializer.Deserialize(sr);
Running the application on 32-bit systems (or with a 32-bit forced using corflags.exe), the code takes about ONE SECOND for the first time (generating the temp serialization class and that's it ...), then it is close to 0.
Launching the application on 64-bit systems, the code takes ONE MINUTE for the first time, then it is close to 0.
What can the system depend on for such a long time, during the first execution of the XmlSerializer, for a large class on a 64-bit system?
Right now I'm not sure if I should blame creating / deleting temp classes, initializing the xml, CAS, Windows Search, AntiVirus or Santa Claus name table ...
SPOILERS
Here are my tests, do not read this if you do not want me to be distracted by my (possible) analysis errors.
- Running code from the Visual Studio debugger makes the code run FAST even on 64-bit systems.
- Adding the (fully undocumented) system.diagnostic switch "XmlSerialization.Compile", which prevents the system from deleting temporary serialization classes, forces the code to run FAST even on 64-bit systems.
- Taking the temporary class FooXmlSerializer created by the runtime, including .cs in my project and using it instead of the XmlSerializer, makes the code run FAST even on 64-bit systems.
- Creating the same FooXmlSerializer class with sgen.exe, including .cs in my project and using it instead of XmlSerializer, makes the code run FAST even on 64-bit systems.
- Creating the same FooXmlSerializer class with sgen.exe that references the Foo.XmlSerializers.dll assembly in my project and using it instead of XmlSerializer makes the code run SLOW even on 64-bit systems ( this scares me a lot )
- Performance loss occurs only if the input for deserialization actually contains a large class field ( this also bothers me a lot )
For further explanation of the last point, if I have a class:
[Serializable] class Bar { public Bar() { } [XmlElement(ElementName = "Foo")] public Foo Foo;
Deserialization is slow only when passing the child element to Foo. Even if I have already done deserialization:
StringReader sr = new StringReader(@"<Bar></Bar>"); XmlSerializer serializer = new XmlSerializer(typeof(Bar)); object o = serializer.Deserialize(sr); // FAST StringReader sr = new StringReader(@"<Bar><Foo><Field1>foo</Field1></Foo></Bar>"); XmlSerializer serializer = new XmlSerializer(typeof(Bar)); object o = serializer.Deserialize(sr); // SLOW
EDIT I forgot to say that I analyzed the execution using Process Monitor, and I don’t see any task taking a lot of time from my application or from csc.exe or something related to the Framework. The system just does other things (or I I'm missing something), for example, antivirus, explorer.exe, Windows Search indexing (already tried to disable them)