Skip to content

Reduce allocation in GetContentFileGroup by providing accurate capacity for List#7037

Merged
zivkan merged 1 commit intoNuGet:devfrom
nareshjo:contentFileUtils-GetContentFileGroup-list-alloc
Jan 13, 2026
Merged

Reduce allocation in GetContentFileGroup by providing accurate capacity for List#7037
zivkan merged 1 commit intoNuGet:devfrom
nareshjo:contentFileUtils-GetContentFileGroup-list-alloc

Conversation

@nareshjo
Copy link
Copy Markdown
Contributor

@nareshjo nareshjo commented Jan 9, 2026

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: In ContentFileUtils.GetContentFileGroup the results list is initialized with contentFileGroups.Count as capacity, but the actual number of items added equals entryMappings.Count (which is the count of unique file paths across all groups). Since each content group typically contains multiple items, entryMappings.Count is significantly larger than contentFileGroups.Count, causing repeated list resizing and unnecessary LockFileContentFile[] allocations.
    Evidence:

    TypeAllocated!NuGet.ProjectModel.LockFileContentFile[]
    clr.dll!JIT_NewArr1
    mscorlib.dll!System.Collections.Generic.List`[System.__Canon].set_Capacity
    mscorlib.dll!System.Collections.Generic.List`[System.__Canon].EnsureCapacity
    mscorlib.dll!System.Collections.Generic.List`[System.__Canon].Add
    nuget.commands.dll!NuGet.Commands.ContentFileUtils.GetContentFileGroup
    
  • Issue type: Specify an up-front capacity for collections if one is known

  • Proposed fix: Move the results list initialization to after the entryMappings dictionary is fully populated, and change the capacity from contentFileGroups.Count to entryMappings.Count. This ensures the list is allocated with the exact required capacity, eliminating all resize-triggered array allocations.

Best practices wiki
See related failure in PRISM
ADO work item

@nareshjo nareshjo requested a review from a team as a code owner January 9, 2026 17:17
@nareshjo nareshjo requested review from donnie-msft and zivkan January 9, 2026 17:17
@dotnet-policy-service dotnet-policy-service bot added the Community PRs created by someone not in the NuGet team label Jan 9, 2026
@zivkan zivkan merged commit d8e33dd into NuGet:dev Jan 13, 2026
17 checks passed
@nareshjo nareshjo deleted the contentFileUtils-GetContentFileGroup-list-alloc branch January 14, 2026 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants