Scott Hanselman

XmlSerializers Part II: Am I Insane?

December 05, 2003 Comment on this post [2] Posted in XmlSerializer | Web Services
Sponsored By

Simon Fell's comment regarding my XmlSerializer quandry got me thinking.  He said the obvious:

Only creating the first instance for a particular type is expensive after the first instance there is no overhead.

However, I SWEAR I saw different performance under 1.0.  Has something changed in 1.1?  Time to pull out a copy of .NET 1.0 and run my tests again.

Running with .NET runtime version 1.1.4322.573.
Took 00:00:00.4531163 ms to create 10000 serializers without pre-initializing.
Took 00:00:00.2499952 ms to create the first serializer.
Took 00:00:00.0312494 ms to create 10000 serializers after pre-initializing the first one.

So, perhaps my whole caching XmlSerializerFactory was sillyness, and I was trying to work around some odd behavior that existed in 1.0.  OR, some behavior that existed purely as an artifact of my previous app.

BUT: Joseph Cooney says:

The XmlSerializer has a number of constructors. If you use any of the constructors other than the one that takes a type (for example specify a type and a default namespace) then a new temporary assembly is created EVERY TIME you create a serializer, rather than only once. I can post some code to demonstrate this if you like.

Maybe that's it, and it would make sense.  The basic constructors would allow the generated type & assembly to hang out.  The others would mean that namespace passed in could come from anywhere (it could be highly variable, while the type wouldn't be)...so, they'd just punt and make a new assembly each time.

Either way, this GOOF goes to show - measure.  Don't assume like I did.  I'm off to figure this out...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
December 05, 2003 22:24
A little Reflectoring into the XMLSerializer source shows that in .NET 1.1 it caches the temp assembly using both the type and default namespace as the key. However, 1.0 cached only on the type, and if you specified a namespace it ignored the cache. And it looks like even in 1.1 the other constructor overloads ignore the cache. Good to know.

So no, you're not (entirely) crazy.
December 08, 2003 9:20
I found out that info on the XML serializer from Dino Esposito's .NET XML programming book. Sorry if my incorrectness re: framework 1.0/1.1 led you astray. I shoulda checked it out under framework 1.1 too.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.