Skip to content

Fix list capacity calculation in GetGraphItemAsync that is leading to allocations#7026

Merged
jeffkl merged 1 commit intoNuGet:devfrom
nareshjo:getGraph-item-list-alloc
Jan 7, 2026
Merged

Fix list capacity calculation in GetGraphItemAsync that is leading to allocations#7026
jeffkl merged 1 commit intoNuGet:devfrom
nareshjo:getGraph-item-list-alloc

Conversation

@nareshjo
Copy link
Copy Markdown
Contributor

@nareshjo nareshjo commented Jan 5, 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: The allocation hot path is TypeAllocated!NuGet.LibraryModel.LibraryDependency[] with frames showing List<T>.EnsureCapacity/set_Capacity, which indicates repeated List backing-array growth.
    In DependencyGraphResolver.DependencyGraphItem.cs, method DependencyGraphResolver.DependencyGraphItem.GetGraphItemAsync, the new list is created with an incorrect capacity due to operator precedence:

new(capacity: RuntimeDependencies?.Count ?? 0 + item.Data.Dependencies.Count)

Because + binds tighter than ??, when RuntimeDependencies is non-null the capacity becomes just RuntimeDependencies.Count (ignoring item.Data.Dependencies.Count), causing predictable resizing and LibraryDependency[] allocations as items are added.

  • Issue type: Specify an up-front capacity for collections if one is known
  • Proposed fix: Fixing the expression to new(capacity: (RuntimeDependencies?.Count ?? 0) + item.Data.Dependencies.Count) is a trivial, localized change with minimal risk and directly addresses the hot-path 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 5, 2026 21:11
@nareshjo nareshjo requested review from martinrrm and zivkan January 5, 2026 21:11
@dotnet-policy-service dotnet-policy-service bot added the Community PRs created by someone not in the NuGet team label Jan 5, 2026
@jeffkl jeffkl self-assigned this Jan 6, 2026
Copy link
Copy Markdown
Contributor

@jeffkl jeffkl left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

@jeffkl jeffkl enabled auto-merge (squash) January 7, 2026 22:42
@jeffkl jeffkl merged commit 571d032 into NuGet:dev Jan 7, 2026
17 checks passed
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