FastExpressionCompiler 4.0.2
FastExpressionCompiler
Targets .NET 6, 7, .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 ;-)
The problem
ExpressionTree compilation is used by the wide variety of tools, e.g. IoC/DI containers, Serializers, ORMs and OOMs.
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).
Benchmarks
Updated to .NET 8.0
BenchmarkDotNet v0.13.10, Windows 11 (10.0.22621.2428/22H2/2022Update/SunValley2)
11th Gen Intel Core i7-1185G7 3.00GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.100-rc.2.23502.2
[Host] : .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2
DefaultJob : .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2
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 | Median | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|---|
| Compile | 121.969 us | 2.4180 us | 5.6040 us | 120.830 us | 35.77 | 2.46 | 0.7324 | - | 4.49 KB | 2.92 |
| CompileFast | 3.406 us | 0.0677 us | 0.1820 us | 3.349 us | 1.00 | 0.00 | 0.2441 | 0.2365 | 1.54 KB | 1.00 |
Invoking the compiled delegate (comparing to the direct constructor call):
| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| DirectConstructorCall | 5.734 ns | 0.1501 ns | 0.2745 ns | 5.679 ns | 0.86 | 0.05 | 0.0051 | 32 B | 1.00 |
| CompiledLambda | 6.857 ns | 0.1915 ns | 0.5434 ns | 6.704 ns | 1.01 | 0.09 | 0.0051 | 32 B | 1.00 |
| FastCompiledLambda | 6.746 ns | 0.1627 ns | 0.1442 ns | 6.751 ns | 1.00 | 0.00 | 0.0051 | 32 B | 1.00 |
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 | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| Compile | 442.02 us | 8.768 us | 21.998 us | 40.00 | 2.34 | 1.9531 | 0.9766 | 12.04 KB | 2.61 |
| CompileFast | 11.06 us | 0.221 us | 0.441 us | 1.00 | 0.00 | 0.7324 | 0.7019 | 4.62 KB | 1.00 |
Invoking compiled delegate comparing to direct method call:
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|
| DirectMethodCall | 35.51 ns | 0.783 ns | 1.308 ns | 0.86 | 0.08 | 0.0267 | 168 B | 1.62 |
| Invoke_Compiled | 1,096.15 ns | 21.507 ns | 41.437 ns | 27.15 | 2.75 | 0.0420 | 264 B | 2.54 |
| Invoke_CompiledFast | 37.65 ns | 1.466 ns | 4.299 ns | 1.00 | 0.00 | 0.0166 | 104 B | 1.00 |
Manually composed expression with parameters and closure
var a = new A();
var bParamExpr = Expression.Parameter(typeof(B), "b");
var expr = Expression.Lambda(
Expression.New(_ctorX,
Expression.Constant(a, typeof(A)), bParamExpr),
bParamExpr);
Compiling expression:
| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|---|
| Compile_SystemExpression | 89.076 us | 2.6699 us | 7.6605 us | 85.180 us | 28.12 | 3.05 | 0.7324 | 0.4883 | 4.74 KB | 3.41 |
| CompileFast_SystemExpression | 3.138 us | 0.0550 us | 0.0565 us | 3.118 us | 0.99 | 0.03 | 0.2213 | 0.2136 | 1.39 KB | 1.00 |
| CompileFast_LightExpression | 3.180 us | 0.0602 us | 0.0591 us | 3.163 us | 1.00 | 0.00 | 0.2213 | 0.2136 | 1.39 KB | 1.00 |
Invoking the compiled delegate compared to the normal delegate and the direct call:
| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| DirectCall | 8.388 ns | 0.2655 ns | 0.7575 ns | 8.092 ns | 1.00 | 0.07 | 0.0051 | 32 B | 1.00 |
| Compiled_SystemExpression | 9.474 ns | 0.1870 ns | 0.4105 ns | 9.381 ns | 1.10 | 0.05 | 0.0051 | 32 B | 1.00 |
| CompiledFast_SystemExpression | 8.575 ns | 0.1624 ns | 0.1440 ns | 8.517 ns | 1.00 | 0.02 | 0.0051 | 32 B | 1.00 |
| CompiledFast_LightExpression | 8.584 ns | 0.0776 ns | 0.0862 ns | 8.594 ns | 1.00 | 0.00 | 0.0051 | 32 B | 1.00 |
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 | Gen0 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|
| Create_SystemExpression | 1,039.5 ns | 20.75 ns | 45.98 ns | 8.29 | 0.50 | 0.2060 | 1304 B | 2.63 |
| Create_LightExpression | 125.7 ns | 2.46 ns | 5.99 ns | 1.00 | 0.00 | 0.0789 | 496 B | 1.00 |
| Create_LightExpression_with_intrinsics | 130.0 ns | 2.47 ns | 6.25 ns | 1.04 | 0.07 | 0.0777 | 488 B | 0.98 |
Creating and compiling:
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|---|---|
| Create_SystemExpression_and_Compile | 159.184 us | 2.9731 us | 7.1235 us | 37.34 | 1.65 | 0.9766 | 0.4883 | 7.4 KB | 3.06 |
| Create_SystemExpression_and_CompileFast | 5.923 us | 0.0996 us | 0.1771 us | 1.34 | 0.05 | 0.5188 | 0.5035 | 3.27 KB | 1.35 |
| Create_LightExpression_and_CompileFast | 4.399 us | 0.0484 us | 0.0453 us | 1.00 | 0.00 | 0.3815 | 0.3662 | 2.42 KB | 1.00 |
| CreateLightExpression_and_CompileFast_with_intrinsic | 4.384 us | 0.0835 us | 0.0697 us | 1.00 | 0.02 | 0.3815 | 0.3662 | 2.35 KB | 0.97 |
Difference between FastExpressionCompiler and FastExpressionCompiler.LightExpression
FastExpressionCompiler
- Provides the
CompileFastextension methods for theSystem.Linq.Expressions.LambdaExpression.
FastExpressionCompiler.LightExpression
- Provides the
CompileFastextension methods forFastExpressionCompiler.LightExpression.LambdaExpression. - Provides the drop-in expression replacement with the less consumed memory and the faster construction at the cost of the less validation.
- Includes its own
ExpressionVisitor. - Supports
ToExpressionmethod 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, LINQ2DB, MapsterMapper
Considering: Moq, 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(_ctorX,
Expression.Constant(a, typeof(A)), bParamExpr),
bParamExpr);
var f = expr.CompileFast();
var x = f(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(_ctorX, Constant(a, typeof(A)), bParamExpr),
bParamExpr);
var f = expr.CompileFast();
var x = f(new B());
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 does not support yet:
QuoteDynamicRuntimeVariablesDebugInfoMemberInitwith theMemberMemberBindingand theListMemberBindingbinding typesNewArrayInitmulti-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.
ThrowOnNotSupportedExpression and NotSupported cases enum
FEC V3.1 adds to the compiler flags the CompilerFlags.ThrowOnNotSupportedExpression
so that compiling the expression with not supported node will throw the respective exception instead of returning null.
To get the actual list of the not supported cases you may check NotSupported enum.
Additional optimizations
- Using
FastExpressionCompiler.LightExpression.Expressioninstead ofSystem.Linq.Expressions.Expressionfor the faster expression creation. - Using
.TryCompileWithPreCreatedClosureand.TryCompileWithoutClosuremethods 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, orGotoexpressions.
Bitten Ice Pop icon icon by Icons8
Showing the top 20 packages that depend on FastExpressionCompiler.
| Packages | Downloads |
|---|---|
|
Masuit.Tools.Core
包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
官网教程:https://ldqk.org/55
github:https://github.com/ldqk/Masuit.Tools
|
3 |
|
Masuit.Tools.Abstractions
Masuit.Tools基础公共库,包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
|
3 |
|
Masuit.Tools.Core
包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
官网教程:https://ldqk.org/55
github:https://github.com/ldqk/Masuit.Tools
|
2 |
v4.0.2 Bug-fix release
- added: #379 Add direct net8.0 target as soon as Appveyor CI will support it
- fixed: #380 Comparisons with nullable types
- fixed: #381 NullReferenceException with V4.X when trying to map
- fixed: #386 Value can not be null(parametr 'meth')
v4.0.1 Bug-fix release
- fixed: #374 CompileFast doesn't work with HasFlag
v4.0.0 Major release
fixed: #352 xxxAssign doesn't work with MemberAccess
fixed: #353 NullReferenceException when calling CompileFast() results
fixed: #357 Invalid program exception
fixed: #366 FastExpressionCompiler[v3.3.4] gives incorrect results in some linq operations
fixed: #368 Fix duplicate nested lambda compilation
fixed: #374 CompileFast doesn't work with HasFlag
added: #264 Optimize the array index emit to emit specific Ldelem_ code instead of generic Ldelem
added: #273 Implement IArgumentProvider for the BlockExpression to minimize the consumed memory
added: #346 Is it possible to implement ref local variables?
added: #359 Improve the performance of variable lookup
added: #367 Better diagnostics and debugging with error codes from Collect and Compile rounds
added: #369 Decrease memory occupied by the LightExpression Block by storing Expression in SmallList (partly on stack)
added: #370 Optimize any nested lambda compilation (LINQ) by compiling right after collect one-by-one
added: #372 Support LightExpression.Expression.TryConvertDelegateIntrinsic for converting one type of delegate to another
added: #373 Support custom C# printing for the LightExpression.Expression
.NET Framework 4.5
- System.ValueTuple (>= 4.5.0)
.NET 6.0
- No dependencies.
.NET 7.0
- No dependencies.
.NET Standard 2.0
- System.Dynamic.Runtime (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.7.0)
.NET Standard 2.1
- No dependencies.
| Version | Downloads | Last updated |
|---|---|---|
| 5.3.0 | 2 | 01/10/2026 |
| 5.2.0 | 1 | 01/10/2026 |
| 5.2.0-preview-02 | 1 | 01/10/2026 |
| 5.2.0-preview-01 | 1 | 01/10/2026 |
| 5.1.1 | 1 | 01/10/2026 |
| 5.1.0 | 1 | 01/10/2026 |
| 5.0.3 | 1 | 01/10/2026 |
| 5.0.2 | 1 | 01/10/2026 |
| 5.0.1 | 1 | 01/10/2026 |
| 5.0.0 | 1 | 01/10/2026 |
| 4.2.2 | 1 | 01/10/2026 |
| 4.2.1 | 1 | 01/10/2026 |
| 4.2.0 | 1 | 01/10/2026 |
| 4.1.0 | 1 | 01/10/2026 |
| 4.0.2 | 1 | 01/10/2026 |
| 4.0.1 | 1 | 01/10/2026 |
| 4.0.0 | 1 | 01/10/2026 |
| 3.4.0-preview-01 | 1 | 01/10/2026 |
| 3.3.4 | 1 | 01/10/2026 |
| 3.3.3 | 1 | 01/10/2026 |
| 3.3.2 | 1 | 01/10/2026 |
| 3.3.1 | 1 | 01/10/2026 |
| 3.3.0 | 1 | 01/10/2026 |
| 3.2.2 | 1 | 01/10/2026 |
| 3.2.1 | 1 | 01/10/2026 |
| 3.2.0 | 1 | 01/10/2026 |
| 3.1.0 | 1 | 01/10/2026 |
| 3.1.0-preview-03 | 1 | 01/10/2026 |
| 3.1.0-preview-02 | 1 | 01/10/2026 |
| 3.1.0-preview-01 | 1 | 01/10/2026 |
| 3.0.6-preview-01 | 1 | 01/10/2026 |
| 3.0.5 | 1 | 01/10/2026 |
| 3.0.4 | 1 | 01/10/2026 |
| 3.0.3 | 1 | 01/10/2026 |
| 3.0.2 | 1 | 01/10/2026 |
| 3.0.1 | 1 | 01/10/2026 |
| 3.0.0 | 1 | 01/10/2026 |
| 3.0.0-preview-07 | 1 | 01/10/2026 |
| 3.0.0-preview-06 | 1 | 01/10/2026 |
| 3.0.0-preview-05 | 1 | 01/10/2026 |
| 3.0.0-preview-04 | 1 | 01/10/2026 |
| 3.0.0-preview-03 | 1 | 01/10/2026 |
| 3.0.0-preview-02 | 1 | 01/10/2026 |
| 3.0.0-preview-01 | 1 | 01/10/2026 |
| 2.0.0 | 1 | 01/10/2026 |
| 2.0.0-preview-03 | 1 | 01/10/2026 |
| 2.0.0-preview-02 | 1 | 01/10/2026 |
| 2.0.0-preview-01 | 1 | 01/10/2026 |
| 1.10.1 | 1 | 01/10/2026 |
| 1.10.0 | 1 | 01/10/2026 |
| 1.9.0 | 1 | 01/10/2026 |
| 1.8.0 | 1 | 01/10/2026 |
| 1.7.2 | 1 | 01/10/2026 |
| 1.7.1 | 1 | 01/10/2026 |
| 1.7.0 | 1 | 01/10/2026 |
| 1.6.0 | 1 | 01/10/2026 |
| 1.5.0 | 1 | 01/10/2026 |
| 1.4.0 | 1 | 01/10/2026 |
| 1.3.0 | 1 | 01/10/2026 |
| 1.2.2 | 1 | 01/10/2026 |
| 1.2.1 | 1 | 01/10/2026 |
| 1.2.0 | 1 | 01/10/2026 |
| 1.1.1 | 1 | 01/10/2026 |
| 1.1.0 | 1 | 01/10/2026 |
| 1.0.1 | 1 | 01/10/2026 |
| 1.0.0 | 1 | 01/10/2026 |
| 1.0.0-preview-04 | 1 | 01/10/2026 |
| 1.0.0-preview-03 | 1 | 01/10/2026 |
| 1.0.0-preview-02 | 1 | 01/10/2026 |
| 1.0.0-preview-01 | 1 | 01/10/2026 |