All Projects → dadhi → Fastexpressioncompiler

dadhi / Fastexpressioncompiler

Licence: mit
Fast ExpressionTree compiler to delegate

Projects that are alternatives of or similar to Fastexpressioncompiler

Adlik
Adlik: Toolkit for Accelerating Deep Learning Inference
Stars: ✭ 237 (-62.44%)
Mutual labels:  compiler, performance
Web Frameworks
Which is the fastest web framework?
Stars: ✭ 6,125 (+870.68%)
Mutual labels:  performance, benchmark
Deepc
vendor independent deep learning library, compiler and inference framework microcomputers and micro-controllers
Stars: ✭ 260 (-58.8%)
Mutual labels:  compiler, performance
Ngraph
nGraph has moved to OpenVINO
Stars: ✭ 1,322 (+109.51%)
Mutual labels:  compiler, performance
Nuitka
Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, and 3.9. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
Stars: ✭ 6,173 (+878.29%)
Mutual labels:  compiler, performance
Rubyspeed
Compile ruby functions to C
Stars: ✭ 180 (-71.47%)
Mutual labels:  compiler, performance
Web Tooling Benchmark
JavaScript benchmark for common web developer workloads
Stars: ✭ 290 (-54.04%)
Mutual labels:  performance, benchmark
Kubestone
Performance benchmarks for Kubernetes
Stars: ✭ 159 (-74.8%)
Mutual labels:  performance, benchmark
Pyperformance
Python Performance Benchmark Suite
Stars: ✭ 406 (-35.66%)
Mutual labels:  performance, benchmark
Ratel Core
High performance JavaScript to JavaScript compiler with a Rust core
Stars: ✭ 367 (-41.84%)
Mutual labels:  compiler, performance
Delta
Programming language focused on performance and productivity
Stars: ✭ 77 (-87.8%)
Mutual labels:  compiler, performance
Nimporter
Compile Nim Extensions for Python On Import!
Stars: ✭ 474 (-24.88%)
Mutual labels:  compiler, performance
Tvm
Open deep learning compiler stack for cpu, gpu and specialized accelerators
Stars: ✭ 7,494 (+1087.64%)
Mutual labels:  compiler, performance
Vbuild
"Compile" your VueJS components (sfc/*.vue) to standalone html/js/css ... python only (no need of nodejs). Support python components too !
Stars: ✭ 236 (-62.6%)
Mutual labels:  compiler, closure
Are We Fast Yet
Are We Fast Yet? Comparing Language Implementations with Objects, Closures, and Arrays
Stars: ✭ 161 (-74.48%)
Mutual labels:  performance, benchmark
Clang
Mirror kept for legacy. Moved to https://github.com/llvm/llvm-project
Stars: ✭ 2,880 (+356.42%)
Mutual labels:  compiler, performance
Gatling Dubbo
A gatling plugin for running load tests on Apache Dubbo(https://github.com/apache/incubator-dubbo) and other java ecosystem.
Stars: ✭ 131 (-79.24%)
Mutual labels:  performance, benchmark
Sltbench
C++ benchmark tool. Practical, stable and fast performance testing framework.
Stars: ✭ 137 (-78.29%)
Mutual labels:  performance, benchmark
Fperf
Framework of performance testing
Stars: ✭ 316 (-49.92%)
Mutual labels:  performance, benchmark
Halide
a language for fast, portable data-parallel computation
Stars: ✭ 4,722 (+648.34%)
Mutual labels:  compiler, performance

FastExpressionCompiler

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

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 faster construction and less memory at the cost of 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, MapsterMapper

Considering: Moq, LINQ to DB, 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());

Benchmarks

Updated to .NET 5

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19041.630 (2004/?/20H1)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT
  DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), 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 274.722 us 5.3167 us 5.6888 us 47.47 1.67 0.9766 0.4883 - 4.52 KB
CompileFast 5.790 us 0.1118 us 0.1197 us 1.00 0.00 0.3815 0.1907 0.0305 1.57 KB

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

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
DirectConstructorCall 7.634 ns 0.2462 ns 0.2303 ns 0.54 0.02 0.0076 - - 32 B
CompiledLambda 15.553 ns 0.1805 ns 0.1600 ns 1.09 0.02 0.0076 - - 32 B
FastCompiledLambda 14.241 ns 0.2844 ns 0.2521 ns 1.00 0.00 0.0076 - - 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 479.87 us 5.039 us 4.208 us 31.98 0.59 2.9297 1.4648 - 12.17 KB
CompileFast 15.00 us 0.291 us 0.298 us 1.00 0.00 1.1902 0.5493 0.0916 4.86 KB

Invoking compiled delegate comparing to direct method call:

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
DirectMethodCall 53.24 ns 0.721 ns 0.674 ns 1.06 0.02 0.0401 - - 168 B
Invoke_Compiled 1,486.71 ns 13.620 ns 12.741 ns 29.64 0.25 0.0629 - - 264 B
Invoke_CompiledFast 50.20 ns 0.484 ns 0.404 ns 1.00 0.00 0.0248 - - 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 148.633 us 1.4863 us 1.2411 us 33.20 1.05 0.9766 0.4883 - 4.78 KB
CompileFast 4.498 us 0.0887 us 0.1022 us 1.02 0.04 0.3510 0.1755 0.0305 1.46 KB
CompileFast_LightExpression 4.365 us 0.0860 us 0.1364 us 1.00 0.00 0.3433 0.1678 0.0305 1.42 KB

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

Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
DirectLambdaCall 11.86 ns 0.140 ns 0.131 ns 1.00 0.0076 - - 32 B
CompiledLambda 13.44 ns 0.115 ns 0.096 ns 1.13 0.0076 - - 32 B
FastCompiledLambda 12.43 ns 0.173 ns 0.154 ns 1.05 0.0076 - - 32 B
FastCompiledLambda_LightExpression 11.87 ns 0.121 ns 0.101 ns 1.00 0.0076 - - 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 Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
CreateExpression 2,508.2 ns 44.12 ns 36.84 ns 8.83 0.14 0.3128 - - 1312 B
CreateLightExpression 284.2 ns 5.19 ns 4.85 ns 1.00 0.00 0.1316 - - 552 B

Creating and compiling:

Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
CreateExpression_and_Compile 244.61 us 4.700 us 6.111 us 17.92 0.50 1.7090 0.7324 - 7.2 KB
CreateExpression_and_CompileFast 17.69 us 0.350 us 0.443 us 1.31 0.04 1.8005 0.8850 0.0305 7.36 KB
CreateLightExpression_and_CompileFast 13.50 us 0.152 us 0.143 us 1.00 0.00 1.5869 0.7935 0.0305 6.58 KB

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.

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.

Bitten Ice Pop icon icon by Icons8

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].