Skip to content

Pre-size List in TryGetPortableFrameworks to avoid array resize allocations - #7095

Merged
zivkan merged 1 commit into
NuGet:devfrom
nareshjo:TryGetPortableFrameworks-list-resizing
Feb 6, 2026
Merged

zivkan merged 1 commit into
NuGet:devfrom
nareshjo:TryGetPortableFrameworks-list-resizing

Conversation

@nareshjo

@nareshjo nareshjo commented Feb 3, 2026

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: FrameworkNameProvider.TryGetPortableFrameworks(string) adds items to a List<NuGetFramework> that starts with default capacity (0). As items are added, the list repeatedly resizes its internal array (0→4→8→16...), allocating new NuGetFramework[] arrays on each resize.

This matches the allocation stack flow showing ParseFolder → TryGetPortableFrameworks → List<>.Add → List<>.EnsureCapacity → List<>.set_Capacity → TypeAllocated!NuGet.Frameworks.NuGetFramework[]

nuget.frameworks.dll![COLD]NuGet.Frameworks.NuGetFramework.ParseFolder
nuget.frameworks.dll!NuGet.Frameworks.FrameworkNameProvider.TryGetPortableFrameworks
mscorlib.dll!System.Collections.Generic.List`[System.__Canon].Add
mscorlib.dll!System.Collections.Generic.List`[System.__Canon].EnsureCapacity
mscorlib.dll!System.Collections.Generic.List`[System.__Canon].set_Capacity
clr.dll!JIT_NewArr1
TypeAllocated!NuGet.Frameworks.NuGetFramework[]
  • Issue type: Avoid repeated array resize allocations in hot path

  • Proposed fix: Pre-size the list capacity using the known shortNames.Length.
    Change new List<NuGetFramework>() to new List<NuGetFramework>(shortNames.Length).
    This eliminates resize allocations since the exact count is known upfront from the split result. After splitting on '+', we know precisely how many frameworks will be parsed and added to the list.

Best practices wiki
See related failure in PRISM
ADO work item

@nareshjo
nareshjo requested a review from a team as a code owner February 3, 2026 01:06
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Feb 3, 2026
@zivkan
zivkan merged commit d8f2f09 into NuGet:dev Feb 6, 2026
16 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