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

Reduce memory and CPU costs due to SegmentedList usage #75661

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions src/Dependencies/Collections/SegmentedArray`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ public SegmentedArray(int length)
}
}

public SegmentedArray<T> Resize(int newLength)
sharwell marked this conversation as resolved.
Show resolved Hide resolved
{
// For now, only allow growing resizes
if (newLength <= _length)
throw new ArgumentOutOfRangeException(nameof(newLength));

var newItems = new T[(newLength + SegmentSize - 1) >> SegmentShift][];
sharwell marked this conversation as resolved.
Show resolved Hide resolved
var lastPageSize = newLength - ((newItems.Length - 1) << SegmentShift);

// Copy over all old pages
for (var i = 0; i < _items.Length; i++)
newItems[i] = _items[i];

// If the previous last page is still the last page, resize it to lastPageSize.
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
// Otherwise, resize it to SegmentSize.
if (_items.Length > 0)
{
Array.Resize(
ref newItems[_items.Length - 1],
_items.Length == newItems.Length ? lastPageSize : SegmentSize);
}

// Create all new pages (except the last one which is done separately)
for (var i = _items.Length; i < newItems.Length - 1; i++)
newItems[i] = new T[SegmentSize];

// Create a new last page if necessary
if (_items.Length < newItems.Length)
newItems[newItems.Length - 1] = new T[lastPageSize];

return new SegmentedArray<T>(newLength, newItems);
}

private SegmentedArray(int length, T[][] items)
{
_length = length;
Expand Down
15 changes: 9 additions & 6 deletions src/Dependencies/Collections/SegmentedList`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ public int Capacity
{
if (value > 0)
{
var newItems = new SegmentedArray<T>(value);
if (_size > 0)
{
SegmentedArray.Copy(_items, newItems, _size);
}
_items = newItems;
_items = _items.Resize(value);
}
else
{
Expand Down Expand Up @@ -503,6 +498,14 @@ internal void Grow(int capacity)
// Capacities exceeding Array.MaxLength will be surfaced as OutOfMemoryException by Array.Resize.
if (newCapacity < capacity)
newCapacity = capacity;
else
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
{
var segmentSize = SegmentedArrayHelper.GetSegmentSize<T>();

// If caller didn't request a large capacity increase, limit the increase to a single page
if (newCapacity > segmentSize)
newCapacity = (((capacity - 1) / segmentSize) + 1) * segmentSize;
sharwell marked this conversation as resolved.
Show resolved Hide resolved
}

Capacity = newCapacity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace IdeCoreBenchmarks
{
[MemoryDiagnoser]
[DisassemblyDiagnoser]
public class SegmentedListBenchmarks_InsertRange
{
Expand All @@ -23,7 +24,7 @@ public class SegmentedListBenchmarks_InsertRange
private Microsoft.CodeAnalysis.Collections.SegmentedList<object?> _segmentedValuesObject = null!;
private SegmentedArray<object?> _segmentedInsertValuesObject;

[Params(100000)]
[Params(1_000, 10_000, 100_000, 1_000_000)]
public int Count { get; set; }

[GlobalSetup]
Expand All @@ -40,6 +41,17 @@ public void GlobalSetup()
_segmentedInsertValuesObject = new SegmentedArray<object?>(100);
}

[Benchmark(Description = "AddToSegmentedList<object>", Baseline = true)]
public void AddList()
sharwell marked this conversation as resolved.
Show resolved Hide resolved
{
var array = new Microsoft.CodeAnalysis.Collections.SegmentedList<object?>();
var iterations = Count;
for (var i = 0; i < iterations; i++)
{
array.Add(null);
}
}

[Benchmark(Description = "List<int>", Baseline = true)]
public void InsertRangeList()
{
Expand Down