dadhi / Fastexpressioncompiler
Projects that are alternatives of or similar to Fastexpressioncompiler
FastExpressionCompiler
Targets .NET Standard 2.0, 2.1 and .NET 4.5
NuGet packages:
The project was originally a part of the DryIoc, so check it out ;-)
-
FastExpressionCompiler
- The problem
- The solution
- Difference between FastExpressionCompiler and FastExpressionCompiler.LightExpression
- Who's using it
- How to use
-
Benchmarks
- Hoisted expression with the constructor and two arguments in closure
- Hoisted expression with the static method and two nested lambdas and two arguments in closure
- Manually composed expression with parameters and closure
- FastExpressionCompiler.LightExpression.Expression vs System.Linq.Expressions.Expression
- How it works
- Diagnostics
- Additional optimizations
The problem
ExpressionTree compilation is used by the wide variety of tools, e.g. IoC/DI containers, Serializers, OO Mappers.
But Expression.Compile()
is just slow.
Moreover the compiled delegate may be slower than the manually created delegate because of the reasons:
TL;DR;
Expression.Compile creates a DynamicMethod and associates it with an anonymous assembly to run it in a sand-boxed environment. This makes it safe for a dynamic method to be emitted and executed by partially trusted code but adds some run-time overhead.
See also a deep dive to Delegate internals.
The solution
The FastExpressionCompiler .CompileFast()
extension method is 10-40x times faster than .Compile()
.
The compiled delegate may be in some cases a lot faster than the one produced by .Compile()
.
Note: The actual performance may vary depending on the multiple factors: platform, how complex is expression, does it have a closure, does it contain nested lambdas, etc.
In addition, the memory consumption taken by the compilation will be much smaller (check the Allocated
column in the benchmarks below).
Difference between FastExpressionCompiler and FastExpressionCompiler.LightExpression
FastExpressionCompiler
- Provides the
CompileFast
extension methods for theSystem.Linq.Expressions.LambdaExpression
.
FastExpressionCompiler.LightExpression
- Provides the
CompileFast
extension methods forFastExpressionCompiler.LightExpression.LambdaExpression
. - Provides the drop-in Expression replacement with the faster construction and less memory at the cost of less validation.
- Includes its own
ExpressionVisitor
. - Supports
ToExpression
method to convert back to theSystem.Linq.Expressions.Expression
.
Both FastExpressionCompiler and FastExpressionCompiler.LightExpression
- Support
ToCSharpString()
method to output the compile-able C# code represented by expression. - Support
ToExpressionString()
method to output the expression construction C# code, so given the expression object you'll get e.g.Expression.Lambda(Expression.New(...))
.
Who's using it
Marten, Rebus, StructureMap, Lamar, ExpressionToCodeLib, NServiceBus, MapsterMapper
Considering: Moq, LINQ to DB, Apex.Serialization
How to use
Install from the NuGet and add the using FastExpressionCompiler;
and replace the call to the .Compile()
with the .CompileFast()
extension method.
Note: CompileFast
has an optional parameter bool ifFastFailedReturnNull = false
to disable fallback to Compile
.
Examples
Hoisted lambda expression (created by the C# Compiler):
var a = new A(); var b = new B();
Expression<Func<X>> expr = () => new X(a, b);
var getX = expr.CompileFast();
var x = getX();
Manually composed lambda expression:
var a = new A();
var bParamExpr = Expression.Parameter(typeof(B), "b");
var expr = Expression.Lambda(
Expression.New(typeof(X).GetTypeInfo().DeclaredConstructors.First(),
Expression.Constant(a, typeof(A)), bParamExpr),
bParamExpr);
var getX = expr.CompileFast();
var x = getX(new B());
Note: You may simplify Expression usage and enable faster refactoring with the C# using static
statement:
using static System.Linq.Expressions.Expression;
// or
// using static FastExpressionCompiler.LightExpression.Expression;
var a = new A();
var bParamExpr = Parameter(typeof(B), "b");
var expr = Lambda(
New(typeof(X).GetTypeInfo().DeclaredConstructors.First(), Constant(a, typeof(A)), bParamExpr),
bParamExpr);
var x = expr.CompileFast()(new B());
Benchmarks
Updated to .NET 5
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.630 (2004/?/20H1)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT
DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT
Hoisted expression with the constructor and two arguments in closure
var a = new A();
var b = new B();
Expression<Func<X>> e = () => new X(a, b);
Compiling expression:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Compile | 274.722 us | 5.3167 us | 5.6888 us | 47.47 | 1.67 | 0.9766 | 0.4883 | - | 4.52 KB |
CompileFast | 5.790 us | 0.1118 us | 0.1197 us | 1.00 | 0.00 | 0.3815 | 0.1907 | 0.0305 | 1.57 KB |
Invoking the compiled delegate (comparing to the direct constructor call):
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
DirectConstructorCall | 7.634 ns | 0.2462 ns | 0.2303 ns | 0.54 | 0.02 | 0.0076 | - | - | 32 B |
CompiledLambda | 15.553 ns | 0.1805 ns | 0.1600 ns | 1.09 | 0.02 | 0.0076 | - | - | 32 B |
FastCompiledLambda | 14.241 ns | 0.2844 ns | 0.2521 ns | 1.00 | 0.00 | 0.0076 | - | - | 32 B |
Hoisted expression with the static method and two nested lambdas and two arguments in closure
var a = new A();
var b = new B();
Expression<Func<X>> getXExpr = () => CreateX((aa, bb) => new X(aa, bb), new Lazy<A>(() => a), b);
Compiling expression:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Compile | 479.87 us | 5.039 us | 4.208 us | 31.98 | 0.59 | 2.9297 | 1.4648 | - | 12.17 KB |
CompileFast | 15.00 us | 0.291 us | 0.298 us | 1.00 | 0.00 | 1.1902 | 0.5493 | 0.0916 | 4.86 KB |
Invoking compiled delegate comparing to direct method call:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
DirectMethodCall | 53.24 ns | 0.721 ns | 0.674 ns | 1.06 | 0.02 | 0.0401 | - | - | 168 B |
Invoke_Compiled | 1,486.71 ns | 13.620 ns | 12.741 ns | 29.64 | 0.25 | 0.0629 | - | - | 264 B |
Invoke_CompiledFast | 50.20 ns | 0.484 ns | 0.404 ns | 1.00 | 0.00 | 0.0248 | - | - | 104 B |
Manually composed expression with parameters and closure
var a = new A();
var bParamExpr = Expression.Parameter(typeof(B), "b");
var expr = Expression.Lambda(
Expression.New(typeof(X).GetTypeInfo().DeclaredConstructors.First(),
Expression.Constant(a, typeof(A)), bParamExpr),
bParamExpr);
Compiling expression:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Compile | 148.633 us | 1.4863 us | 1.2411 us | 33.20 | 1.05 | 0.9766 | 0.4883 | - | 4.78 KB |
CompileFast | 4.498 us | 0.0887 us | 0.1022 us | 1.02 | 0.04 | 0.3510 | 0.1755 | 0.0305 | 1.46 KB |
CompileFast_LightExpression | 4.365 us | 0.0860 us | 0.1364 us | 1.00 | 0.00 | 0.3433 | 0.1678 | 0.0305 | 1.42 KB |
Invoking the compiled delegate compared to the normal delegate and the direct call:
Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|
DirectLambdaCall | 11.86 ns | 0.140 ns | 0.131 ns | 1.00 | 0.0076 | - | - | 32 B |
CompiledLambda | 13.44 ns | 0.115 ns | 0.096 ns | 1.13 | 0.0076 | - | - | 32 B |
FastCompiledLambda | 12.43 ns | 0.173 ns | 0.154 ns | 1.05 | 0.0076 | - | - | 32 B |
FastCompiledLambda_LightExpression | 11.87 ns | 0.121 ns | 0.101 ns | 1.00 | 0.0076 | - | - | 32 B |
FastExpressionCompiler.LightExpression.Expression vs System.Linq.Expressions.Expression
FastExpressionCompiler.LightExpression.Expression
is the lightweight version of System.Linq.Expressions.Expression
.
It is designed to be a drop-in replacement for the System Expression - just install the FastExpressionCompiler.LightExpression package instead of FastExpressionCompiler and replace the usings
using System.Linq.Expressions;
using static System.Linq.Expressions.Expression;
with
using static FastExpressionCompiler.LightExpression.Expression;
namespace FastExpressionCompiler.LightExpression.UnitTests
You may look at it as a bare-bone wrapper for the computation operation node which helps you to compose the computation tree (without messing with the IL emit directly).
It won't validate operations compatibility for the tree the way System.Linq.Expression
does it, and partially why it is so slow.
Hopefully you are checking the expression arguments yourself and not waiting for the Expression
exceptions to blow-up.
Creating the expression:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
CreateExpression | 2,508.2 ns | 44.12 ns | 36.84 ns | 8.83 | 0.14 | 0.3128 | - | - | 1312 B |
CreateLightExpression | 284.2 ns | 5.19 ns | 4.85 ns | 1.00 | 0.00 | 0.1316 | - | - | 552 B |
Creating and compiling:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
CreateExpression_and_Compile | 244.61 us | 4.700 us | 6.111 us | 17.92 | 0.50 | 1.7090 | 0.7324 | - | 7.2 KB |
CreateExpression_and_CompileFast | 17.69 us | 0.350 us | 0.443 us | 1.31 | 0.04 | 1.8005 | 0.8850 | 0.0305 | 7.36 KB |
CreateLightExpression_and_CompileFast | 13.50 us | 0.152 us | 0.143 us | 1.00 | 0.00 | 1.5869 | 0.7935 | 0.0305 | 6.58 KB |
How it works
The idea is to provide the fast compilation for the supported expression types
and fallback to the system Expression.Compile()
for the not supported types:
What's not supported yet
FEC V3 does not support yet:
Quote
Dynamic
RuntimeVariables
DebugInfo
-
MemberInit
with theMemberMemberBinding
and theListMemberBinding
binding types -
NewArrayInit
multi-dimensional array initializer is not supported yet
To find what nodes are not supported in your expression you may use the technic described below in the Diagnostics section.
The compilation is done by traversing the expression nodes and emitting the IL. The code is tuned for the performance and the minimal memory consumption.
The expression is traversed twice:
- 1st round is to collect the constants and nested lambdas into the closure objects.
- 2nd round is to emit the IL code and create the delegate using the
DynamicMethod
.
If visitor finds the not supported expression node or the error condition,
the compilation is aborted, and null
is returned enabling the fallback to System .Compile()
.
Diagnostics
FEC V3 adds powerful diagnostics tools.
You may pass the optional CompilerFlags.EnableDelegateDebugInfo
into the CompileFast
methods.
EnableDelegateDebugInfo
adds the diagnostic info into the compiled delegate including its source Expression and C# code.
Can be used as following:
var f = e.CompileFast(true, CompilerFlags.EnableDelegateDebugInfo);
var di = f.Target as IDelegateDebugInfo;
Assert.IsNotNull(di.Expression);
Assert.IsNotNull(di.ExpressionString);
Assert.IsNotNull(di.CSharpString);
Those conversion capabilities are also available as the ToCSharpString
and ToExpressionString
extension methods.
Besides that, when converting the source expression to either C# code or to the Expression construction code you may find
the // NOT_SUPPORTED_EXPRESSION
comments marking the not supported yet expressions by FEC. So you may verify the presence or absence of this comment in a test.
Additional optimizations
- Using
FastExpressionCompiler.LightExpression.Expression
instead ofSystem.Linq.Expressions.Expression
for the faster expression creation. - Using
.TryCompileWithPreCreatedClosure
and.TryCompileWithoutClosure
methods when you know the expression at hand and may skip the first traversing round, e.g. for the "static" expression which does not contain the bound constants. Note: You cannot skip the 1st round if the expression contains theBlock
,Try
, orGoto
expressions.
Bitten Ice Pop icon icon by Icons8