Skip to content

Reduce allocation in GetNuGetProjects by providing capacity for List - #7050

Merged
nkolev92 merged 1 commit into
NuGet:devfrom
nareshjo:projectsystemcache-getnugetprojects-list-alloc
Jan 22, 2026
Merged

nkolev92 merged 1 commit into
NuGet:devfrom
nareshjo:projectsystemcache-getnugetprojects-list-alloc

Conversation

@nareshjo

Copy link
Copy Markdown
Contributor

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: ProjectSystemCache.GetNuGetProjects() builds the return value via LINQ + ToList():
_primaryCache.Select(kv => kv.Value.NuGetProject).ToList();

Because the source is an iterator (Select over the dictionary), ToList() can’t pre-size the list and grows it incrementally, triggering List<T>.EnsureCapacity and allocating/resizing the backing NuGetProject[].
This matches the allocation stack:

TypeAllocated!NuGet.ProjectManagement.NuGetProject[]
clr.dll!SVR::gc_heap::try_allocate_more_space
clr.dll!SVR::GCHeap::Alloc
clr.dll!AllocateArrayEx
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
mscorlib.dll!System.Collections.Generic.List`[System.__Canon]..ctor
system.core.dll!System.Linq.Enumerable.ToList[System.__Canon]
nuget.packagemanagement.visualstudio.dll!NuGet.PackageManagement.VisualStudio.ProjectSystemCache.GetNuGetProjects
  • Issue type: Specify an up-front capacity for collections if one is known

  • Proposed fix: Pre-size the list using _primaryCache.Count and fill it without LINQ:
    This removes the EnsureCapacity -> set_Capacity -> AllocateArray growth path that’s producing the NuGetProject[] allocation spikes.

Best practices wiki
See related failure in PRISM
ADO work item

@nareshjo
nareshjo requested a review from a team as a code owner January 14, 2026 22:46
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Jan 14, 2026
@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Jan 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 30 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

@dotnet-policy-service dotnet-policy-service Bot removed the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Jan 22, 2026
@nkolev92
nkolev92 enabled auto-merge (squash) January 22, 2026 19:07
@nkolev92 nkolev92 self-assigned this Jan 22, 2026
@nkolev92
nkolev92 merged commit 1e80048 into NuGet:dev Jan 22, 2026
16 checks passed
@nareshjo
nareshjo deleted the projectsystemcache-getnugetprojects-list-alloc branch January 22, 2026 20:02
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.

2 participants