Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fast Compiler for C# Expression Trees and the lightweight LightExpression alternative. Diagnostic and code generation tools for the expressions.

License

NotificationsYou must be signed in to change notification settings

dadhi/FastExpressionCompiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

Latest Release NotesLicenseBuild Windows,Ubuntu

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

NuGet packages:

  • FastExpressionCompilerNuGet VersionNuGet Downloads
    • sources package: FastExpressionCompiler.srcNuGet VersionNuGet Downloads
    • sources with the public code made internal: FastExpressionCompiler.Internal.srcNuGet VersionNuGet Downloads
  • FastExpressionCompiler.LightExpressionNuGet VersionNuGet Downloads
    • sources package: FastExpressionCompiler.LightExpression.srcNuGet VersionNuGet Downloads
    • sources with the public code made internal: FastExpressionCompiler.LightExpression.Internal.srcNuGet VersionNuGet Downloads

The project was originally a part of theDryIoc, 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.ButExpression.Compile() is just slow.Moreover the compiled delegate may be slower than the manually created delegate because of thereasons:

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 alsoa deep dive to Delegate internals.

The solution

The FastExpressionCompiler.CompileFast() extension method is10-40x times faster than.Compile().
The compiled delegate may bein 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 theAllocated column in thebenchmarks below).

Benchmarks

Updated to .NET 9.0

BenchmarkDotNet v0.15.0, Windows 11 (10.0.26100.4061/24H2/2024Update/HudsonValley)Intel Core i9-8950HK CPU 2.90GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores.NET SDK 9.0.203[Host]     : .NET 9.0.4 (9.0.425.16305), X64 RyuJIT AVX2DefaultJob : .NET 9.0.4 (9.0.425.16305), X64 RyuJIT AVX2

Hoisted expression with the constructor and two arguments in closure

vara=newA();varb=newB();Expression<Func<X>>e=()=>newX(a,b);

Compiling expression:

MethodMeanErrorStdDevRatioRatioSDRankGen0Gen1AllocatedAlloc Ratio
CompileFast3.183 us0.0459 us0.0407 us1.000.0210.19840.19451.23 KB1.00
Compile147.312 us1.9291 us1.8946 us46.280.8120.48830.24414.48 KB3.65

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

MethodMeanErrorStdDevRatioRatioSDRankGen0AllocatedAlloc Ratio
DirectConstructorCall6.055 ns0.0632 ns0.0560 ns1.000.0110.005132 B1.00
CompiledLambda7.853 ns0.2013 ns0.1681 ns1.300.0320.005132 B1.00
FastCompiledLambda7.962 ns0.2186 ns0.4052 ns1.310.0720.005132 B1.00

Hoisted expression with the static method and two nested lambdas and two arguments in closure

vara=newA();varb=newB();Expression<Func<X>>getXExpr=()=>CreateX((aa,bb)=>newX(aa,bb),newLazy<A>(()=>a),b);

Compiling expression:

MethodMeanErrorStdDevRatioRatioSDRankGen0Gen1AllocatedAlloc Ratio
CompileFast11.12 us0.189 us0.158 us1.000.0210.61040.57983.77 KB1.00
Compile415.09 us4.277 us3.571 us37.340.6021.95311.464812.04 KB3.19

Invoking compiled delegate comparing to direct method call:

MethodMeanErrorStdDevRatioRatioSDRankGen0AllocatedAlloc Ratio
DirectMethodCall40.29 ns0.549 ns0.487 ns1.000.0210.0268168 B1.00
Invoke_CompiledFast40.59 ns0.157 ns0.123 ns1.010.0110.0166104 B0.62
Invoke_Compiled1,142.12 ns11.877 ns14.586 ns28.350.4820.0420264 B1.57

Manually composed expression with parameters and closure

vara=newA();varbParamExpr=Expression.Parameter(typeof(B),"b");varexpr=Expression.Lambda(Expression.New(_ctorX,Expression.Constant(a,typeof(A)),bParamExpr),bParamExpr);

Compiling expression:

