ReleaseBlock method removed.

This commit is contained in:
mlemanczyk committed 2023-01-23 00:16:02 +01:00
1 parent 64be78d1c6
commit 30dfd97336
1 file changed
+16 -35
+16 -35
View File
@@ -47,7 +47,8 @@ namespace Recyclable.Collections
if ((list._capacity * blockSize) - oldCountMinus1 == blockSize) if ((list._capacity * blockSize) - oldCountMinus1 == blockSize)
{ {
T[][] memoryBlocks = list._memoryBlocks; T[][] memoryBlocks = list._memoryBlocks;
ReleaseBlock(list, memoryBlocks, blockSize, memoryBlocks.Length - 1); memoryBlocks[^1].ReturnToPool(_blockArrayPool);
list._capacity -= blockSize;
} }
} }
@@ -168,7 +169,7 @@ namespace Recyclable.Collections
_memoryBlocks = _emptyBlockArray; _memoryBlocks = _emptyBlockArray;
} }
AddRange(this, source); AddRange(source);
} }
public T this[long index] public T this[long index]
@@ -302,7 +303,7 @@ namespace Recyclable.Collections
_longCount = targetCapacity; _longCount = targetCapacity;
} }
private void AddRange(RecyclableList<T> destination, IEnumerable<T> source, int growByCount = RecyclableDefaults.MinPooledArrayLength) private void AddRange(IEnumerable<T> source, int growByCount = RecyclableDefaults.MinPooledArrayLength)
{ {
if (source is T[] sourceArray) if (source is T[] sourceArray)
{ {
@@ -420,30 +421,17 @@ namespace Recyclable.Collections
public void Clear() public void Clear()
{ {
try ArrayPool<T> blockArrayPool = _blockArrayPool;
Span<T[]> memoryBlocksSpan = new(_memoryBlocks);
int memoryBlocksCount = memoryBlocksSpan.Length;
for (var toRemoveIdx = 0; toRemoveIdx < memoryBlocksCount; toRemoveIdx++)
{ {
// Remove in reversed order for performance savings memoryBlocksSpan[toRemoveIdx].ReturnToPool(blockArrayPool);
var toRemoveIdx = _memoryBlocks.Length - 1;
while (toRemoveIdx >= 0)
{
try
{
ReleaseBlock(this, _memoryBlocks, _blockSize, toRemoveIdx);
}
catch (Exception)
{
// We want to try returning as many arrays, as possible, before
// the list is cleared.
} }
toRemoveIdx--; _capacity = 0;
}
}
finally
{
_longCount = 0; _longCount = 0;
} }
}
public bool Contains(T item) => _memoryBlocks.Any(x => x.Contains(item)); public bool Contains(T item) => _memoryBlocks.Any(x => x.Contains(item));
@@ -455,24 +443,17 @@ namespace Recyclable.Collections
public void Insert(int index, T item) => throw new NotSupportedException(); public void Insert(int index, T item) => throw new NotSupportedException();
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 bool Remove(T item) => throw new NotSupportedException();
public void RemoveBlock(int index) => ReleaseBlock(this, _memoryBlocks, _blockSize, index); public void RemoveBlock(int index)
{
_memoryBlocks[index].ReturnToPool(_blockArrayPool);
_capacity -= _blockSize;
}
public void RemoveAt(int index) => RemoveAt(this, index); public void RemoveAt(int index) => RemoveAt(this, index);
public void RemoveAt(long index) => RemoveAt(this, index); public void RemoveAt(long index) => RemoveAt(this, index);
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
private static void ReleaseBlock(RecyclableList<T> owner, in T[][] memoryBlocks, int blockSize, int blockIndex)
{
try
{
memoryBlocks[blockIndex].ReturnToPool(_blockArrayPool);
}
finally
{
owner._capacity -= blockSize;
}
}
public void Dispose() public void Dispose()
{ {
if (_capacity > 0) if (_capacity > 0)