[From the developer mailing list...]
All,
I have completed work on a lightweight code generation (LCG) version of
NHibernate which removed the need to use reflection at runtime.
Reflection is only used during startup time to help compile the
generated IL code. There is no temporary assembly with this
approach. Instead, it is using the new DynamicMethod class in .NET
2.0.
IL opcodes are emitted at runtime in dynamically created methods.
These methods have direct access to the member data of a class,
without concern for private vs. public accessibility on those
fields. It is as if these methods were hand written to get/set data
to/from the class.
I have included a link to the source tree for a modified NHibernate 1.0.2.0
codebase. I am using VS 2005, so the project has been converted. The
important information to get out of this source tree is the changes
to the followings files:
\Cfg\Environment.cs - in particular, the new
UseLightweightCodeGeneration property.
\Persister\GetSetHelperFactory.cs - added ability to use the LCG
version of the GetSetHelper.
\Persister\LCGSetSetHelper.cs - where all the magic occurs
\Property\IGetter.cs - added two members, CanEmit and Emit
\Property\ISetter.cs - added two members, CanEmit and Emit
(...there may be others, but these are the big ones...)
The biggest change that you may notice is the change the IGetter and
ISetter interfaces. This change allows a getter and setter
implementation to contribute the generation of IL code to get/set a
value. If an implementation chooses not to support IL generation
(i.e. CanEmit returns false), then the generater IL code will just
call into the getter/setter at runtime, just like before. In that
scenario, you have a mixed use of generated IL and reflection
(depending on how your getter/setter works).
Finally, I have also added support for implicit/explicit operator
overloads. If during IL generation, we can detect that a given type
supports an implicit/explicit cast to/from the database data type,
then the generated IL code will use those operator overloads.
In my testing, the use of lightweight code generated methods greatly
improves the performance of NHibernate. Of course, debugging LCG is
a bear!
In order to turn on this new functionality, you need to turn on both
the UseReflectionOptimizer flag and the UseLightweightCodeGeneration
flags.
I hope you find this new functionality useful. Let me know if you
have any questions. My hope is that this functionality will be integrated with the main NHibernate codebase. I am interested in hearing about anyone's trial of this new functionality.
Here is the link:
http://nhibernate.sourceforge.net/contr ... ernate.zip
Enjoy!