FastExpressionCompiler.LightExpression 3.4.0-preview-01

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This is a prerelease version of FastExpressionCompiler.LightExpression.
There is a newer version of this package available.
See the version list below for details.
dotnet add package FastExpressionCompiler.LightExpression --version 3.4.0-preview-01
NuGet\Install-Package FastExpressionCompiler.LightExpression -Version 3.4.0-preview-01
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastExpressionCompiler.LightExpression" Version="3.4.0-preview-01" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FastExpressionCompiler.LightExpression --version 3.4.0-preview-01
#r "nuget: FastExpressionCompiler.LightExpression, 3.4.0-preview-01"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FastExpressionCompiler.LightExpression as a Cake Addin
#addin nuget:?package=FastExpressionCompiler.LightExpression&version=3.4.0-preview-01&prerelease

// Install FastExpressionCompiler.LightExpression as a Cake Tool
#tool nuget:?package=FastExpressionCompiler.LightExpression&version=3.4.0-preview-01&prerelease

FastExpressionCompiler

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

latest release notes Windows buildlicense

Targets .NET Standard 2.0, 2.1 and .NET 4.5

NuGet packages:

  • FastExpressionCompiler NuGet Badge
    • sources package: FastExpressionCompiler.src NuGet Badge
    • sources with the public code made internal: FastExpressionCompiler.Internal.src NuGet Badge
  • FastExpressionCompiler.LightExpression NuGet Badge
    • sources package: FastExpressionCompiler.LightExpression.src NuGet Badge
    • sources with the public code made internal: FastExpressionCompiler.LightExpression.Internal.src NuGet Badge

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.

Sample expression

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 the System.Linq.Expressions.LambdaExpression.

FastExpressionCompiler.LightExpression

  • Provides the CompileFast extension methods for FastExpressionCompiler.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 the System.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 the MemberMemberBinding and the ListMemberBinding 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.

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

  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>

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on FastExpressionCompiler.LightExpression:

Package Downloads
SecurityLogin.Redis

An security method to login

StirlingLabs.StringToExpression The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

StringToExpression allows you to create methods that take strings and outputs .NET expressions. It is highly configurable allowing you to define your own language with your own syntax.

Faster.Ioc

Package Description

SecurityLogin.Cache

An security method to login

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on FastExpressionCompiler.LightExpression:

Repository Stars
daleao/modular-overhaul
Version Downloads Last updated
4.1.0 430 1/20/2024
4.0.1 2,904 11/23/2023
4.0.0 144 11/12/2023
3.4.0-preview-01 155 8/19/2023
3.3.4 4,815 1/17/2023
3.3.3 10,330 7/24/2022
3.3.2 929 5/27/2022
3.3.1 483 5/25/2022
3.3.0 43,974 4/26/2022
3.2.2 8,068 2/2/2022
3.2.1 556 7/21/2021
3.2.0 632 6/14/2021
3.1.0 455 5/3/2021
3.0.5 377 4/21/2021
3.0.4 370 4/6/2021
3.0.3 423 4/1/2021
3.0.2 416 3/30/2021
3.0.1 423 3/27/2021
3.0.0 10,279 3/17/2021
3.0.0-preview-07 377 12/25/2020
3.0.0-preview-06 258 12/1/2020
3.0.0-preview-05 867 11/27/2020
3.0.0-preview-04 324 11/3/2020
3.0.0-preview-03 303 11/2/2020
3.0.0-preview-02 593 10/23/2020
3.0.0-preview-01 323 10/23/2020
2.0.0 11,251 1/25/2019
2.0.0-preview-03 593 11/9/2018
2.0.0-preview-02 617 10/25/2018
2.0.0-preview-01 605 10/24/2018

## 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