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

This commit is contained in:
mlemanczyk committed 2023-01-29 18:04:25 +01:00
1 parent b01dc63165
commit ea29108731
2 files changed
+23 -23

No files matched your search

@@ -14,7 +14,7 @@ namespace Recyclable.Collections.Benchmarks
[Benchmark] [Benchmark]
public void RecyclableList_Create_WithCapacity() public void RecyclableList_Create_WithCapacity()
{ {
using var list = new RecyclableList<object>(blockSize: 102_400, expectedItemsCount: TestObjectCount); using var list = new RecyclableList<object>(minBlockSize: 102_400, expectedItemsCount: TestObjectCount);
DoNothing(list); DoNothing(list);
} }
@@ -35,7 +35,7 @@ namespace Recyclable.Collections.Benchmarks
{ {
var data = TestObjects; var data = TestObjects;
var dataCount = TestObjectCount; var dataCount = TestObjectCount;
using var list = new RecyclableList<object>(blockSize: 102_400, expectedItemsCount: dataCount); using var list = new RecyclableList<object>(minBlockSize: 102_400, expectedItemsCount: dataCount);
for (var i = 0; i < dataCount; i++) for (var i = 0; i < dataCount; i++)
{ {
list.Add(data[i]); list.Add(data[i]);
+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;
} }