Home

Awesome

FastExpressionCompiler

<img src="./logo.png" alt="logo"/>

latest release notes Windows buildlicense

Targets .NET 6+, .NET 4.7.2+, .NET Standard 2.0+

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 9.0

BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.4391/23H2/2023Update/SunValley3)
Intel Core i9-8950HK CPU 2.90GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK 9.0.100
  [Host]     : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
  DefaultJob : .NET 9.0.0 (9.0.24.52809), 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:

MethodMeanErrorStdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Compile151.570 us3.0196 us6.7538 us44.272.130.7324-4.49 KB2.92
CompileFast3.425 us0.0676 us0.0664 us1.000.030.24410.23651.54 KB1.00

Invoking the compiled delegate (comparing to the direct constructor call):

MethodMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
DirectConstructorCall6.920 ns0.2007 ns0.3462 ns7.051 ns0.860.060.005132 B1.00
CompiledLambda8.095 ns0.2195 ns0.5216 ns7.845 ns1.010.080.005132 B1.00
FastCompiledLambda8.066 ns0.2206 ns0.3234 ns8.156 ns1.000.060.005132 B1.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:

MethodMeanErrorStdDevMedianRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Compile421.09 us8.382 us18.221 us413.02 us36.292.091.95310.976612.04 KB2.61
CompileFast11.62 us0.230 us0.464 us11.42 us1.000.060.73240.70194.62 KB1.00

Invoking compiled delegate comparing to direct method call:

MethodMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
DirectMethodCall43.45 ns0.922 ns1.905 ns44.13 ns1.090.080.0268168 B1.62
Invoke_Compiled1,181.25 ns23.664 ns56.240 ns1,161.87 ns29.662.240.0420264 B2.54
Invoke_CompiledFast39.96 ns0.856 ns2.442 ns38.96 ns1.000.080.0166104 B1.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:

MethodMeanErrorStdDevMedianRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Compile_SystemExpression89.076 us2.6699 us7.6605 us85.180 us28.123.050.73240.48834.74 KB3.41
CompileFast_SystemExpression3.138 us0.0550 us0.0565 us3.118 us0.990.030.22130.21361.39 KB1.00
CompileFast_LightExpression3.180 us0.0602 us0.0591 us3.163 us1.000.000.22130.21361.39 KB1.00

Invoking the compiled delegate compared to the normal delegate and the direct call:

MethodMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
DirectCall8.388 ns0.2655 ns0.7575 ns8.092 ns1.000.070.005132 B1.00
Compiled_SystemExpression9.474 ns0.1870 ns0.4105 ns9.381 ns1.100.050.005132 B1.00
CompiledFast_SystemExpression8.575 ns0.1624 ns0.1440 ns8.517 ns1.000.020.005132 B1.00
CompiledFast_LightExpression8.584 ns0.0776 ns0.0862 ns8.594 ns1.000.000.005132 B1.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.

Sample expression

Creating the expression:

MethodMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
Create_SystemExpression1,110.9 ns22.19 ns62.23 ns1,086.1 ns7.250.560.20601304 B2.63
Create_LightExpression153.7 ns3.14 ns8.61 ns150.5 ns1.000.080.0789496 B1.00
Create_LightExpression_with_intrinsics161.0 ns2.80 ns2.19 ns161.0 ns1.050.060.0777488 B0.98

Creating and compiling:

MethodMeanErrorStdDevRatioRatioSDGen0Gen1AllocatedAlloc Ratio
Create_SystemExpression_and_Compile212.157 us4.2180 us11.4036 us44.773.310.97660.48837.15 KB2.95
Create_SystemExpression_and_CompileFast6.656 us0.1322 us0.3065 us1.400.100.51880.48833.27 KB1.35
Create_LightExpression_and_CompileFast4.751 us0.0947 us0.2411 us1.000.070.38150.36622.42 KB1.00
CreateLightExpression_and_CompileFast_with_intrinsic4.604 us0.0918 us0.1915 us0.970.060.38150.36622.35 KB0.97

Difference between FastExpressionCompiler and FastExpressionCompiler.LightExpression

FastExpressionCompiler

FastExpressionCompiler.LightExpression

Both FastExpressionCompiler and FastExpressionCompiler.LightExpression

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:

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:

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 and Code Generation

FEC V3 has added powerful diagnostics and code generation tools.

Diagnostics

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);

ThrowOnNotSupportedExpression and NotSupported_ flags

FEC V3.1 has added the compiler flag CompilerFlags.ThrowOnNotSupportedExpression. When passed to CompileFast(flags: CompilerFlags.ThrowOnNotSupportedExpression) and the expression contains not (yet) supported Expression node the compilation will throw the exception instead of returning null.

To get the whole list of the not yet supported cases you may check in Result.NotSupported_ enum values.

Code Generation

The Code Generation capabilities are available via the ToCSharpString and ToExpressionString extension methods.

Note: 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 test the presence or absence of this comment.

Additional optimizations

  1. Using FastExpressionCompiler.LightExpression.Expression instead of System.Linq.Expressions.Expression for the faster expression creation.
  2. 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 the Block, Try, or Goto 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>