Skip to content

Avoid boxing NativeAOT dependency enumerators - #132922

Open
awakecoding wants to merge 2 commits into
dotnet:mainfrom
awakecoding:copilot/nativeaot-dependency-iteration
Open

Avoid boxing NativeAOT dependency enumerators#132922
awakecoding wants to merge 2 commits into
dotnet:mainfrom
awakecoding:copilot/nativeaot-dependency-iteration

Conversation

@awakecoding

@awakecoding awakecoding commented Aug 29, 2026

Copy link
Copy Markdown

Summary

  • Let hot dependency producers explicitly expose their existing concrete lists to the dependency analyzer.
  • Enumerate those lists with their struct enumerators, avoiding boxed IEnumerable<T> enumeration without runtime type tests in the analyzer.
  • Preserve the existing IEnumerable<T> contract and fallback for all other nodes.

Motivation

Dependency analysis exposes dependencies as IEnumerable<T>, but hot NativeAOT nodes commonly build DependencyList or List<CombinedDependencyListEntry>. Enumerating those values through the interface boxes collection enumerators.

The original profiling experiment observed 28.536 million static concrete-list visits covering 96.861 million entries and 6.715 million conditional-list visits covering 73.188 million entries. A general prototype established the optimization ceiling: graph and mark time each improved by about 7.3 seconds, managed allocation fell by 1.35-1.39 GiB, and output was byte-identical.

Implementation

DependencyNodeCore now has internal opt-in methods for nodes that naturally own a concrete static or conditional dependency list. ObjectNode, VirtualMethodUseNode, ScannedMethodNode, MethodCodeNode, and EETypeNode implement those paths. DependencyAnalyzer asks each producer for its concrete representation and otherwise uses the unchanged enumerable fallback.

This keeps representation knowledge with the producer: the analyzer contains no collection casts or runtime type checks. The public IEnumerable<T> APIs remain unchanged for existing consumers and source-included implementations.

The concrete paths use normal List<T>.Enumerator values, retaining ordering and version checks if an OnMarked callback mutates a list during enumeration. Null, already-marked, and deferred conditional dependencies retain their previous behavior.

Validation

  • .\dotnet.cmd test src\coreclr\tools\aot\ILCompiler.Compiler.Tests\ILCompiler.Compiler.Tests.csproj -c Release -p:Platform=x64 -p:UseSharedCompilation=false
    • 69 passed, 0 failed
    • Covers concrete-list and enumerable fallback selection, ordering, empty/single/multiple entries, custom enumerable behavior, null concrete lists, immediate/deferred/null conditions, and mutation detection
  • Built ILCompiler Release x64: 0 warnings, 0 errors
  • Built ILCompiler.ReadyToRun Release x64: 0 warnings, 0 errors
  • .\src\tests\build.cmd nativeaot Release tree nativeaot /p:UseSharedCompilation=false
  • .\src\tests\run.cmd runnativeaottests Release
    • 28 passed, 0 failed

Current-main benchmark

The retained RDM response is pinned to net10.0, WindowsDesktop 10.0.11, and the matching .NET 10 framework closure, while current main builds a net11.0 compiler and framework. Mixing those contracts would not be a valid comparison.

Instead, an ignored local runner generated a current-main net11.0 workload with 10,000 worker/marker type pairs and ran five interleaved baseline/changed pairs after warmup. Both compilers contained identical temporary measurement probes; no probes or experiment gates are in this change.

Metric Baseline median Changed median Change
Wall time 5.257 s 5.383 s +2.41%
CPU time 13.484 s 13.156 s -2.43%
Graph time 3.110 s 3.113 s +0.07%
Mark time 1.396 s 1.429 s +2.40%
Graph allocation 846.06 MiB 831.21 MiB -14.84 MiB (-1.75%)
Peak private memory 714.26 MiB 713.40 MiB -0.86 MiB

Short-run wall and phase measurements are within machine noise; the repeatable local signal is lower graph allocation. Every baseline/changed run produced the same 39,922,359-byte object with SHA-256 CE5C81C91A3774A8324455A306F9AE6AE06C30331E7A14D762F94AEEA27CC919.

Note

This PR description was drafted with GitHub Copilot.

Enumerate known concrete dependency lists and arrays directly while preserving custom enumerable and mutation semantics.

Add focused coverage for collection shapes, ordering, conditional dependencies, and mutation.
Copilot AI lite review requested due to automatic review settings August 29, 2026 17:15
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 29, 2026
@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.

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

Pull request overview

This PR updates the NativeAOT dependency analysis “mark” walk to avoid allocator churn when static/conditional dependencies are provided as common concrete collections but exposed via IEnumerable<T>.

Changes:

  • Adds fast paths in DependencyAnalyzer.GetStaticDependenciesImpl for exact DependencyList, exact List<T>, and arrays to avoid boxing/allocation of enumerators.
  • Extracts conditional dependency handling into a shared helper (ProcessConditionalDependency) while keeping existing conditional/deferred semantics.
  • Adds targeted unit tests covering ordering, fallback enumeration behavior (including List<T> subclasses that reimplement IEnumerable<T>), and mutation detection.
File summaries
File Description
src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs Adds exact-type fast paths for list/array dependency enumeration and factors conditional dependency handling into a helper.
src/coreclr/tools/aot/ILCompiler.Compiler.Tests/ILCompiler.Compiler.Tests.csproj Includes the new DependencyAnalyzerTests.cs in the explicit compile item list.
src/coreclr/tools/aot/ILCompiler.Compiler.Tests/DependencyAnalyzerTests.cs Adds coverage for ordering, fallback enumeration semantics, null/unconditional condition handling, and mutation detection.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

Move concrete-list selection to dependency producers so the analyzer no longer relies on runtime type checks. Preserve the enumerable fallback and cover empty concrete-list results.
Copilot AI review requested due to automatic review settings August 30, 2026 01:28

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

Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants