Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improve performance of XmlSerializer

I use a XmlSerializer to serialize/deserialize some objects. The problem is the performance. When profiling, using the XmlSerializer make our application 2 seconds longer to start. We cache our XmlSerializer and reuse them. We cannot use sgen.exe because we are creating the XmlSerializer with XmlAttributeOverrides.

I try to use serialization alternative like Json.Net and, at first, it's work great. The problem is that we need to be backward compatible so all the xml already generated need to be parsed correctly. Also, the object serialization output must be Xml.

To summarize:

  1. I receive Xml data serialized by a XmlSerializer.
  2. I need to deserialize the Xml data and convert it into an object.
  3. I need to serialize object into Xml (ideally an Xml format like the one a XmlSerializer would have done)
like image 403
Melursus Avatar asked Feb 29 '12 15:02

Melursus


2 Answers

Ultimately, it depends on the complexity of your model. XmlSerializer needs to do a lot of thinking, and the fact that it is taking so long leads me to suspect that your model is pretty complex. For a simple model, it might be possible to manually implement the deserialization using LINQ-to-XML (pretty easy), or maybe even XmlReader (if you are feeling very brave - it isn't easy to get 100% correct).

However, if the model is complex, this is a problem and frankly would be very risky in terms of introducing subtle bugs.

Another option is DataContractSerializer which handles xml, but not as well as XmlSerializer, and certainly not with as much control over the layout. I strongly suspect that DataContractSerializer would not help you.

There is no direct replacement for XmlSerializer that I am aware of, and if sgen.exe isn't an option I believe you basically have options:

  • live with it
  • rewrite XmlSerializer yourself, somehow doing better than them
  • use something like LINQ-to-XML and accept the effort involved

Long term, I'd say "switch formats", and use xml for legacy import only. I happen to know of some very fast binary protocols that would be pretty easy to substitute in ;p

like image 92
Marc Gravell Avatar answered Nov 17 '22 03:11

Marc Gravell


The problem is that you are requesting types which are not covered by sgen which results in the generation of new assemblies during startup.

You could try to get your hands on the temp files generated by Xmlserializer for your specific types and use this code for your own pregnerated xmlserializer assembly. I have used this approach to find out why csc.exe was executed which did delay startup of my application.

Besides this it might help to rename some types like in the article to arrive at the same type names that sgen is creating to be able to use sgen. Usually type arrays are not precreated by sgen which is a pitty sometimes. But if you name your class ArrayOf HereGoesYourTypeName then you will be able to use the pregenerated assemblies.

like image 2
Alois Kraus Avatar answered Nov 17 '22 03:11

Alois Kraus