Skip to content

Commit 5b05c1e

Browse files
authored
Reduce List resize allocations in PopulateItemGroups hot path (#7083)
1 parent 1f8b2d8 commit 5b05c1e

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/NuGet.Core/NuGet.Packaging/ContentModel/ContentItemCollection.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public void PopulateItemGroups(PatternSet definition, IList<ContentItemGroup> co
115115

116116
if (groupAssets != null)
117117
{
118+
// Pre-size the list to avoid repeated array allocations during Add operations.
119+
// Callers typically pass a List but check just to be safe.
120+
if (contentItemGroupList is List<ContentItemGroup> list)
121+
{
122+
list.Capacity = Math.Max(list.Capacity, contentItemGroupList.Count + groupAssets.Count);
123+
}
124+
118125
foreach (var (item, assets) in groupAssets)
119126
{
120127
contentItemGroupList.Add(new ContentItemGroup(

0 commit comments

Comments
 (0)