XmlSerializer and web page load times
I haven't seen a post about this, so I thought I'd leave a signpost for those doing XML Serializing / Deserializing to objects.
In a nutshell, we have a web application currently under development that was seeing extremely slow load times.Some pages were taking between 3 and 10 seconds to render and be shipped to the browser.Our design calls for XML data (about 300KB) to be pulled from a SQL 2005 server, deserialized into objects, and populate web controls with the data.
On a post back each page validates the input, stores it back into the various objects, serializes the objects, and ultimately updates the data in SQL server.Most pages also passed the data to various Web Services for additional processing prior to updating SQL server.
On paper this sounded great, increased our maintainability, and increased our integration prospects with client systems.The reality was that it was too slow to be usable regardless of the other benefits.
Realizing this would lead to a failed project, I started logging every method call and the time it took to process.Eventually I narrowed it down to the sections of code where we were instantiating the XMLSerializer.No matter what object or piece of the XML data our code worked with it took roughly 1.5 seconds to create the XMLSerializer. Given that this could happen for different objects a couple times within the page life cycle it was easy to see how simple pages would kill our web server.
The seemingly innocuous line of code was very simple:
XmlSerializer serLoan = new XmlSerializer(typeof(MRG.Loan), null, new Type[0], new XmlRootAttribute("LOAN"), http://yaddayadda/loan);
I searched the internet trying to come up with a "fix" for this. We tried various Readers and we tried using different parameter sets for the XMLSerializer, nothing made a difference.Ultimately I found that the XmlSerializer object creates a temporary assembly for every type it uses.This temp assembly is stored in a temp directory on the server and is recreated every time the XmlSerializer is instantiated. The actual speed of this process is dependent on your processing power, hard drive speeds, and current load on the server.
The real fix should be for Visual Studio to precompile those assemblies and have .net intelligently reuse that code instead of doing a just in time compile for it.In light of MS changing this behavior the easy solution was to move all of the XmlSerializer instantiations to the global.asax.cs file as static properties and simply have the pages call the already created object. My developer senses do not like this solution as it adds properties to a global file which are really local to specific pages by nature, however, there doesn't seem to be another way. We did try several different mechanisms for preloading the XMLSerializer; the only one that worked was making each serializer a public static property.
This simple change reduced our page load and postback times from 10 seconds down to less than 300 milliseconds, and in some cases as low as 40 milliseconds. The downside is that the application start now takes around 15 seconds or so; however, this only happens when the application pool is reset so that is acceptable. As you can tell, this simple change took what was a behemoth and turned it into a highly responsive system.
I hope this helps someone,
Enjoy.
Chris Lively

