Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
XmlSerializer slow to construct
Message
From
29/04/2011 14:18:58
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01508806
Message ID:
01508855
Views:
28
>>Hi all,
>>
>>I am tracing some code that is performing slowly and found this method to be very slow in performance compared to the rest of the code in the cycle. It appears from the trace that constructing an XmlSerializer is not very quick amongst other issues Microsoft has noted with Memory (see link below).
>>
>>I am wondering about other alternatives or obvious choices you might see that could be done to speed this up. I am looking into if it is possible to keep the XmlSerializer around instead of constructing it several times but not sure if that will be a reasonable choice yet.
>>
>>I also attached a screen shot of the dot trace showing the performance metrics to this code.
>>Thanks
>>
>>Microsoft Link
>>http://support.microsoft.com/kb/886385
>>
>>
>>
>>public XmlSerializer GetXmlSerializer(Type type)
>>{
>>	XmlSerializer serializer;
>>	lock (SerializerCache)
>>	{
>>		if (SerializerCache.ContainsKey(type))
>>		{
>>			serializer = SerializerCache[type];
>>		}
>>		else
>>		{
>>			// Next line of code is VERY slow comparitavily and it isn't the ToArray() call.
>>			serializer = new XmlSerializer(type, SerializationTypesList.ToArray());
>>			serializer.UnknownNode += this.XmlSerializer_UnknownNode;
>>			serializer.UnreferencedObject += this.XMLSerializer_UnreferencedObject;
>>			serializer.UnknownElement += this.XMLSerializer_UnknownElement;
>>			serializer.UnknownAttribute += this.XMLSerializer_UnknownAttribute;
>>			SerializerCache.Add(type, serializer);
>>		}
>>	}
>>	return serializer;
>>}
>>
>
>XMlSerializer constructors build dynamic assemblies so the process will be slow. The particular constructor used above is especially inefficient and you should probably cache it. See the 'Dynamically generated assemblies' section here for an example using a hashtable:
>http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx


Hi Viv,

The serializer is added to the Cache and checked, but there are 37 different calls to this method all with different types. On startup there are 13 calls, one right after another and all with different types. I can't help but think there should be a cleaner more efficient way to take care of the process without this overhead. Especially when I know that creating an XmlSerializer consumes better than 25% of the total time in making a web service call. The server responds faster than this method does. I am hopeful I can find some value in doing this differently.
Thanks a bunch.
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform