Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Reflection Optimizer - what does it do?

I've found initializing my session factory is massively quicker whenever I disable the nhibernate reflection optimizer. This is great for testing purposes.

<add key="hibernate.use_reflection_optimizer" value="false" />

My question is what is the knock-on effect of doing this, and why isn't it the default setting? Everything seems to be working just the same.

like image 444
James L Avatar asked Jun 01 '09 15:06

James L


2 Answers

If you want more than a copypaste of the single paragraph mention of the reflection optimizer from the manual, there is a good discussion here of the performance improvements from the reflection optimizer, as well as a brief discussion of the methods.

From my limited understanding (this comes all from reading, no experimentation on my part, so corrections are welcomed) the short of it is that there are actually two reflection optimizers:

  1. codedom, which is basically generated and then compiled code (i.e. nhibernate generates wrapper Getter/Setter code for your entities and then compiles it). From the looks of it this only works on public members, and is probably extra expensive to use because it naively generates the wrapper classes (irrespective if whether a particular field is public), tries to compile it and throws an exception if it fails.

  2. lightweight code generation, which uses the occult practice of reflection.emit to get/set values. This is still a very new area for me, but personal experimentation with this shows that you can use SRE to manipulate private variables very speedily, and the source at least seems to provide emission for basic fields/properties irregardless access modifier. This is the default.

As for the knock-on effect - well, from the shiny graphs in 1 it looks like if you are persisting/hydrating a lot of heavy objects during development it could be quite substantial. If not (and I hazard a guess that you aren't during testing/development) then it seems quite sensible to turn it off.

like image 189
fostandy Avatar answered Sep 19 '22 19:09

fostandy


From the documentation:

Enables use of a runtime-generated class to set or get properties of an entity or component instead of using runtime reflection (System-level property). The use of the reflection optimizer inflicts a certain startup cost on the application but should lead to better performance in the long run. You can not set this property in hibernate.cfg.xml or section of the application configuration file.

like image 22
Min Avatar answered Sep 20 '22 19:09

Min