Boost XmlSerializer Performance

I use XmlSerializer to serialize / deserialize some objects. Performance issue. When profiling with XmlSerializer make our application 2 seconds longer. We cache our XmlSerializer and reuse them. We cannot use sgen.exe because we are creating an XmlSerializer with XmlAttributeOverrides .

I am trying to use an alternative to serialization like Json.Net, and firstly, it works great. The problem is that we need to be backward compatible, so all XML files already created must be parsed correctly. In addition, the serialization output of the object must be Xml.

Summarizing:

  • I get XML data serialized by XmlSerializer.
  • I need to deserialize the Xml data and convert it to an object.
  • I need to serialize an object in Xml (ideally an Xml format like XmlSerializer)
+6
source share
6 answers

Ultimately, it depends on the complexity of your model. XmlSerializer needs a lot of reflection, and the fact that it takes so long to make me suspect that your model is quite complex. For a simple model, you could manually implement deserialization using LINQ-to-XML (pretty easy) or perhaps even XmlReader (if you feel very brave, it's not easy to get 100% correct).

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

Another option is the DataContractSerializer , which processes xml, but not the same as the XmlSerializer , and, of course, does not have the same control over the layout. I strongly suspect that the DataContractSerializer will not help you.

There is no direct replacement for the XmlSerializer that I know of, and if sgen.exe is not an option, I believe that you have options:

  • live with him
  • rewrite XmlSerializer yourself, somehow better than they
  • use something like LINQ-to-XML and take the effort you make.

Long-term, I would say "conversion formats" and use xml only for obsolete imports. I know some very fast binary protocols that could be easily replaced: p

+9
source

The problem is that you are requesting types that are not covered by sgen, which leads to the generation of new assemblies at startup.

You can try to get the temporary file files created by Xmlserializer for your specific types, and use this code for your own xmlserializer pre-build. I used this approach to find out why csc.exe was executed , which delayed the launch of my application.

In addition, it can help to rename some types, for example, in an article, to get the same type names as sgen, to use sgen. Typically, array types are not handled by sgen, which is sometimes useless. But if you name your class ArrayOf HereGoesYourTypeName, then you can use the pre-built assemblies.

+2
source

you need to deserialize your list using classic .net serialization

something like below:

 TextReader tr = new StreamReader("yourlist.xml"); XmlSerializer serializer = new XmlSerializer(typeof(List<YourObject>)); List<YourObject> myCutomList = (List<YourObject>)serializer.Deserialize(tr); tr.Close(); 

then you can use Json.Serialization

 JavaScriptSerializer json = new JavaScriptSerializer(); JsonResult output = json.Serialize(myCutomList ); 
+1
source

If you have xml stored in a format that you cannot use, use xslt to convert it to a format that you can use.

If this xml is stored in the XmlSerializer format β€” say, in flat files β€” or in db β€” then you can run your conversions on it once and not cause the XmlSerializer overhead at normal runtime.

Alternatively, you can execute xslt at runtime, but I doubt it will be faster than the method described by Massimiliano.

0
source

This answer contains some useful information on why the XmlSerializer is slow using XmlAttributeOverrides.

Do you really need to use the XmlSerializer in your main thread at startup?

Perhaps run it in the background thread; If only some pieces of data are required to run, perhaps you could manually read them in proxy / sparse versions of real classes, while XmlSerializer initializes.

If this is a GUI application, you can simply add a screensaver to hide the delay (or a tetris game !!)

If all else fails, you cannot convert existing files to JSON by simply running the existing JSON deserializer and serialization, or is there a strict requirement to save their XML?

0
source

You can use threads or tasks to run the application faster and not wait for a hard drive or deserialization.

0
source

Source: https://habr.com/ru/post/909642/


All Articles