You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LambdaExpression.Compile() is >10x slower on .NET 10 for an EntityGraphQL-generated projection tree; Compile(preferInterpretation: true) is unaffected #127203
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.
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:
Parses a small GraphQL query.
Builds the exact projection LambdaExpression EntityGraphQL would execute.
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?
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?
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?
@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.
@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 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?
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 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.
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.
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.
Activity
dotnet-policy-service commentedon Apr 21, 2026
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.
lukemurray commentedon Apr 21, 2026
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 commentedon Apr 21, 2026
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
.NET 10.0.6
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
.NET 10.0.6
So for this particular queryable-shaped lambda:
dotnet-policy-service commentedon Apr 21, 2026
Tagging subscribers to this area: @phil-allen-msft, @333fred
See info in area-owners.md if you want to be subscribed.
lukemurray commentedon Apr 21, 2026
Adding to this, as from this discovery I learnt about the
preferInterpretationflag. It is unclear when one should use that (truevsfalse).CodingCossack commentedon Sep 19, 2026
Good afternoon Sir @lukemurray
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?
AndyAyersMS commentedon Sep 21, 2026
Seems like it might be JIT related, taking a look.
CodingCossack commentedon Sep 22, 2026
sorry for the extra work if it is :)
9 remaining items
EgorBo commentedon Oct 2, 2026
@korchak-aleksandr thanks for the heads up, I'm investigating the issue
korchak-aleksandr commentedon Oct 15, 2026
@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:
DOTNET_TieredPGO=0brought it back to .NET 9 levels.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.
EgorBo commentedon Oct 15, 2026
@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.
EgorBo commentedon Oct 20, 2026
@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?
JIT: Gate single-edge root profile trust on hotness or speed preferen…
korchak-aleksandr commentedon Oct 20, 2026
@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?
EgorBo commentedon Oct 20, 2026
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
lukemurray commentedon Oct 20, 2026
agree as 10 is LTS that would be ideal.
But I'm also happy it it gets fixed at all.
korchak-aleksandr commentedon Oct 26, 2026
@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.
EgorBo commentedon Oct 26, 2026
@korchak-aleksandr sure, feel free to submit.
[release/11.0] JIT: Gate single-edge root profile trust on hotness or…
Add LambdaExpression.Compile benchmarks (#5341)
Thorium commentedon Nov 17, 2026
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.
deryaza commentedon Nov 17, 2026
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.