Skip to content

Conversation

@nareshjo
Copy link

@nareshjo nareshjo commented Dec 9, 2025

This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights

  • Issue: ProcessMetadataElements(ProjectItemElement, OperationBuilderWithMetadata) calls AddRange(itemElement.Metadata) on ImmutableArray.Builder. ETW traces show this method as an allocation hotspot due to allocations from IEnumerable.GetEnumerator interface calls during AddRange(KeyValuePair<K, V>[]), indicating the collection's struct enumerator is being boxed to satisfy the IEnumerable parameter requirement.
  • Issue type: AVOID boxing enumerators of built-in types
  • Proposed fix: Remove the AddRange(KeyValuePair<K, V>[]) call and add elements individually inside the existing ForEach(Action) loop. The C# compiler optimizes ForEach(Action) to directly use the collection's concrete GetEnumerator() method, avoiding boxing when a struct enumerator is available. This also eliminates duplicate enumeration of Metadata, providing two performance benefits:
  1. Eliminates boxing allocation - The struct enumerator is used directly without boxing to the IEnumerable interface
  2. Eliminates duplicate enumeration - The collection is only enumerated once instead of twice

Best practices wiki
See related failure in PRISM
ADO work item

Copilot AI review requested due to automatic review settings December 9, 2025 18:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes ProcessMetadataElements to reduce allocations by eliminating enumerator boxing. The change moves metadata element addition from a single AddRange call to individual Add calls within the existing loop.

Key changes:

  • Removed AddRange(itemElement.Metadata) call that caused boxing of struct enumerator
  • Added individual Add(metadatumElement) calls inside the existing foreach loop
  • Eliminated duplicate enumeration of the Metadata collection

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant