Skip to content

Perf: Reorder RAR output metadata copies#12298

Merged
YuliiaKovalova merged 9 commits into
dotnet:mainfrom
ccastanedaucf:dev/chcasta/rar-metadata-perf
Oct 27, 2025
Merged

Perf: Reorder RAR output metadata copies#12298
YuliiaKovalova merged 9 commits into
dotnet:mainfrom
ccastanedaucf:dev/chcasta/rar-metadata-perf

Conversation

@ccastanedaucf

@ccastanedaucf ccastanedaucf commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Fixes

Refactor of ReferenceTable.SetItemMetadata to take advantage of new TaskItem COW cloning and batch updates, reducing total number of ImmutableDictionary operations and RAR e2e time.

Before (CPU time in RAR SetItemMetadata 1,951ms)
image

After (758ms aka -1.1s)
image

Before (allocations under TaskItem)
image

After (-288MB)
image

Context

ReferenceTable.SetItemMetadata copies metadata from inputs (ProjectItemInstance.TaskItem) to RAR's primary and related file outputs (Utilities.TaskItem), with certain keys filtered out or removed depending on the output parameter.

Since we now support ImmutableDictionary clones across TaskItem implementations, we can significantly reduce CPU and allocations here by reordering the sequence of modifications and batching updates when possible, as each operation will always result in a new ImmutableDictionary instance.

Changes Made

  • Added RemoveMetadataRange(IEnumerable<string>) to IMetadataContainer, which forwards to ImmutableDictionary.RemoveRange(IEnumerable<string>.
  • The source TaskItem now populates initial metadata rather than EnumerateCommonMetadata().
  • "NonFowardableMetadata" is reimplemented as a separate clone of the output reference item, and only if related items are found. This skips the need to track another dictionary and removes/updates throughout.
  • General consolidation / batching of adds / removes that always need to occur.

Copilot AI review requested due to automatic review settings August 7, 2025 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.

Pull Request Overview

This performance optimization PR refactors the ResolveAssemblyReference (RAR) task to reduce CPU time and memory allocations when copying metadata between TaskItem instances. The changes leverage new copy-on-write (COW) cloning capabilities to minimize expensive ImmutableDictionary operations.

Key changes:

  • Added RemoveMetadataRange method to batch metadata removal operations
  • Reordered metadata operations to maximize COW efficiency by performing initial copies before modifications
  • Consolidated metadata setting logic into iterator methods for better batching

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Framework/IMetadataContainer.cs Added RemoveMetadataRange interface method
src/Framework/TaskItemData.cs Added NotImplementedException stub for RemoveMetadataRange
src/Build/Instance/ProjectItemInstance.cs Implemented RemoveMetadataRange using ImmutableDictionary.RemoveRange
src/Utilities/TaskItem.cs Implemented RemoveMetadataRange with null/empty checks
src/Shared/TaskParameter.cs Implemented RemoveMetadataRange as loop over individual removals
src/Tasks/AssemblyDependency/ReferenceTable.cs Major refactoring of SetItemMetadata for performance optimization

Comment thread src/Tasks/AssemblyDependency/ReferenceTable.cs Outdated
Comment thread src/Tasks/AssemblyDependency/ReferenceTable.cs Outdated
Comment thread src/Tasks/AssemblyDependency/ReferenceTable.cs Outdated
Comment thread src/Tasks/AssemblyDependency/ReferenceTable.cs Outdated
@ghost ghost assigned SimaTian Sep 2, 2025
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.

4 participants