Repository navigation
[release/11.0] [NativeAOT] Hash relocations in method body folding keys - #136016
Conversation
Fixes #135942 ### Problem - `MethodInternKey` hashes code bytes, GC info, frame infos and EH bytes, but not relocations, so bodies that differ only in their relocation targets always share a hash code. - With optimized code this is very common: each async method has its own struct state machine, so every async method gets its own byte-identical copy of `AsyncStateMachineBox<TResult, TSM>.MoveNext`, `AsyncMethodBuilderCore.Start<TSM>`, etc. - All of those land in one `HashSet` bucket, and each insertion runs `AreSame` against every earlier copy, so deduplication is O(n²) per method family, per pass. ### Change Relocations are mixed into the hash, consistently with `MethodInternComparer.AreSame`: - **Reloc type and offset:** compared exactly by `AreSame`. - **`MethodReadOnlyDataNode` targets:** their data bytes. `AreSame` compares them by content, and the hash covers a subset of what is compared. - **Other targets:** the identity of the target after mapping it through the previous pass's remapping. - `AreSame` treats `t1` and `t2` as equal when `remapping[t1] == t2` (or vice versa). - Remapping values are never keys: a body only becomes a key when it matched a body already in the set, and bodies in the set are never keys. So both sides hash `id(t2)`. Why the output does not change: - The set of pairs `Equals` accepts is unchanged; only the bucket distribution changes. - The iteration order is still the sorted `GetCompiledMethodBodies()`. - `ISymbolNode`s already hash by identity. ### Results Measured with the same change on v10.0.13 (osx-arm64 host, M1 Pro): | | before | after | |---|---:|---:| | iOS app, ~12,000 async methods, ILC wall time | ~1200 s | 247-252 s | | same, `--methodbodyfolding:none` | 252 s | | | sample with 8000 `async Task<int>` methods, `dotnet publish` | 271 s | 16 s | | sample, 4000 methods | 47 s | 9 s | - The object files are byte-identical to the unpatched compiler in all cases: the iOS app's 1.19 GB `.o` (two runs) and the sample's `.o`. - `__managedcode` keeps the folding savings (69.3 MB vs 80.0 MB without folding). Repro: https://github.com/andrew-kulikov/runtime-samples/tree/master/nativeaot-async-folding
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
|
Would this meet the backport bar for 11 at this point? I could maybe see a way to fit it if this was a perf regression in 11, but we've had it like this since 10. |
|
If this can't make it into the initial .NET 11 release, could it be considered for the first servicing update (11.0.1)? We're planning to upgrade to .NET 11, and this would significantly improve our NativeAOT build times without requiring workarounds. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported, although builds and tests were not run.
No open issues 🎉
What changed in this PR
Backports #135943 to release/11.0 to reduce NativeAOT compilation time for generic-heavy applications by improving method-body folding hash distribution.
Changes:
- Includes relocation types, offsets, and targets in method-body hashes.
- Hashes read-only data targets by content and other targets by identity after applying the previous pass’s symbol remapping.
Builds and tests were not run.
| File | Description |
|---|---|
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MethodBodyDeduplicator.cs |
Adds relocation-aware hashing to reduce collisions during method-body deduplication. |
Review effort: Balanced
|
@andrew-kulikov This was approved for backport. It won't be in the initial release, but it will be in servicing. |
MichalStrehovsky
left a comment
There was a problem hiding this comment.
Code change LGTM. I assume shiproom approved knowing that this is not a .NET 11 regression.
Backport of #135943 to release/11.0
/cc @agocke @andrew-kulikov
Customer Impact
Quite simply, this is a bug around the hash code implementation that can cause massive compile time increases in code with lots of generics. Fixing this bug can reduce compile times at a level that becomes qualitative, not just quantitative -- 5 minutes instead of 30 minutes.
Regression
Testing
Verified perf improvement on multiple large apps (e.g. Azure MCP).
Risk
Low, only changing hash code