MethodMeanErrorStdDevRatioRatioSDRankGen0Gen1AllocatedAlloc Ratio
CompileFast_LightExpression3.107 us0.0562 us0.0498 us0.990.0210.17550.16781.08 KB1.00
CompileFast_SystemExpression3.126 us0.0288 us0.0256 us1.000.0110.17550.16781.08 KB1.00
Compile_SystemExpression103.948 us1.9593 us2.5477 us33.260.8420.73240.48834.74 KB4.40

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

MethodMeanErrorStdDevRatioRankGen0AllocatedAlloc Ratio
DirectCall10.19 ns0.108 ns0.085 ns1.0010.005132 B1.00
CompiledFast_LightExpression10.70 ns0.089 ns0.070 ns1.0520.005132 B1.00
CompiledFast_SystemExpression10.91 ns0.071 ns0.066 ns1.0720.005132 B1.00
Compiled_SystemExpression11.59 ns0.098 ns0.081 ns1.1430.005132 B1.00

FastExpressionCompiler.LightExpression.Expression vs System.Linq.Expressions.Expression

FastExpressionCompiler.LightExpression.Expression is the lightweight version ofSystem.Linq.Expressions.Expression.It is designed to be adrop-in replacement for the System Expression - just install theFastExpressionCompiler.LightExpression package instead ofFastExpressionCompiler and replace the usings

usingSystem.Linq.Expressions;usingstaticSystem.Linq.Expressions.Expression;

with

usingstaticFastExpressionCompiler.LightExpression.Expression;namespaceFastExpressionCompiler.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).Itwon't validate operations compatibility for the tree the waySystem.Linq.Expression does it, and partially why it is so slow.Hopefully you are checking the expression arguments yourself and not waiting for theExpression exceptions to blow-up.

Sample expression

Creating the expression:

MethodMeanErrorStdDevMedianRatioRatioSDRankGen0AllocatedAlloc Ratio
Create_LightExpression156.6 ns3.19 ns8.18 ns151.9 ns1.000.0710.0827520 B1.00
Create_SystemExpression1,065.0 ns14.24 ns11.89 ns1,069.3 ns6.820.3420.20601304 B2.51

Creating and compiling:

MethodMeanErrorStdDevMedianRatioRatioSDRankGen0Gen1AllocatedAlloc Ratio
Create_LightExpression_and_CompileFast4.957 us0.0986 us0.2362 us4.913 us1.000.0710.35100.30522.15 KB1.00
Create_SystemExpression_and_CompileFast6.518 us0.1889 us0.5541 us6.300 us1.320.1320.45780.42722.97 KB1.38
Create_SystemExpression_and_Compile205.000 us4.0938 us7.3819 us206.353 us41.442.4530.97660.48837.15 KB3.33

Difference between FastExpressionCompiler and FastExpressionCompiler.LightExpression

FastExpressionCompiler

  • Provides theCompileFast extension methods for theSystem.Linq.Expressions.LambdaExpression.

FastExpressionCompiler.LightExpression

  • Provides theCompileFast 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 ownExpressionVisitor.
  • SupportsToExpression method to convert backto theSystem.Linq.Expressions.Expression.
  • SupportsToLightExpression conversion method to convertfrom theSystem.Linq.Expressions.Expression toFastExpressionCompiler.LightExpression.Expression.

Both FastExpressionCompiler and FastExpressionCompiler.LightExpression

  • SupportToCSharpString() method to output the compilable C# code represented by the expression.
  • SupportToExpressionString() 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 theusing FastExpressionCompiler; and replace the call to the.Compile() with the.CompileFast() extension method.

Note:CompileFast has an optional parameterbool ifFastFailedReturnNull = false to disable fallback toCompile.

Examples

Hoisted lambda expression (created by the C# Compiler):

vara=newA();varb=newB();Expression<Func<X>>expr=()=>newX(a,b);vargetX=expr.CompileFast();varx=getX();

Manually composed lambda expression:

vara=newA();varbParamExpr=Expression.Parameter(typeof(B),"b");varexpr=Expression.Lambda(Expression.New(_ctorX,Expression.Constant(a,typeof(A)),bParamExpr),bParamExpr);varf=expr.CompileFast();varx=f(newB());

Note: You may simplify Expression usage and enable faster refactoring with the C#using static statement:

usingstaticSystem.Linq.Expressions.Expression;// or// using static FastExpressionCompiler.LightExpression.Expression;vara=newA();varbParamExpr=Parameter(typeof(B),"b");varexpr=Lambda(New(_ctorX,Constant(a,typeof(A)),bParamExpr),bParamExpr);varf=expr.CompileFast();varx=f(newB());

How it works

The idea is to provide the fast compilation for the supported expression typesand fallback to the systemExpression.Compile() for the not supported types:

What's not supported yet

FEC 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 theDiagnostics 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 theDynamicMethod.

If visitor finds the not supported expression node or the error condition,the compilation is aborted, andnull 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 optionalCompilerFlags.EnableDelegateDebugInfo into theCompileFast methods.

EnableDelegateDebugInfo adds the diagnostic info into the compiled delegate including its source Expression and compiled IL code.

It can be used as following:

System.Linq.Expressions.Expression<Func<int,Func<int>>>e=   n=>()=>n+1;varf=e.CompileFast(flags:CompilerFlags.EnableDelegateDebugInfo);vard=f.TryGetDebugInfo();d.PrintExpression();d.PrintCSharp();d.PrintIL();// available in NET8+
Expand to see the output of the above code...

Output ofd.PrintExpression() is the valid C#:

varp=newParameterExpression[1];// the parameter expressionsvare=newExpression[3];// the unique expressionsvarexpr=Lambda<Func<int,Func<int>>>(e[0]=Lambda<Func<int>>(e[1]=MakeBinary(ExpressionType.Add,p[0]=Parameter(typeof(int),"n"),e[2]=Constant(1)),newParameterExpression[0]),p[0// (int n)]);

Output ofd.PrintCSharp() is the valid C#:

var@cs=(Func<int,Func<int>>)((intn)=>//Func<int>(Func<int>)(()=>//intn+1));

Output ofd.PrintIL() (includes the IL of the nested lambda):

<Caller>0   ldarg.01   ldfld object[] ExpressionCompiler.ArrayClosure.ConstantsAndNestedLambdas6   stloc.07   ldloc.08   ldc.i4.09   ldelem.ref10  stloc.111  ldloc.112  ldc.i4.113  newarr object18  stloc.219  ldloc.220  stfld object[] ExpressionCompiler.NestedLambdaForNonPassedParams.NonPassedParams25  ldloc.226  ldc.i4.027  ldarg.128  box int33  stelem.ref34  ldloc.135  ldfld object ExpressionCompiler.NestedLambdaForNonPassedParams.NestedLambda40  ldloc.241  ldloc.142  ldfld object[] ExpressionCompiler.NestedLambdaForNonPassedParamsWithConstants.ConstantsAndNestedLambdas47  newobj ExpressionCompiler.ArrayClosureWithNonPassedParams(System.Object[], System.Object[])52  call Func<int> ExpressionCompiler.CurryClosureFuncs.Curry(System.Func`2[FastExpressionCompiler.LightExpression.ExpressionCompiler+ArrayClosure,System.Int32], ArrayClosure)57  ret</Caller><0_nested_in_Caller>0   ldarg.01   ldfld object[] ExpressionCompiler.ArrayClosureWithNonPassedParams.NonPassedParams6   ldc.i4.07   ldelem.ref8   unbox.any int13  ldc.i4.114  add15  ret</0_nested_in_Caller>

ThrowOnNotSupportedExpression and NotSupported_ flags

FEC V3.1 has added the compiler flagCompilerFlags.ThrowOnNotSupportedExpression.When passed toCompileFast(flags: CompilerFlags.ThrowOnNotSupportedExpression) and the expression contains not (yet) supported Expression node the compilation will throw the exception instead of returningnull.

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

Code Generation

The Code Generation capabilities are available via theToCSharpString andToExpressionString extension methods.

Note: When converting the source expression to either C# code or to the Expression construction code you may findthe// 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. UsingFastExpressionCompiler.LightExpression.Expression instead ofSystem.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 theBlock,Try, orGoto expressions.

Bitten Ice Pop icon icon byIcons8

About

Fast Compiler for C# Expression Trees and the lightweight LightExpression alternative. Diagnostic and code generation tools for the expressions.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors24

Languages


[8]ページ先頭

©2009-2025 Movatter.jp