Delay creation of instance of XmlSerializer (loading xml assembly) until its needed.

Description

The solution to wasn't the most wise one IMHO. Now there's a static field in configuration.cs which initializes a XmlSerializer even if you don't need it (ie. when mapping by code and you have no hbm.xml files). Starting the XML assembly takes up nearly a second on my fairly new PC, so a better initialization would be helpful.

The current line of code is:

private static XmlSerializer _mappingDocumentSerializer = new XmlSerializer(typeof (HbmMapping));

my suggestion is to rewrite this code to:

private static XmlSerializer _mappingDocumentSerializer = null;

private XmlSerializer mappingDocumentSerializer
{
get
{
if (_mappingDocumentSerializer == null)
_mappingDocumentSerializer = new XmlSerializer(typeof (HbmMapping));
return _mappingDocumentSerializer;
}
}

This way the XmlSerializer will only be created once as well, but only when it's really needed. More info on unnecessary initialization can be found in this article: http://msdn.microsoft.com/en-us/magazine/cc163655.aspx#S3

Environment

None

Activity

Show:

Alex Zaytsev January 24, 2014 at 3:29 AM

Commited to 3.4.x as 97c89997f02e54c2e792b30a125433bd166daa71

Alex Zaytsev January 24, 2014 at 3:12 AM
Edited

Shell we just move the field to NamedXmlDocument ?

Rafał Kłys January 23, 2014 at 2:42 PM

I added a pull request with this change at: https://github.com/nhibernate/nhibernate-core/pull/250

Ted April 11, 2013 at 3:47 PM

Still hoping this will be bumped as it will speed up my project startup with about 2 seconds.

Richard Birkby July 31, 2012 at 9:14 AM

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?

Created November 30, 2011 at 1:17 PM
Updated September 21, 2014 at 12:44 PM
Resolved January 24, 2014 at 3:29 AM
Who's Looking?