Repository navigation
Description
Description
Since .NET 10, Native AOT folds identical generic method bodies by default (--methodbodyfolding:generic when Optimize=true, #117411).
- With optimizations on, each
asyncmethod has its own struct state machine, so ILC emits a separate copy per async method of:AsyncStateMachineBox<TResult, TSM>.MoveNextAsyncStateMachineBox<TResult, TSM>.ExecutionContextCallbackAsyncMethodBuilderCore.Start<TSM>AsyncTaskMethodBuilder<TResult>.GetStateMachineBox<TSM>, and similar methods
- These copies have identical code bytes, GC info and unwind info, and differ only in relocation targets.
MethodInternKey(ObjectDataInternerin 10.0,MethodBodyDeduplicatorin main) does not hash relocations, so all copies of a method family get the same hash code. The generics-only check (same typical method definition) passes too.- Every
HashSet.TryGetValuethen runsMethodInternComparer.AreSameagainst all earlier copies, which fails only at the differing relocation. - This is O(n²) per method family, repeated for every pass of the fixed-point loop in
EnsureMap, and it all runs single-threaded insideObjectWriter.EmitObject.
In our iOS app (~12,000 async methods), ObjectDataInterner.EnsureMap takes ~886 s of the ~1200 s ILC run. With IlcFoldIdenticalMethodBodies=false, ILC takes 252 s.
Reproduction steps
Self-contained sample: https://github.com/andrew-kulikov/runtime-samples/tree/master/nativeaot-async-folding
git clone https://github.com/andrew-kulikov/runtime-samples
cd runtime-samples/nativeaot-async-folding
python3 ./generate_async.py -n 8000 # 8000 x `async Task<int> M(int x) { await Task.Yield(); return x + i; }`
dotnet publish AsyncFolding -c Release # ~270 s
dotnet publish AsyncFolding -c Release -p:IlcFoldIdenticalMethodBodies=false # ~15 spython3 ./measure.py 2000 4000 8000 runs both variants for several sizes.
Whole dotnet publish wall time, M1 Pro, SDK 10.0.401 (ILCompiler 10.0.12):
| async methods | folding on (default) | folding off |
|---|---|---|
| 2000 | 13 s | 7 s |
| 4000 | 47 s | 9 s |
| 8000 | 271 s | 15 s |
The binary size is the same in both cases: the async copies aren't foldable, they only collide in the hash.
Expected behavior
Enabling method body folding should not make compilation time grow quadratically with the number of async methods.
Actual behavior
Compilation time with folding grows quadratically. At a few thousand async methods it dominates the whole build.
Regression?
Yes, in practice: before .NET 10, folding was opt-in for these builds (#117411).
Known workarounds
<IlcFoldIdenticalMethodBodies>false</IlcFoldIdenticalMethodBodies>. Loses the size gains of folding elsewhere (~+11 MB in our app).<Optimize>false</Optimize>for the assemblies that contain most of the async methods.- Without optimizations, the C# compiler emits class state machines instead of struct ones, so all async methods share the canonical
AsyncStateMachineBox<TResult, __Canon>code and there are no per-method copies to deduplicate. - In our app, the configuration that compiles our libraries without optimization spends ~16 s in
EnsureMapinstead of ~886 s. - The cost is unoptimized IL in those assemblies and an extra heap allocation per async call.
- Without optimizations, the C# compiler emits class state machines instead of struct ones, so all async methods share the canonical
Runtime async
Runtime async should mostly make the async case go away, since there are no per-method state machine structs. But:
- it isn't on by default for user code in .NET 11;
- NuGet libraries targeting older TFMs keep the struct state machines;
- it isn't enabled for Apple mobile/Android (Enable runtime-async for shared framework source projects in net11.0+ and remove RuntimeAsync config knob #125406);
- any generic code instantiated over many structs whose bodies only differ in relocations would hit the same buckets.
Proposed fix
Hash relocations in MethodInternKey consistently with MethodInternComparer.AreSame:
- reloc type and offset;
- for a
MethodReadOnlyDataNodetarget, its contents; - otherwise, the identity of the target after mapping it through the previous pass's remapping.
With that change (on 10.0.13), the 8000-method sample publishes in 16 s and our app's ILC run takes ~250 s. The object files are byte-identical to the unpatched compiler. PR: #135943
Configuration
- .NET SDK 10.0.401, ILCompiler 10.0.12 and 10.0.13 built from source
- macOS 26 arm64, targets osx-arm64 and ios-arm64
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 16, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
[NativeAOT] Hash relocations in method body folding keys (#135943)