RecyclableBlockList renamed to RecyclableArrayList for clarity. First set of optimizations and implementation.
This commit is contained in:
1 parent
8147c4ecbf
commit
880abd605e
16 files changed
+1040
-800
No files matched your search
@@ -1,51 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public class RecyclableList<T> : IDisposable, IList<T>
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
private const int _minPooledArraySize = 100;
|
||||
private readonly int _blockSize;
|
||||
private readonly RecyclableArrayList<T[]> _memoryBlocks;
|
||||
|
||||
private readonly RecyclableBlockList<T[]> _memoryBlocks;
|
||||
private bool _disposedValue;
|
||||
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
private static void AddCapacity(RecyclableList<T> list, RecyclableArrayList<T[]> memoryBlocks, int additionalCapacity)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
initialCapacity ??= 8 * BlockSize;
|
||||
int additionalArray = initialCapacity % BlockSize > 0 ? 1 : 0;
|
||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalArray;
|
||||
_memoryBlocks = new(targetCapacity);
|
||||
Capacity = initialCapacity.Value;
|
||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
||||
{
|
||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
||||
}
|
||||
|
||||
//MemoryBlocks.LongCount = MemoryBlocks.Capacity;
|
||||
T[] newMemoryBlock = RentArrayFromPool(additionalCapacity);
|
||||
memoryBlocks.Add(newMemoryBlock);
|
||||
list.Capacity += additionalCapacity;
|
||||
}
|
||||
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
private static void AddRange(RecyclableList<T> destination, IEnumerable<T> source)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
initialCapacity ??= 8 * BlockSize;
|
||||
int additionalCapacity = initialCapacity % BlockSize > 0 ? 1 : 0;
|
||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalCapacity;
|
||||
_memoryBlocks = new(targetCapacity);
|
||||
Capacity = initialCapacity.Value;
|
||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
||||
{
|
||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
||||
}
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
Add(item);
|
||||
destination.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public int BlockSize { get; }
|
||||
private static void RemoveAt(RecyclableList<T> list, long index)
|
||||
{
|
||||
if (index == list.LongCount - 1)
|
||||
{
|
||||
list.LongCount--;
|
||||
if (list.Capacity - list.LongCount == list._blockSize)
|
||||
{
|
||||
list.RemoveBlock(list.BlockCount - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Only removal of the last element is supported");
|
||||
}
|
||||
}
|
||||
|
||||
private static T[] RentArrayFromPool(int minSize) => (minSize >= _minPooledArraySize)
|
||||
? ArrayPool<T>.Shared.Rent(minSize)
|
||||
: new T[minSize];
|
||||
|
||||
private static void ReturnToPool(in T[] array)
|
||||
{
|
||||
if (array.LongLength is < _minPooledArraySize or > int.MaxValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
|
||||
private static void RemoveBlock(RecyclableList<T> owner, RecyclableArrayList<T[]> memoryBlocks, int blockSize, int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
ReturnToPool(memoryBlocks[index]);
|
||||
memoryBlocks.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
owner.Capacity -= blockSize;
|
||||
}
|
||||
}
|
||||
|
||||
private static RecyclableArrayList<T[]> SetupMemoryBlocks(RecyclableList<T> owner, int blockSize, long? totalItemsCount)
|
||||
{
|
||||
totalItemsCount ??= 8 * blockSize;
|
||||
int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0;
|
||||
int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray;
|
||||
RecyclableArrayList<T[]> memoryBlocks = new(memoryBlockCount);
|
||||
for (var blockIdx = 0; blockIdx < memoryBlockCount; blockIdx++)
|
||||
{
|
||||
memoryBlocks[blockIdx] = RentArrayFromPool(blockSize);
|
||||
}
|
||||
|
||||
memoryBlocks.Count = memoryBlockCount;
|
||||
owner.Capacity = totalItemsCount.Value;
|
||||
return memoryBlocks;
|
||||
}
|
||||
|
||||
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||
{
|
||||
_blockSize = blockSize;
|
||||
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||
}
|
||||
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||
{
|
||||
_blockSize = blockSize;
|
||||
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||
AddRange(this, source);
|
||||
}
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
@@ -59,23 +110,22 @@ namespace Recyclable.Collections
|
||||
set => _memoryBlocks[index / _blockSize][index % _blockSize] = value;
|
||||
}
|
||||
|
||||
public long Capacity { get; set; }
|
||||
public long Capacity { get; protected set; }
|
||||
public int Count => (int)LongCount;
|
||||
public long LongCount { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
public long BlockCount => _memoryBlocks.LongCount;
|
||||
public int BlockCount => _memoryBlocks.Count;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(T item)
|
||||
{
|
||||
if (LongCount == Capacity)
|
||||
long oldCount = LongCount;
|
||||
int blockSize = _blockSize;
|
||||
if (oldCount == Capacity)
|
||||
{
|
||||
T[] newMemoryBlock = BlockSize.RentArrayFromPool<T>();
|
||||
_memoryBlocks.Add(newMemoryBlock);
|
||||
Capacity += BlockSize;
|
||||
AddCapacity(this, _memoryBlocks, blockSize);
|
||||
}
|
||||
|
||||
memoryBlocks[oldCount / blockSize][oldCount % blockSize] = item;
|
||||
_memoryBlocks[(int)(oldCount / blockSize)][oldCount % blockSize] = item;
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
@@ -88,7 +138,7 @@ namespace Recyclable.Collections
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveBlock(_memoryBlocks.Count - 1);
|
||||
RemoveBlock(this, _memoryBlocks, _blockSize, _memoryBlocks.Count - 1);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -106,81 +156,29 @@ namespace Recyclable.Collections
|
||||
|
||||
public bool Contains(T item) => _memoryBlocks.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.CopyTo(0, BlockSize, (int)LongCount % BlockSize, array, arrayIndex);
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.CopyTo(0, _blockSize, (int)LongCount % _blockSize, array, arrayIndex);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||
|
||||
public int IndexOf(T item) => (int)_memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
public int IndexOf(T item) => (int)_memoryBlocks.LongIndexOf(_blockSize, item, _equalityComparer);
|
||||
|
||||
public void Insert(int index, T item) => throw new NotSupportedException();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
public long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(_blockSize, item, _equalityComparer);
|
||||
|
||||
public bool Remove(T item) => throw new NotSupportedException();
|
||||
|
||||
public void RemoveAt(int index) => RemoveAt(this, index);
|
||||
public void RemoveAt(long index) => RemoveAt(this, index);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void RemoveAt(RecyclableList<T> list, long index)
|
||||
{
|
||||
if (index == list.LongCount - 1)
|
||||
{
|
||||
list.LongCount--;
|
||||
if (list.Capacity - list.LongCount == list.BlockSize)
|
||||
{
|
||||
list.RemoveBlock(list.BlockCount - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Only removal of the last element is supported");
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
||||
// ~RecyclableList()
|
||||
// {
|
||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
Clear();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void RemoveBlock(long index)
|
||||
{
|
||||
try
|
||||
{
|
||||
_memoryBlocks[index].ReturnToPool();
|
||||
_memoryBlocks.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Capacity -= BlockSize;
|
||||
LongCount -= BlockSize;
|
||||
}
|
||||
}
|
||||
public void RemoveBlock(int index) => RemoveBlock(this, _memoryBlocks, _blockSize, index);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user