Skip to content

[release/11.0] [NativeAOT] Hash relocations in method body folding keys - #136016

Merged
agocke merged 1 commit into
release/11.0from
backport/pr-135943-to-release/11.0
Nov 10, 2026
Merged

agocke merged 1 commit into
release/11.0from
backport/pr-135943-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #135943 to release/11.0

/cc @agocke @andrew-kulikov

Customer Impact

  • Customer reported
  • Found internally

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

  • Yes
  • No

Testing

Verified perf improvement on multiple large apps (e.g. Azure MCP).

Risk

Low, only changing hash code

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky

Copy link
Copy Markdown
Member

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.

@andrew-kulikov

Copy link
Copy Markdown
Contributor

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.

@agocke agocke added the Servicing-approved Approved for servicing release label Nov 4, 2026
@agocke
agocke requested a balanced review from Copilot November 4, 2026 20:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@agocke

agocke commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

@andrew-kulikov This was approved for backport. It won't be in the initial release, but it will be in servicing.

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code change LGTM. I assume shiproom approved knowing that this is not a .NET 11 regression.

@agocke
agocke merged commit ba608b4 into release/11.0 Nov 10, 2026
115 of 117 checks passed
@agocke
agocke deleted the backport/pr-135943-to-release/11.0 branch November 10, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants