Skip to content

Use MoveToImmutable for ImmutableArray<T>.Builder in ILCompiler.Compiler - #134426

Merged
agocke merged 2 commits into
dotnet:mainfrom
prozolic:pr-ilc
Sep 25, 2026
Merged

agocke merged 2 commits into
dotnet:mainfrom
prozolic:pr-ilc

Conversation

@prozolic

@prozolic prozolic commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR updates ImmutableArray<T>.Builder usages in ILCompiler.Compiler to call MoveToImmutable instead of ToImmutable/ToImmutableArray, specifying an initial capacity where it was missing. This avoids reallocating the backing array as it grows, as well as the extra copy when creating the ImmutableArray<T>.
Also replace the LINQ Skip(1) in ReflectionMethodBodyScanner with a span slice over the underlying list.
The overall performance impact is small, but this removes unnecessary allocations and copies.

Changes

  • AttributeDataFlow,MethodProxy,TypeProxy
    • Use MoveToImmutable instead of ToImmutableArray
  • TrimAnalysisMethodCallPattern
    • Specify capacity and use MoveToImmutable instead of ToImmutableArray
  • ReflectionMethodBodyScanner
    • Use CollectionsMarshal.AsSpan and Slice instead of the LINQ Skip

Validation

I ran the NativeAOT smoketests and ILCompiler.*.Tests using the following commands and confirmed that they all passed in local.

.\build.cmd clr.aot -rc Release -lc Release
.\src\tests\build.cmd nativeaot Release tree nativeaot /p:LibrariesConfiguration=Release
.\src\tests\run.cmd runnativeaottests Release
dotnet test src/coreclr/tools/aot/ILCompiler.Trimming.Tests/ILCompiler.Trimming.Tests.csproj -c Release
dotnet test src/coreclr/tools/aot/ILCompiler.TypeSystem.Tests/ILCompiler.TypeSystem.Tests.csproj -c Release
dotnet test src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework.Tests/ILCompiler.DependencyAnalysisFramework.Tests.csproj -c Release

.\build.cmd -s tools.illink -c Debug
dotnet test src/tools/illink/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj -c Debug

Update ImmutableArray<T>.Builder usages in ILCompiler.Compiler to call
MoveToImmutable instead of ToImmutable/ToImmutableArray, specifying an
initial capacity where it was missing.  This avoids reallocating the
backing array as it grows, as well as the extra copy when creating the
ImmutableArray<T>.
Also replace the LINQ Skip(1) in ReflectionMethodBodyScanner with a
span slice over the underlying list.

The overall performance impact is small, but this removes unnecessary
allocations and copies.

Changes:
- AttributeDataFlow,MethodProxy,TypeProxy
  - Use MoveToImmutable instead of ToImmutableArray
- TrimAnalysisMethodCallPattern
  - Specify capacity and use MoveToImmutable instead of ToImmutableArray
- ReflectionMethodBodyScanner
  - Use CollectionsMarshal.AsSpan and Slice instead of the LINQ Skip
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 22, 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.

@prozolic
prozolic marked this pull request as ready for review September 22, 2026 15:11
Copilot AI lite review requested due to automatic review settings September 22, 2026 15:11

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.

Copilot review overview

🟡 Changes recommended

Add the requested representative benchmark or allocation profile.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

This PR reduces ILCompiler dataflow allocations by using ImmutableArray builder moves and span slicing.

Changes:

  • Uses MoveToImmutable and builder capacities.
  • Replaces LINQ Skip(1) with CollectionsMarshal.AsSpan(...).Slice(1).
  • Review note: add a representative benchmark or allocation profile for the optimization.
File Description
src/​coreclr/​tools/​Common/​Compiler/​Dataflow/​TypeProxy.cs Moves generic parameter builder contents directly.
src/​coreclr/​tools/​Common/​Compiler/​Dataflow/​MethodProxy.cs Moves method generic parameter builder contents directly.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​Dataflow/​TrimAnalysisMethodCallPattern.cs Adds capacity and avoids immutable-array copies.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​Dataflow/​ReflectionMethodBodyScanner.cs Uses span slicing instead of Skip(1).
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​Dataflow/​AttributeDataFlow.cs Moves fixed attribute arguments directly.

Copilot AI review requested due to automatic review settings September 24, 2026 13:38
@prozolic
prozolic requested a review from sbomer as a code owner September 24, 2026 13:38
@dotnet-policy-service dotnet-policy-service Bot added linkable-framework Issues associated with delivering a linker friendly framework labels Sep 24, 2026

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.

Copilot review overview

🔵 Needs a closer look

Add representative before/after allocation or throughput measurements supporting the claimed performance improvements.

Review effort: Lite
Findings: None

Resolved since last review (1)

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@agocke
agocke merged commit 5686741 into dotnet:main Sep 25, 2026
156 of 158 checks passed
@prozolic
prozolic deleted the pr-ilc branch September 26, 2026 04:40
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants