Home >

An improvement on SessionFactory Initialization

14. March 2009

UPDATE: I have just committed the PersistentConfigurationBuilder for Castle NHibernate Facility. Thank you Jonathon Rossi for informing me!

We have received several complaints about slowness of SessionFactory initialization when there’s hundreds of entities, and Ayende has replied one of them here. It even gets worse if you’re using it in a web environment. You may think that it is not a problem since SessionFactory is initialized once in a web environment, but the major impact is not on production but development. Think how many times you start your application a day.

The problem is not really with NHibernate but with xml validation against the schema. Here are some profiler results for SessionFactory initialization with one thousand entities:

image

As you see, the adding XML resources takes the most time and the reason behind this is the schema validation. There is also an I/O cost involved (1040 resources should be read by NHibernate). There are several ways to get rid of it, one being the serialization of configuration. I spend 3 days (statics prevented me from spotting some bugs in the code) on this and I believe it pretty much works for every configuration. Another way of doing this is the merging of HBM files, which I believe faster than Serialization as Deserialization also takes some amount.

Now the results for the one using the Deserialized Configuration.

image

A nice feature of dotTrace allows us to compare the performance improvements over the old way.

image

We got 10 seconds rescued! Yay!

Now I am going to show how I used this feature in Castle NHibernate Facility. We have IConfigurationBuilder that is used to integrate various Configuration sources (such as FluentNHibernate).

First of all I must ensure that if any of the files that are used to create the Configuration change, we shouldn’t use the serialized configuration, instead the Configuration should be re-created.

public override Configuration GetConfiguration(IConfiguration config)
{
    log.Debug("Building the Configuration");

    string fileName = config.Attributes["fileName"];

    IConfiguration dependsOn = config.Children["dependsOn"];
    IList<string> list = new List<string>();

    foreach (var on in dependsOn.Children)
        list.Add(on.Value);

    Configuration cfg;
    if (IsNewConfigurationRequired(fileName, list))
    {
        log.Debug("Configuration is either old or some of the dependencies have changed");
        using(var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
        {
            cfg = base.GetConfiguration(config);
            this.WriteConfigurationToStream(fileStream, cfg);
        }
    }
    else
    {
        using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
        {
            cfg = this.GetConfigurationFromStream(fileStream);
        }
    }
    return cfg;
}protected virtual bool IsNewConfigurationRequired(string fileName,IList<string> dependencies)
{
    if (!File.Exists(fileName))
        return true;
    FileInfo fi = new FileInfo(fileName);
    DateTime lastModified = fi.LastWriteTime;
    bool requiresNew=false;
    for (int i = 0; i < dependencies.Count && !requiresNew; i++)
    {
        FileInfo dependency = new FileInfo(dependencies[i]);
        DateTime dependencyLastModified = dependency.LastWriteTime;
        requiresNew |= dependencyLastModified > lastModified;
    }
    return requiresNew;
}

Code doesn’t look really good, I guess, so I am open to any suggestions on improvement. The code is not yet in Castle Codebase, as our NH dependency on trunk is not the latest (and i am too lazy to update it). When I find time, I may update the dependency if others agree.

There is one thing that you have to be careful about. You must be aware that if you’re using IUserType, IInterceptor, ISqlFunction etc, all of those should be Serializable too!

, ,

Comments

3/14/2009 1:26:22 PM #
Thanks Tuna!
This one will be really useful.
I'm looking foward for Castle to get updated to try this. ;)
3/14/2009 2:58:54 PM #
German, I have just committed the modifications for this! Feel free to report any bug and improvement as usual Smile
4/12/2009 8:35:07 PM #
Hi Tuna,

Great work finding this!  Is there a patch available for NHibernate 1.2 (we are not using Castle) that I could get, to integrate this into our software?

Thanks
Craig
4/12/2009 8:40:22 PM #
Hi Craig,

No it is not available, we stopped maintaining 1.2 release a long ago. You should really consider updating your NH to 2.1
4/13/2009 12:50:13 AM #
Tuna,

I'll make that comment to the developers.  I'm seeing this problem in a legacy version of our software and I don't think we can upgrade it in the old version, though doing so in our trunk is definitely possible.

Are you able to give me the patch as it exists for NH 2.1 and we can look at using some of it in our backport for now?  Is there a changeset we can pull from SVN or something?

Thanks
Craig
4/13/2009 1:03:18 AM #
(Also, it seems 2.1 isn't out yet?  Did you mean 2.0.1?)
4/13/2009 1:20:25 AM #
Found the changeset at nhibernate.svn.sourceforge.net/.../nhibernate  Will see if I can apply it to our older code.  Thank you very much for your work on NHibernate!
4/13/2009 7:09:10 AM #
Hi Craig,

You can take the patch from the SVN, I believe. This feature is implemented in rev 4122.
NH 2.1 is not yet out (it is 2.1 Alpha3 atm) but will be out soon.
Comments are closed