Code cleanup - renaming blockSize => minBlockSize to better reflect the use.

This commit is contained in:
mlemanczyk committed 2023-01-29 14:50:47 +01:00
1 parent b01dc63165
commit 463ad3a365
1 file changed
+21 -21
+21 -21
View File
@@ -74,11 +74,11 @@ namespace Recyclable.Collections
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static T[][] SetNewLength(in T[][]? source, int blockSize, in long newCapacity) protected static T[][] SetNewLength(in T[][]? source, int minBlockSize, in long newCapacity)
{ {
ArrayPool<T[]> arrayPool = _arrayPool; ArrayPool<T[]> arrayPool = _arrayPool;
var sourceBlockCount = source?.Length ?? 0; var sourceBlockCount = source?.Length ?? 0;
int requiredBlockCount = (int)(newCapacity / blockSize) + (newCapacity % blockSize > 0 ? 1 : 0); int requiredBlockCount = (int)(newCapacity / minBlockSize) + (newCapacity % minBlockSize > 0 ? 1 : 0);
T[][] newMemoryBlocks = requiredBlockCount < RecyclableDefaults.MinPooledArrayLength T[][] newMemoryBlocks = requiredBlockCount < RecyclableDefaults.MinPooledArrayLength
? new T[requiredBlockCount][] ? new T[requiredBlockCount][]
@@ -101,32 +101,32 @@ namespace Recyclable.Collections
int uninitializedBlocksCount = newMemoryBlocksSpan.Length; int uninitializedBlocksCount = newMemoryBlocksSpan.Length;
if (uninitializedBlocksCount == requiredBlockCount) if (uninitializedBlocksCount == requiredBlockCount)
{ {
if (blockSize >= RecyclableDefaults.MinPooledArrayLength) if (minBlockSize >= RecyclableDefaults.MinPooledArrayLength)
{ {
newMemoryBlocksSpan[0] = blockArrayPool.Rent(blockSize); newMemoryBlocksSpan[0] = blockArrayPool.Rent(minBlockSize);
blockSize = newMemoryBlocksSpan[0].Length; minBlockSize = newMemoryBlocksSpan[0].Length;
} }
else else
{ {
newMemoryBlocksSpan[0] = new T[blockSize]; newMemoryBlocksSpan[0] = new T[minBlockSize];
} }
newMemoryBlocksSpan = newMemoryBlocksSpan[1..]; newMemoryBlocksSpan = newMemoryBlocksSpan[1..];
uninitializedBlocksCount--; uninitializedBlocksCount--;
} }
if (blockSize >= RecyclableDefaults.MinPooledArrayLength) if (minBlockSize >= RecyclableDefaults.MinPooledArrayLength)
{ {
for (int i = 0; i < uninitializedBlocksCount; i++) for (int i = 0; i < uninitializedBlocksCount; i++)
{ {
newMemoryBlocksSpan[i] = blockArrayPool.Rent(blockSize); newMemoryBlocksSpan[i] = blockArrayPool.Rent(minBlockSize);
} }
} }
else else
{ {
for (int i = 0; i < uninitializedBlocksCount; i++) for (int i = 0; i < uninitializedBlocksCount; i++)
{ {
newMemoryBlocksSpan[i] = new T[blockSize]; newMemoryBlocksSpan[i] = new T[minBlockSize];
} }
} }
@@ -163,42 +163,42 @@ namespace Recyclable.Collections
return newCapacity; return newCapacity;
} }
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) public RecyclableList(int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default)
{ {
if (expectedItemsCount > 0) if (expectedItemsCount > 0)
{ {
_memoryBlocks = SetNewLength(_memoryBlocks, blockSize, expectedItemsCount.Value); _memoryBlocks = SetNewLength(_memoryBlocks, minBlockSize, expectedItemsCount.Value);
if (_memoryBlocks.Length > 0) if (_memoryBlocks.Length > 0)
{ {
blockSize = _memoryBlocks[0].Length; minBlockSize = _memoryBlocks[0].Length;
} }
_blockSize = blockSize; _blockSize = minBlockSize;
_capacity = _memoryBlocks.Length * blockSize; _capacity = _memoryBlocks.Length * minBlockSize;
} }
else else
{ {
_blockSize = blockSize; _blockSize = minBlockSize;
_memoryBlocks = _emptyMemoryBlocksArray; _memoryBlocks = _emptyMemoryBlocksArray;
} }
} }
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) public RecyclableList(IEnumerable<T> source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default)
{ {
if (expectedItemsCount > 0) if (expectedItemsCount > 0)
{ {
_memoryBlocks = SetNewLength(_memoryBlocks, blockSize, expectedItemsCount.Value); _memoryBlocks = SetNewLength(_memoryBlocks, minBlockSize, expectedItemsCount.Value);
if (_memoryBlocks.Length > 0) if (_memoryBlocks.Length > 0)
{ {
blockSize = _memoryBlocks[0].Length; minBlockSize = _memoryBlocks[0].Length;
} }
_blockSize = blockSize; _blockSize = minBlockSize;
_capacity = _memoryBlocks.Length * blockSize; _capacity = _memoryBlocks.Length * minBlockSize;
} }
else else
{ {
_blockSize = blockSize; _blockSize = minBlockSize;
_memoryBlocks = _emptyMemoryBlocksArray; _memoryBlocks = _emptyMemoryBlocksArray;
} }