Skip to content

Reduce allocations in ResolverMetadataClient.ProcessPackageVersion by avoiding JArray enumerator allocations - #7089

Merged
donnie-msft merged 1 commit into
NuGet:devfrom
nareshjo:ProcessPackageVersion-EnumeratorAlloc
Feb 10, 2026
Merged

donnie-msft merged 1 commit into
NuGet:devfrom
nareshjo:ProcessPackageVersion-EnumeratorAlloc

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: ResolverMetadataClient.ProcessPackageVersion() uses foreach to iterate over JArray objects.
    ForEach over JArray calls JArray.GetEnumerator(), which returns the internal List.Enumerator struct as IEnumerator, causing it to be boxed and allocated on the heap.
nuget.protocol.dll!NuGet.Protocol.ResolverMetadataClient+<GetDependencies>d__.MoveNext
nuget.protocol.dll!NuGet.Protocol.ResolverMetadataClient.ProcessPackageVersion
newtonsoft.json.dll!Newtonsoft.Json.Linq.JArray.GetEnumerator
mscorlib.dll!System.Collections.Generic.List`[System.__Canon].System.Collections.Generic.IEnumerable<T>.GetEnumerator
TypeAllocated!Enumerator[Newtonsoft.Json.Linq.JToken]
  • Issue type: Avoid enumerator allocations in hot path

  • Proposed fix: Replace foreach with index-based for loops using JArray.Count and JArray[index].
    This eliminates Enumerator heap allocations since JArray supports O(1) index access via its internal List.
    The fix is applied to both the outer loop (dependency groups) and inner loop (dependencies), with the inner loop having higher impact due to nesting.

Best practices wiki
See related failure in PRISM
ADO work item

@nareshjo
nareshjo requested a review from a team as a code owner January 30, 2026 02:06
@dotnet-policy-service dotnet-policy-service Bot added the Community PRs created by someone not in the NuGet team label Jan 30, 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 Feb 6, 2026
@nareshjo

Copy link
Copy Markdown
Contributor Author

@nkolev92, @donnie-msft this has been marked as stale and will be auto-closed in a few days. A review would be helpful.

@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 Feb 10, 2026
@donnie-msft donnie-msft self-assigned this Feb 10, 2026
@donnie-msft
donnie-msft merged commit 4ab100e into NuGet:dev Feb 10, 2026
16 checks passed
@nareshjo
nareshjo deleted the ProcessPackageVersion-EnumeratorAlloc branch February 11, 2026 03:18
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.

3 participants