Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an allocation regression due to the recent SegmentedList change c… #75895

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,18 @@ public void EnsureCapacity_MatchesSizeWithLargeCapacityRequest(int segmentCount)

Assert.Equal(expectedCapacity, list.Capacity);
}

[Fact]
public void EnsureCapacity_InitialCapacitySlightlyMoreThanHalfSegmentSizeGrowsToFullSegmentSize()
{
var elementCount = SegmentedArray<T>.TestAccessor.SegmentSize / 2 + 1;
var list = new SegmentedList<T>(elementCount);

Assert.Equal(elementCount, list.Capacity);

list.EnsureCapacity(elementCount + 1);

Assert.Equal(SegmentedArray<T>.TestAccessor.SegmentSize, list.Capacity);
}
}
}
5 changes: 5 additions & 0 deletions src/Dependencies/Collections/SegmentedList`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ internal void Grow(int capacity)
// should be DefaultCapacity. Otherwise, the new capacity should be double the current array size.
newCapacity = _items.Length == 0 ? DefaultCapacity : _items.Length * 2;
}
else if (_items.Length < SegmentedArrayHelper.GetSegmentSize<T>())
{
// There is only a single segment that is over half full. Increase it to a full segment.
newCapacity = SegmentedArrayHelper.GetSegmentSize<T>();
}
else
{
// If the last segment is fully sized, increase the number of segments by the desired growth rate
Expand Down
Loading