From 30dfd973367a0d63c788839461cb7ad06e2e6c95 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Mon, 23 Jan 2023 00:16:02 +0100 Subject: [PATCH] ReleaseBlock method removed. --- Recyclable.Collections/RecyclableList.cs | 55 ++++++++---------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 0765cb4..6dd787a 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -47,7 +47,8 @@ namespace Recyclable.Collections if ((list._capacity * blockSize) - oldCountMinus1 == blockSize) { 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; } - AddRange(this, source); + AddRange(source); } public T this[long index] @@ -302,7 +303,7 @@ namespace Recyclable.Collections _longCount = targetCapacity; } - private void AddRange(RecyclableList destination, IEnumerable source, int growByCount = RecyclableDefaults.MinPooledArrayLength) + private void AddRange(IEnumerable source, int growByCount = RecyclableDefaults.MinPooledArrayLength) { if (source is T[] sourceArray) { @@ -420,29 +421,16 @@ namespace Recyclable.Collections public void Clear() { - try + ArrayPool blockArrayPool = _blockArrayPool; + Span memoryBlocksSpan = new(_memoryBlocks); + int memoryBlocksCount = memoryBlocksSpan.Length; + for (var toRemoveIdx = 0; toRemoveIdx < memoryBlocksCount; toRemoveIdx++) { - // Remove in reversed order for performance savings - 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. - } + memoryBlocksSpan[toRemoveIdx].ReturnToPool(blockArrayPool); + } - toRemoveIdx--; - } - } - finally - { - _longCount = 0; - } + _capacity = 0; + _longCount = 0; } 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 long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(_blockSize, item, _equalityComparer); 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(long index) => RemoveAt(this, index); IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator(); - private static void ReleaseBlock(RecyclableList owner, in T[][] memoryBlocks, int blockSize, int blockIndex) - { - try - { - memoryBlocks[blockIndex].ReturnToPool(_blockArrayPool); - } - finally - { - owner._capacity -= blockSize; - } - } - public void Dispose() { if (_capacity > 0)