FastExpressionCompiler 3.4.0-preview-01
Prefix ReservedSee the version list below for details.
dotnet add package FastExpressionCompiler --version 3.4.0-preview-01
NuGet\Install-Package FastExpressionCompiler -Version 3.4.0-preview-01
<PackageReference Include="FastExpressionCompiler" Version="3.4.0-preview-01" />
paket add FastExpressionCompiler --version 3.4.0-preview-01
#r "nuget: FastExpressionCompiler, 3.4.0-preview-01"
// Install FastExpressionCompiler as a Cake Addin #addin nuget:?package=FastExpressionCompiler&version=3.4.0-preview-01&prerelease // Install FastExpressionCompiler as a Cake Tool #tool nuget:?package=FastExpressionCompiler&version=3.4.0-preview-01&prerelease
FastExpressionCompiler
<img src="./logo.png" alt="logo"/>
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 😉
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).
Benchmarks
Updated to .NET 6
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
Intel Core i5-8350U CPU 1.70GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=6.0.201
[Host] : .NET Core 6.0.3 (CoreCLR 6.0.322.12309, CoreFX 6.0.322.12309), X64 RyuJIT
DefaultJob : .NET Core 6.0.3 (CoreCLR 6.0.322.12309, CoreFX 6.0.322.12309), 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 | 272.904 us | 5.4074 us | 11.8694 us | 50.84 | 3.34 | 1.4648 | 0.4883 | - | 4.49 KB |
CompileFast | 5.379 us | 0.1063 us | 0.2048 us | 1.00 | 0.00 | 0.4959 | 0.2441 | 0.0381 | 1.52 KB |
Invoking the compiled delegate (comparing to the direct constructor call):
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
DirectConstructorCall | 7.736 ns | 0.2472 ns | 0.6336 ns | 7.510 ns | 0.57 | 0.05 | 0.0102 | - | - | 32 B |
CompiledLambda | 13.917 ns | 0.2723 ns | 0.3818 ns | 13.872 ns | 1.03 | 0.04 | 0.0102 | - | - | 32 B |
FastCompiledLambda | 13.412 ns | 0.2355 ns | 0.4124 ns | 13.328 ns | 1.00 | 0.00 | 0.0102 | - | - | 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 | 641.72 us | 12.785 us | 26.117 us | 28.87 | 1.78 | 3.9063 | 1.9531 | - | 12.05 KB |
CompileFast | 22.31 us | 0.444 us | 0.876 us | 1.00 | 0.00 | 1.7700 | 0.8850 | 0.1221 | 5.45 KB |
Invoking compiled delegate comparing to direct method call:
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
DirectMethodCall | 67.15 ns | 1.401 ns | 1.965 ns | 1.06 | 0.05 | 0.0535 | - | - | 168 B |
Invoke_Compiled | 1,889.47 ns | 37.145 ns | 53.272 ns | 29.75 | 1.44 | 0.0839 | - | - | 264 B |
Invoke_CompiledFast | 63.21 ns | 1.239 ns | 2.203 ns | 1.00 | 0.00 | 0.0331 | - | - | 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 | 179.266 us | 3.5687 us | 7.2089 us | 39.11 | 2.15 | 1.4648 | 0.7324 | - | 4.74 KB |
CompileFast | 4.791 us | 0.0955 us | 0.2307 us | 1.04 | 0.06 | 0.4578 | 0.2289 | 0.0305 | 1.41 KB |
CompileFast_LightExpression | 4.636 us | 0.0916 us | 0.1531 us | 1.00 | 0.00 | 0.4425 | 0.2213 | 0.0305 | 1.38 KB |
Invoking the compiled delegate compared to the normal delegate and the direct call:
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
DirectLambdaCall | 13.72 ns | 0.274 ns | 0.500 ns | 13.62 ns | 1.05 | 0.06 | 0.0102 | - | - | 32 B |
CompiledLambda | 17.12 ns | 1.006 ns | 2.950 ns | 15.78 ns | 1.24 | 0.15 | 0.0102 | - | - | 32 B |
FastCompiledLambda | 12.87 ns | 0.164 ns | 0.128 ns | 12.88 ns | 0.97 | 0.03 | 0.0102 | - | - | 32 B |
FastCompiledLambda_LightExpression | 13.11 ns | 0.258 ns | 0.471 ns | 13.01 ns | 1.00 | 0.00 | 0.0102 | - | - | 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 | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | |-------------------------------------- |-----------:|----------:|----------:|-----------:|------:|--------:|-------:|------:|------:|----------:| | CreateExpression | 4,698.0 ns | 110.77 ns | 317.81 ns | 4,623.0 ns | 7.99 | 0.85 | 0.4501 | - | - | 1416 B | | CreateLightExpression | 591.2 ns | 15.42 ns | 44.98 ns | 580.7 ns | 1.00 | 0.00 | 0.1574 | - | - | 496 B | | CreateLightExpression_with_intrinsics | 580.2 ns | 16.95 ns | 48.08 ns | 565.0 ns | 0.98 | 0.10 | 0.1554 | - | - | 488 B |
Creating and compiling:
Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|---|
CreateExpression_and_Compile | 541.65 us | 16.585 us | 47.048 us | 520.79 us | 33.98 | 3.97 | 1.9531 | 0.9766 | - | 7.26 KB |
CreateExpression_and_CompileFast | 23.51 us | 0.724 us | 2.102 us | 23.08 us | 1.47 | 0.17 | 1.2207 | 0.6104 | 0.0305 | 3.79 KB |
CreateLightExpression_and_CompileFast | 16.03 us | 0.430 us | 1.227 us | 15.50 us | 1.00 | 0.00 | 0.9155 | 0.4578 | 0.0305 | 2.84 KB |
CreateLightExpression_and_CompileFast_with_intrinsics | 13.94 us | 0.629 us | 1.845 us | 13.37 us | 0.88 | 0.13 | 0.8545 | 0.4272 | 0.0305 | 2.64 KB |
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 less consumed memory and the faster construction at the cost of the 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, 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(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());
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 typesNewArrayInit
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.
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.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.
<a target="_blank" href="https://icons8.com/icons/set/bitten-ice-pop">Bitten Ice Pop icon</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.5
- System.ValueTuple (>= 4.5.0)
-
.NETStandard 2.0
- System.Dynamic.Runtime (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.7.0)
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (57)
Showing the top 5 NuGet packages that depend on FastExpressionCompiler:
Package | Downloads |
---|---|
JasperFx.CodeGeneration
Code Generation Chicanery for .Net |
|
RulesEngine
Rules Engine is a package for abstracting business logic/rules/policies out of the system. This works in a very simple way by giving you an ability to put your rules in a store outside the core logic of the system thus ensuring that any change in rules doesn't affect the core system. |
|
WolverineFx
Build Robust Event Driven Architectures with Simpler Code |
|
DotVVM
DotVVM is an open source ASP.NET-based framework which allows to build interactive web apps easily by using mostly C# and HTML. |
|
Miruken
Miruken handles your application |
GitHub repositories (12)
Showing the top 5 popular GitHub repositories that depend on FastExpressionCompiler:
Repository | Stars |
---|---|
ldqk/Masuit.Tools
全龄段友好的C#万能工具库,码数吐司库,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展、Excel导出等常用封装。诸多功能集一身,代码量不到2MB!
|
|
MapsterMapper/Mapster
A fast, fun and stimulating object to object Mapper
|
|
microsoft/RulesEngine
A Json based Rules Engine with extensive Dynamic expression support
|
|
JasperFx/wolverine
Supercharged .NET server side development!
|
|
ldqk0/Masuit.Tools
该仓库为 https://github.com/ldqk/Masuit.Tools 的镜像仓库,代码更新存在较大的延迟。建议前往源仓库:https://github.com/ldqk/Masuit.Tools
|
Version | Downloads | Last updated |
---|---|---|
5.0.1 | 0 | 12/22/2024 |
5.0.0 | 13,792 | 11/22/2024 |
4.2.2 | 115,906 | 10/13/2024 |
4.2.1 | 126,240 | 7/2/2024 |
4.2.0 | 78,023 | 4/29/2024 |
4.1.0 | 1,231,261 | 1/20/2024 |
4.0.2 | 508 | 1/20/2024 |
4.0.1 | 1,041,783 | 11/23/2023 |
4.0.0 | 1,613,971 | 11/12/2023 |
3.4.0-preview-01 | 3,306 | 8/19/2023 |
3.3.4 | 1,910,155 | 1/17/2023 |
3.3.3 | 2,705,480 | 7/24/2022 |
3.3.2 | 103,046 | 5/27/2022 |
3.3.1 | 23,682 | 5/25/2022 |
3.3.0 | 25,959 | 4/26/2022 |
3.2.2 | 348,931 | 2/2/2022 |
3.2.1 | 1,572,429 | 7/21/2021 |
3.2.0 | 271,051 | 6/14/2021 |
3.1.0 | 130,339 | 5/3/2021 |
3.1.0-preview-03 | 272 | 5/3/2021 |
3.1.0-preview-02 | 288 | 5/3/2021 |
3.1.0-preview-01 | 279 | 5/2/2021 |
3.0.6-preview-01 | 303 | 4/23/2021 |
3.0.5 | 4,122 | 4/21/2021 |
3.0.4 | 6,539 | 4/6/2021 |
3.0.3 | 1,604 | 4/1/2021 |
3.0.2 | 7,704 | 3/30/2021 |
3.0.1 | 538 | 3/27/2021 |
3.0.0 | 1,529 | 3/17/2021 |
3.0.0-preview-07 | 7,584 | 12/25/2020 |
3.0.0-preview-06 | 535 | 12/1/2020 |
3.0.0-preview-05 | 1,527 | 11/27/2020 |
3.0.0-preview-04 | 553 | 11/3/2020 |
3.0.0-preview-03 | 365 | 11/2/2020 |
3.0.0-preview-02 | 4,403 | 10/23/2020 |
3.0.0-preview-01 | 380 | 10/23/2020 |
2.0.0 | 805,839 | 1/25/2019 |
2.0.0-preview-03 | 1,370 | 11/9/2018 |
2.0.0-preview-02 | 1,069 | 10/25/2018 |
2.0.0-preview-01 | 798 | 10/24/2018 |
1.10.1 | 87,627 | 8/8/2018 |
1.10.0 | 1,110 | 8/3/2018 |
1.9.0 | 1,653 | 7/24/2018 |
1.8.0 | 34,995 | 6/24/2018 |
1.7.2 | 17,700 | 6/7/2018 |
1.7.1 | 194,892 | 3/27/2018 |
1.7.0 | 7,039 | 3/17/2018 |
1.6.0 | 51,062 | 12/3/2017 |
1.5.0 | 3,239 | 11/12/2017 |
1.4.0 | 16,288 | 9/9/2017 |
1.3.0 | 1,653 | 8/28/2017 |
1.2.2 | 1,801 | 8/8/2017 |
1.2.1 | 1,372 | 8/8/2017 |
1.2.0 | 1,326 | 8/8/2017 |
1.1.1 | 183,082 | 7/18/2017 |
1.1.0 | 1,223 | 7/13/2017 |
1.0.1 | 2,157 | 5/26/2017 |
1.0.0 | 5,019 | 4/2/2017 |
1.0.0-preview-04 | 1,110 | 3/31/2017 |
1.0.0-preview-03 | 1,679 | 3/30/2017 |
1.0.0-preview-02 | 1,079 | 3/29/2017 |
1.0.0-preview-01 | 1,184 | 3/23/2017 |
## v3.3.4 Bug-fix release
- fixed: #345 EmitCall is for the varargs method and should not be used for normal convention
- fixed: #347 InvalidProgramException on compiling an expression that returns a record which implements IList
- fixed: #349 Error when loading struct parameters closed by the nested lambda e.g. predicate in Linq
- fixed: #355 Error with converting to/from signed/unsigned integers (Thanks to @TYoungSL for the PR!)
- fixed: the C# output for if-else test condition, and inc/dec operations; and for label at the end of the lambda
- fixed: ref assignment C# output
- perf: Replace ILGenerator.Emit(OpCodes.Call, ..) with EmitMethodCall performance
## v3.3.3 Bug-fix release
- fixed: #336 Method TryCompileBoundToFirstClosureParam is returning passed Type array to pool may cause undefined behavior
- fixed: #337 Switch LightExpression.Expression.ToString from System.Expression.ToString pseudo-code to ToCSharpString
- fixed: #338 InvocationExpression is not properly converted to System Expression via ToExpression for non-lambda expression
- fixed: #341 Equality comparison between nullable and null inside Any produces incorrect compiled expression
## v3.3.2 Bug-fix release
- fixed: #335 FastExpressionCompiler.LightExpressions - MemberInit has recursive calls
## v3.3.1 Optimization and bug-fix release
- fixed: #333 AccessViolationException and other suspicious behaviour on invoking result of CompileFast()
- optim: LightExpression.Parameter consumes less memory for the common standard types.
## v3.3.0 Feature and bug-fix release
- added: #235 [GodMode] Expression to IL intrinsic
- added: #325 Add LightExpression.NewNoByRefArgs overloads for performance
- added: #327 Replace direct il.Emit(OpCodes.Call, ...) with EmitMethodCall
- added: #329 Optimize special case compilation for New and Call with no arguments
- added: #330 Optimize nested lambda ClosureInfo memory footprint
- fixed: #324 The LightExpression.New of IEnumerable is recursive
- fixed: #328 Fix the performance of TryEmitConvert to String