Skip to content

LambdaExpression.Compile() is >10x slower on .NET 10 for an EntityGraphQL-generated projection tree; Compile(preferInterpretation: true) is unaffected #127203

Closed

Description

@lukemurray

Summary

While benchmarking EntityGraphQL query compilation, I found a regression when compiling different LambdaExpression shapes on .NET 10.0.6 compared to .NET 9.0.6.

The important part is that this is not the full GraphQL pipeline. I isolated the issue to compiling the exact LambdaExpression generated for a query projection - I hope.

On the same machine:

  • .NET 9.0.6: Compile() = 252.950 us
  • .NET 10.0.6: Compile() = 5.143 ms
  • .NET 9.0.6: Compile(preferInterpretation: true) = 2.329 us
  • .NET 10.0.6: Compile(preferInterpretation: true) = 2.046 us

So the slowdown appears to be in the non-interpreted LambdaExpression.Compile() path for this tree shape.

Environment

  • macOS Tahoe 26.4.1 (Darwin 25.4.0)
  • Apple M1 Max
  • SDK 10.0.202
  • Benchmarked runtimes:
    • .NET 9.0.6
    • .NET 10.0.6

Repro

Repository benchmark: https://github.com/EntityGraphQL/EntityGraphQL

  • Benchmark file: src/Benchmarks/GraphQLLambdaCompileBenchmarks.cs
  • Run:
dotnet run -c Release --project src/Benchmarks/Benchmarks.csproj --framework net9.0 --filter '*GraphQLLambdaCompileBenchmarks*'
dotnet run -c Release --project src/Benchmarks/Benchmarks.csproj --framework net10.0 --filter '*GraphQLLambdaCompileBenchmarks*'

This benchmark:

  1. Parses a small GraphQL query.
  2. Builds the exact projection LambdaExpression EntityGraphQL would execute.
  3. Benchmarks only:
    • lambda.Compile()
    • lambda.Compile(preferInterpretation: true)

Benchmark Output

.NET 9.0.6

| Method                     | Mean       | Allocated |
|--------------------------- |-----------:|----------:|
| Compile                    | 252.950 us |  11.87 KB |
| CompilePreferInterpretation|   2.329 us |   3.45 KB |

.NET 10.0.6

| Method                     | Mean     |
|--------------------------- |---------:|
| Compile                    | 5.143 ms |
| CompilePreferInterpretation| 2.046 us |

Notes

  • A simpler synthetic benchmark in src/Benchmarks/ExpressionCompileBenchmarks.cs does not reproduce the regression.
  • The regression seems sensitive to the exact tree shape generated by EntityGraphQL rather than expression compilation in general.
  • In EntityGraphQL, using Compile(preferInterpretation: true) for one-shot compile-and-run paths is an effective mitigation, but it would still be useful to understand what changed in the IL-emitting Compile() path between .NET 9 and .NET 10.

Question

Was there an intentional change in the non-interpreted System.Linq.Expressions compile pipeline in .NET 10 that would explain this tree-shape-specific slowdown, or does this look like a runtime regression worth investigating further?

Or have I missed something?

Activity

  1. added
    tenet-performance
    on Apr 21, 2026
  2. added
    untriaged
    on Apr 21, 2026
  3. dotnet-policy-service commented on Apr 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
    See info in area-owners.md if you want to be subscribed.

  4. lukemurray commented on Apr 21, 2026

    @lukemurray
    Author

    Feel like the bot got the area wrong? Sorry I'm going to tag @333fred for the expressions part. Apologies if that is wrong also.

  5. lukemurray commented on Apr 21, 2026

    @lukemurray
    Author

    I narrowed the repro further and found the lambda shape matters a lot.

    A simplified in-memory benchmark built over plain object properties only showed about a 2x compile slowdown on .NET 10, which suggested the original regression was shape-sensitive.

    I then adjusted the benchmark to make the root path more like the original query execution shape by using IQueryable rather than List, while still avoiding the EF/SQLite benchmark-worker issues. That pushed the generated LambdaExpression much closer to the original problematic shape.

    Updated results on the same machine:

    .NET 9.0.6

    • Compile: 245.462 us
    • Compile(preferInterpretation: true): 2.329 us

    .NET 10.0.6

    • Compile: 4,954.412 us
    • Compile(preferInterpretation: true): 1.951 us

    So this updated repro is again showing about a 20x regression in the non-interpreted Compile() path on .NET 10.

    I also measured invocation by precompiling both delegates once and benchmarking only DynamicInvoke:

    .NET 9.0.6

    • compiled delegate invoke: 865.526 us
    • interpreted delegate invoke: 939.422 us

    .NET 10.0.6

    • compiled delegate invoke: 1,112.477 us
    • interpreted delegate invoke: 1,096.740 us

    So for this particular queryable-shaped lambda:

    • compile regression is very large on .NET 10
    • preferInterpretation: true avoids the compile cliff - should this just be used all the time?
  6. dotnet-policy-service commented on Apr 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @phil-allen-msft, @333fred
    See info in area-owners.md if you want to be subscribed.

  7. lukemurray commented on Apr 21, 2026

    @lukemurray
    Author

    Adding to this, as from this discovery I learnt about the preferInterpretation flag. It is unclear when one should use that (true vs false).

  8. CodingCossack commented on Sep 19, 2026

    @CodingCossack

    Good afternoon Sir @lukemurray

    • regression still reproduces on .NET 10.0.12 and current main.
    • EntityGraphQL itself is not required. A very small expression around Queryable.Where reproduces it. same behaviour can be reproduced with a raw DynamicMethod.
    • most extra time is spent in native JIT compilation during DynamicMethod.CreateDelegate(), rather than in expression tree analysis or IL emission.
    • so this doesn't appear to come from a change in the System.Linq.Expressions compile pipeline itself. the expensive part happens when the emitted dynamic method is jit compiled.
    • the big change appears between .NET 10 preview 6 and preview 7.
    • on the small Where repro, the generated native code grows from roughly 40 bytes to around 1.5 kb+ because much more gets inlined.

    tried narrowing the single edge synthetic profile behaviour to Tier1 compilations. That fixes the compile time regression very effectively, but it doesn't look safe as a general fix.

    it also removes some cheap inlines from non-Tier1 fully optimised methods. In the small controls I tested, that caused persistent warmed regressions of roughly 28–63%, while Tier1 versions were unchanged. so I don't think I have enough evidence to propose that change as a PR.

    looked at limiting how much inline expansion is allowed for these trusted synthetic profiles. looks more promising conceptually but choosing the limit from a few local benchmarks would be guessing.

    key Q:
    would the preferred direction here be to keep the single edge trust and put some kind of limit on how much inlining it can trigger, or is dropping that trust for non-Tier1 roots an acceptable trade off?

  9. AndyAyersMS commented on Sep 21, 2026

    @AndyAyersMS
    Member

    Seems like it might be JIT related, taking a look.

  10. CodingCossack commented on Sep 22, 2026

    @CodingCossack

    sorry for the extra work if it is :)

  11. added
    area-CodeGen-coreclr
    and removed on Sep 22, 2026
  12. 9 remaining items

  13. EgorBo commented on Oct 2, 2026

    @EgorBo
    Member

    @korchak-aleksandr thanks for the heads up, I'm investigating the issue

  14. korchak-aleksandr commented on Oct 15, 2026

    @korchak-aleksandr

    @EgorBo a follow-up: for us this is not just slow CI, it hits production.

    We run a multi-tenant SaaS on .NET 10, with hundreds of tenants and each tenant on its own pods. Several of our code paths build EF Core queries or compile expressions dynamically.

    After moving from .NET 9:

    • One background operation that builds an EF query on the fly went from ~57 ms to ~1.5 s per call. The slowdown led to ~8x more database conflict retries. DOTNET_TieredPGO=0 brought it back to .NET 9 levels.
    • With PGO on, memory grows on small pods (~700 MiB limits) until they get OOM-killed.
    • With PGO off, CPU per request is ~20% higher on API pods and ~10% higher on background workers.

    So we now juggle runtime knobs per workload: PGO off for background workers and admin, PGO on for large API pods, PGO off plus GC tuning for small ones. Every runtime update means re-validating this whole matrix.

    A fix in 10.0.x servicing would let us drop this workaround matrix. Happy to share more data if that's useful.

  15. EgorBo commented on Oct 15, 2026

    @EgorBo
    Member

    @korchak-aleksandr thanks for the reminder. I am thinking of #135893 as the fix for this problem.

    The ideal fix here is still enabling TieredCompilation for dynamic methods, but we're not there yet.

  16. EgorBo commented on Oct 20, 2026

    @EgorBo
    Member

    @lukemurray @korchak-aleksandr the bar for backports to 10.0 is very high (especially for perf related improvements), is it acceptable for you to consume the fix in .NET 11.0 only?

  17. added a commit that references this issue on Oct 20, 2026
    8cff292
  18. korchak-aleksandr commented on Oct 20, 2026

    @korchak-aleksandr

    @EgorBo Thanks for the quick fix! A 10.0 backport would still be our first choice: 10 is LTS and we've just migrated to it. But if the bar can't be met, .NET 11 works for us, and we'll plan our upgrade around it.

    Since main is already .NET 12, could you confirm whether the fix will make it into 11.0 GA or ship in an 11.0.x update?

  19. EgorBo commented on Oct 20, 2026

    @EgorBo
    Member

    29. @EgorBo Thanks for the quick fix! A 10.0 backport would still be our first choice: 10 is LTS and we've just migrated to it. But if the bar can't be met, .NET 11 works for us, and we'll plan our upgrade around it.
    Since main is already .NET 12, could you confirm whether the fix will make it into 11.0 GA or ship in an 11.0.x update?

    Unfortunately, it might miss the GA as it's already locked in, so only first servicing. We'll discuss the possability of backporting it to 10.0

  20. lukemurray commented on Oct 20, 2026

    @lukemurray
    Author

    agree as 10 is LTS that would be ideal.

    But I'm also happy it it gets fixed at all.

  21. korchak-aleksandr commented on Oct 26, 2026

    @korchak-aleksandr

    @EgorBo a small follow-up to make sure this one stays fixed: as far as I can see, nothing in dotnet/performance currently covers LambdaExpression.Compile() / DynamicMethod JIT time, which is probably why this regression went unnoticed from preview 7 until 10.0.12.

    Would you accept a small benchmark there based on the LINQ-stub repro (compiling a Queryable.Where-shaped lambda), so the perf lab can catch it if it regresses again?
    I'm happy to open the PR.

  22. EgorBo commented on Oct 26, 2026

    @EgorBo
    Member

    Would you accept a small benchmark there based on the LINQ-stub repro

    @korchak-aleksandr sure, feel free to submit.

  23. Thorium commented on Nov 17, 2026

    @Thorium
    Contributor

    So, dynamic lambda expressions are broken in .NET 10 (and in .NET 11 for now). That's annoying, given Microsoft .NET's significant role in translating C# to SQL in enterprise applications. The .NET Framework 4.8 still works as always.

  24. deryaza commented on Nov 17, 2026

    @deryaza

    So, dynamic lambda expressions are broken in .NET 10 (and in .NET 11 for now). That's annoying, given Microsoft .NET's significant role in translating C# to SQL in enterprise applications. The .NET Framework 4.8 still works as always.

    This is indeed an unfortunate regression with an even more unfortunate workaround.

    We haven't observed regressions in creation of expression trees, but only in compilation (and maybe execution) tho. You might be running into a different issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions