runtime icon indicating copy to clipboard operation
runtime copied to clipboard

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

Open lukemurray opened this issue 7 months ago • 22 comments

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?

lukemurray avatar Apr 21 '26 05:04 lukemurray

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

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.

lukemurray avatar Apr 21 '26 05:04 lukemurray

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?

lukemurray avatar Apr 21 '26 05:04 lukemurray

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

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

lukemurray avatar Apr 21 '26 12:04 lukemurray

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?

CodingCossack avatar Sep 19 '26 11:09 CodingCossack

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

AndyAyersMS avatar Sep 21 '26 15:09 AndyAyersMS

sorry for the extra work if it is :)

CodingCossack avatar Sep 22 '26 08:09 CodingCossack

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

Setting inline budget to 5 kind of fixes the issue for us (10 which I belive was the old default didn't)

Stumbling upon issues like this makes you think that not a lot of people bother to update their dotnet versions...

We suffer a 10seconds->2 minutes startup regression because of this, can it be atleast at 11 milestone?

deryaza avatar Sep 28 '26 05:09 deryaza

@EgorBo @JulieLeeMSFT another case: a ~18 700-test MSTest suite went from 5 min on .NET 9 to 18 min on 10.0.12, and back to 4 min 42 s with DOTNET_TieredPGO=0.

Same shape as the reports above: an in-memory IQueryable test stub compiles a fresh lambda into a DynamicMethod per query, and that 50-byte-IL lambda grows from 202 to 7 135 bytes of machine code on net10.

Minimal repro with CI numbers, ~8.5x on a 4-vCPU ubuntu-latest runner and independent of core count: https://github.com/korchak-aleksandr/net10-regression-repro/tree/main/linq-stub-pgo. CI run link: https://github.com/korchak-aleksandr/net10-regression-repro/actions/runs/36846943066

Since the only workaround we have is a runtime-wide switch, could this be considered for 10.0.x servicing ?

korchak-aleksandr avatar Oct 02 '26 13:10 korchak-aleksandr

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

EgorBo avatar Oct 02 '26 14:10 EgorBo

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

korchak-aleksandr avatar Oct 15 '26 19:10 korchak-aleksandr

@korchak-aleksandr thanks for the reminder. I am thinking of https://github.com/dotnet/runtime/pull/135893 as the fix for this problem.

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

EgorBo avatar Oct 15 '26 20:10 EgorBo

@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?

EgorBo avatar Oct 20 '26 13:10 EgorBo

@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?

korchak-aleksandr avatar Oct 20 '26 14:10 korchak-aleksandr

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

EgorBo avatar Oct 20 '26 18:10 EgorBo

agree as 10 is LTS that would be ideal.

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

lukemurray avatar Oct 20 '26 22:10 lukemurray

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

korchak-aleksandr avatar Oct 26 '26 19:10 korchak-aleksandr

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

@korchak-aleksandr sure, feel free to submit.

EgorBo avatar Oct 26 '26 19:10 EgorBo