Use MoveToImmutable for ImmutableArray<T>.Builder in ILCompiler.Compiler - #134426
Merged
Merged
Conversation
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
|
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. |
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Contributor
There was a problem hiding this comment.
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
What changed in this PR
This PR reduces ILCompiler dataflow allocations by using ImmutableArray builder moves and span slicing.
Changes:
- Uses
MoveToImmutableand builder capacities. - Replaces LINQ
Skip(1)withCollectionsMarshal.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. |
This was referenced Sep 24, 2026
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
agocke
approved these changes
Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
This PR updates
ImmutableArray<T>.Builderusages inILCompiler.Compilerto callMoveToImmutableinstead ofToImmutable/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 theImmutableArray<T>.Also replace the LINQ
Skip(1)inReflectionMethodBodyScannerwith a span slice over the underlying list.The overall performance impact is small, but this removes unnecessary allocations and copies.
Changes
MoveToImmutableinstead ofToImmutableArrayMoveToImmutableinstead ofToImmutableArrayCollectionsMarshal.AsSpanandSliceinstead of the LINQSkipValidation
I ran the NativeAOT smoketests and
ILCompiler.*.Testsusing the following commands and confirmed that they all passed in local.