Skip to content

Use CompileFast() instead of Compile() in ExpressionFactoryUtilities #34

@illyaz

Description

@illyaz

Description

Replace LambdaExpression.Compile() with CompileFast() from FastExpressionCompiler in ExpressionFactoryUtilities

Why This is Needed

CreatePropertyWriter compiles property accessor expressions on every converter registration. With Remora.Discord registering a large number of converters at startup, the cumulative cost is significant and regresses further on .NET 10.

Benchmark results (source):

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7840/25H2/2025Update/HudsonValley2)
Intel Core i9-14900K 3.20GHz, 1 CPU, 32 logical and 24 physical cores                                                                                                                                      
.NET SDK 10.0.103                                                                                                                                                                                          
  [Host]    : .NET 10.0.3 (10.0.3, 10.0.326.7603), X64 RyuJIT x86-64-v3                                                                                                                                    
  .NET 10.0 : .NET 10.0.3 (10.0.3, 10.0.326.7603), X64 RyuJIT x86-64-v3                                                                                                                                    
  .NET 9.0  : .NET 9.0.13 (9.0.13, 9.0.1326.6317), X64 RyuJIT x86-64-v3                                                                                                                                    
                                                                                                                                                                                                           

| Method       | Job       | Runtime   | Mean         | Error     | StdDev    | Ratio  | RatioSD |
|------------- |---------- |---------- |-------------:|----------:|----------:|-------:|--------:|
| Original     | .NET 10.0 | .NET 10.0 | 1,415.535 us | 7.8831 us | 6.5828 us | 402.69 |    8.05 |                                                                                                         
| FastCompiler | .NET 10.0 | .NET 10.0 |     3.517 us | 0.0534 us | 0.0712 us |   1.00 |    0.03 |
|              |           |           |              |           |           |        |         |
| Original     | .NET 9.0  | .NET 9.0  |   290.462 us | 3.4586 us | 3.2351 us |  72.12 |    1.55 |
| FastCompiler | .NET 9.0  | .NET 9.0  |     4.029 us | 0.0786 us | 0.0772 us |   1.00 |    0.03 |

Alternatives Considered

Additional Details

  • FastExpressionCompiler works directly on standard System.Linq.Expressions
  • No public API changes whatsoever
  • Happy to submit a PR if you're open to the dependency

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions