Compile Linq Expressions to increase performance

Compile Linq Expressions to increase performance

Expressions are now an absolute part of a developer's everyday life in .NET thanks to Linq.

However, due to their nature, expressions are not one of the very best performing tools, which is why the .NET team is also doing a lot to improve general performance while maintaining usability.

Performance Improvements in .NET 7

In simpler words, expressions are a markup language for how the runtime should traverse a "tree" to get properties or perform comparisons. This traversal takes time. And the more often you do it, the more time it takes.

However, expressions can also be compiled so that the traversal only has to be done once and the runtime otherwise has a plan of what to do. Very similar to how database statements (plans) are also cached.
Afterwards you only have to use the plan and save the overhead - and it is really big!

Unfortunately, it is not currently possible to shift the expression entirely to compile-time, but at least the overhead can be shifted to a one-time operation. Not to mention that compiling itself also takes time.

Sample Code

As an example for my benchmark I have chosen a very simple expression, so that you can see that even with the simplest expressions the effect is present. However, the more complex the expression, the higher the effect!

So we see an expression that only performs an Int-based comparison to get an element.

    _persons.Where( person => person.Age == 23).Single();

To perform a compilation the expression must first be taken out of the operation itself, for example as a static field.

    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;

This expression alone can actually be used.

    public Person Ex() => _persons.Where(s_ageExpression).Single();

However, we wanted to compile our expression, for which we can again use a static field.

    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
    private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();

Benchmark

So, as a result, we compare two things:

  • A Where-query with a simple expression, and one with a compiled expression
    private static readonly Expression<Func<Person, bool>> s_ageExpression = e => e.Age == 23;
    [Benchmark]
    public Person Ex() => _persons.Where(s_ageExpression).Single();

    // Compiled
    private static readonly Func<Person, bool> s_ageExpressionCompiled = s_ageExpression.Compile();
    [Benchmark(Baseline = true)]
    public Person Ex_Compiled() => _persons.Where(s_ageExpressionCompiled).Single();

The result is very clear: the compiled query is over 3000 more performant.

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2006/21H2/November2021Update)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK=7.0.100-rc.1.22431.12
  [Host]     : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
  DefaultJob : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2


|      Method |          Mean |        Error |       StdDev |    Ratio |
|------------ |--------------:|-------------:|-------------:|---------:|
|          Ex | 228,941.16 ns | 4,508.268 ns | 4,823.797 ns | 3,464.82 |
| Ex_Compiled |      66.10 ns |     1.258 ns |     1.346 ns |     1.00 |

Full Sample and Results: Benjamin Abt - Sustainable Code - Compiled Expressions

Conclusion:

It can be very worthwhile to compile certain expressions, the effect and performance improvements are gigantic.