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:
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.
A nice feature of dotTrace allows us to compare the performance improvements over the old way.
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!