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

Reduced allocations in ArrayBuilder #63

Merged
merged 2 commits into from
Nov 25, 2021
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
23 changes: 12 additions & 11 deletions source/gfoidl.DataCompression/Builders/ArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ internal struct ArrayBuilder<T> : ICollectionBuilder<T>
{
private const int StartCapacity = 4;
private const int ResizeThreshold = 8;
private readonly int _maxCapacity;
private readonly List<T[]> _buffers;
private readonly int _maxCapacity;

private T[] _firstBuffer;
private T[] _currentBuffer;
private int _index;
private int _count;
private List<T[]>? _buffers;
private T[] _firstBuffer;
private T[] _currentBuffer;
private int _index;
private int _count;
//---------------------------------------------------------------------
public ArrayBuilder(bool initialize) : this(int.MaxValue) { }
//---------------------------------------------------------------------
public ArrayBuilder(int maxCapacity) : this()
{
_maxCapacity = maxCapacity;
_firstBuffer = _currentBuffer = new T[StartCapacity];
_buffers = new List<T[]>();
_buffers = null;
}
//---------------------------------------------------------------------
public readonly int Count => _count;
Expand Down Expand Up @@ -104,8 +104,8 @@ private void AddRange(T[] array)
[MethodImpl(MethodImplOptions.NoInlining)]
private void AddWithBufferAllocation(T item, ref T[] destination, ref int index)
{
_count += index - _index;
_index = index;
_count += index - _index;
_index = index;
this.AllocateBuffer();
destination = _currentBuffer;
index = _index;
Expand Down Expand Up @@ -160,7 +160,7 @@ private void AllocateBuffer()
int newCapacity = Math.Min(_count == 0 ? StartCapacity : _count * 2, _maxCapacity);
_currentBuffer = new T[newCapacity];
Array.Copy(_firstBuffer, 0, _currentBuffer, 0, _count);
_firstBuffer = _currentBuffer;
_firstBuffer = _currentBuffer;
}
else
{
Expand All @@ -175,6 +175,7 @@ private void AllocateBuffer()
// doing min(64, 100 - 64). The lhs represents double the last buffer,
// the rhs the limit minus the amount we've already allocated.

_buffers ??= new List<T[]>();
_buffers.Add(_currentBuffer);
newCapacity = Math.Min(_count, _maxCapacity - _count);
}
Expand All @@ -188,7 +189,7 @@ private readonly T[] GetBuffer(int index)
{
return index == 0
? _firstBuffer
: index <= _buffers.Count
: index <= _buffers?.Count
? _buffers[index - 1] // first "buffer" is _firstBuffer resized
: _currentBuffer;
}
Expand Down
6 changes: 3 additions & 3 deletions source/gfoidl.DataCompression/DataPointIndexedIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override bool MoveNext()
_incomingIndex = 1;
return true;
case 1:
TList source = _list;
TList source = _list!;
int snapShotIndex = _snapShotIndex;
int incomingIndex = _incomingIndex;
int lastArchivedIndex = _lastArchivedIndex;
Expand Down Expand Up @@ -132,7 +132,7 @@ public override DataPoint[] ToArray()
{
Debug.Assert(_list is not null);

TList source = _list;
TList source = _list!;

Debug.Assert(source.Count > 0);
if (source.Count == 1 && 0 < (uint)source.Count)
Expand All @@ -148,7 +148,7 @@ public override List<DataPoint> ToList()
{
Debug.Assert(_list is not null);

TList source = _list;
TList source = _list!;

Debug.Assert(source.Count > 0);
if (source.Count == 1 && 0 < (uint)source.Count)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (c) gfoidl, all rights reserved
// (c) gfoidl, all rights reserved

using System.Collections.Generic;
using gfoidl.DataCompression.Builders;
Expand Down Expand Up @@ -32,7 +32,9 @@ private static IEnumerable<TestCaseData> Add_items_ToArray___correct_result_Test
yield return new TestCaseData(1);
yield return new TestCaseData(2);
yield return new TestCaseData(4);
yield return new TestCaseData(5); // StartCapacity + 1
yield return new TestCaseData(8);
yield return new TestCaseData(9); // ResizeThreshold + 1
yield return new TestCaseData(16);
yield return new TestCaseData(100);
yield return new TestCaseData(1_000);
Expand Down