I\'m generating an expression tree that maps properties from a source object to a destination object, that is then compiled to a Func
You are may compile Expression Tree manually via Reflection.Emit
. It will generally provide faster compilation time (in my case below ~30 times faster), and will allow you to tune emitted result performance. And it not so hard to do, especially if your Expressions are limited known subset.
The idea is to use ExpressionVisitor
to traverse the expression and emit the IL for corresponding expression type. It's also "quite" simple to write your own Visitor to handle the known subset of expressions, and fallback to normal Expression.Compile
for not yet supported expression types.
In my case I am generating the delegate:
Func
The test creates the corresponding expression tree and compares its Expression.Compile
vs visiting and emitting the IL and then creating delegate from DynamicMethod
.
The results:
Compile Expression 3000 times: 814
Invoke Compiled Expression 5000000 times: 724
Emit from Expression 3000 times: 36
Run Emitted Expression 5000000 times: 722
36 vs 814 when compiling manually.
Here the full code.