From b9933d377b303037ba7de4cf16757dc1ca115c36 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 16 Sep 2023 20:24:32 +0200 Subject: [PATCH] Small cleanup --- .../AddRangeWhenSourceIsSameType.cs | 2 +- .../Benchmarks/AddRange_WithCapacity.cs | 2 +- .../Benchmarks/Add_WithCapacity.cs | 2 +- .../Benchmarks/Create_WithCapacity.cs | 2 +- .../Benchmarks/RemoveAt_FirstItems.cs | 2 +- .../Benchmarks/RemoveAt_LastItems.cs | 2 +- .../Benchmarks/Remove_FirstItems.cs | 2 +- .../Benchmarks/Remove_LastItems.cs | 2 +- ...leLongListPoc.Benchmarks.EnsureCapacity.cs | 30 +++++++++---------- .../RecyclableBenchmarkBase.cs | 2 +- Recyclable.Collections/MathUtils.cs | 8 ----- .../RecyclableList.Helpers.cs | 1 - .../RecyclableLongList.IndexOfHelpers.cs | 2 -- Recyclable.Collections/RecyclableLongList.cs | 18 +++++------ 14 files changed, 33 insertions(+), 44 deletions(-) diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/AddRangeWhenSourceIsSameType.cs b/Recyclable.Collections.Benchmarks/Benchmarks/AddRangeWhenSourceIsSameType.cs index e2c68bf..293192f 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/AddRangeWhenSourceIsSameType.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/AddRangeWhenSourceIsSameType.cs @@ -21,7 +21,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_AddRangeWhenSourceIsRecyclableLongList() { var data = TestObjectsAsRecyclableList; - using var list = new RecyclableLongList(minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(minBlockSize: BlockSize, initialCapacity: TestObjectCount); list.AddRange(data); } } diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/AddRange_WithCapacity.cs b/Recyclable.Collections.Benchmarks/Benchmarks/AddRange_WithCapacity.cs index f57d883..3eac8f4 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/AddRange_WithCapacity.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/AddRange_WithCapacity.cs @@ -40,7 +40,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var dataCount = TestObjectCount; - using var list = new RecyclableLongList(minBlockSize: BlockSize, expectedItemsCount: dataCount); + using var list = new RecyclableLongList(minBlockSize: BlockSize, initialCapacity: dataCount); list.AddRange(data); } } diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Add_WithCapacity.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Add_WithCapacity.cs index 0838d36..7d01ee0 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Add_WithCapacity.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Add_WithCapacity.cs @@ -41,7 +41,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var dataCount = TestObjectCount; - using var list = new RecyclableLongList(minBlockSize: BlockSize, expectedItemsCount: dataCount); + using var list = new RecyclableLongList(minBlockSize: BlockSize, initialCapacity: dataCount); for (var i = 0; i < dataCount; i++) { list.Add(data[i]); diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Create_WithCapacity.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Create_WithCapacity.cs index 372f2da..b27c3ec 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Create_WithCapacity.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Create_WithCapacity.cs @@ -30,7 +30,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_Create_WithCapacity() { - using var list = new RecyclableLongList(minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(minBlockSize: BlockSize, initialCapacity: TestObjectCount); DoNothing(list); } } diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_FirstItems.cs b/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_FirstItems.cs index 6c5f1db..c4f7059 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_FirstItems.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_FirstItems.cs @@ -40,7 +40,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_RemoveAt_FirstItems() { var data = TestObjects; - using var list = new RecyclableLongList(data, minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount); var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount; for (var i = 0; i < dataCount; i++) { diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_LastItems.cs b/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_LastItems.cs index 30e6af3..abbadbb 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_LastItems.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/RemoveAt_LastItems.cs @@ -40,7 +40,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_RemoveAt_LastItems() { var data = TestObjects; - using var list = new RecyclableLongList(data, minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount); var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount; for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--) { diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Remove_FirstItems.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Remove_FirstItems.cs index 3f18e65..659d17f 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Remove_FirstItems.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Remove_FirstItems.cs @@ -40,7 +40,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_Remove_FirstItems() { var data = TestObjects; - using var list = new RecyclableLongList(data, minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(data, minBlockSize: BlockSize, initialCapacity: base.TestObjectCount); var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount; for (var i = 0; i < dataCount; i++) { diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Remove_LastItems.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Remove_LastItems.cs index dfa2bef..af08df1 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Remove_LastItems.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Remove_LastItems.cs @@ -40,7 +40,7 @@ namespace Recyclable.Collections.Benchmarks public void RecyclableLongList_Remove_LastItems() { var data = TestObjects; - using var list = new RecyclableLongList(data, minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(data, minBlockSize: BlockSize, initialCapacity:base.TestObjectCount); var dataCount = TestObjectCount / 10 > 0 ? TestObjectCount / 10 : TestObjectCount; for (var i = data.Length - 1; i > data.Length - dataCount - 1; i--) { diff --git a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs index 599f55c..c337a92 100644 --- a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs +++ b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs @@ -357,16 +357,16 @@ namespace Recyclable.Collections.Benchmarks.POC return newCapacity; } - public RecyclableLongListV1(int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool? memoryBlocksPool = default, ArrayPool? blockArrayPool = default) + public RecyclableLongListV1(int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default, ArrayPool? memoryBlocksPool = default, ArrayPool? blockArrayPool = default) { memoryBlocksPool ??= _defaultMemoryBlocksPool; blockArrayPool ??= _defaultBlockArrayPool; _memoryBlocksPool = memoryBlocksPool; _blockArrayPool = blockArrayPool; - if (expectedItemsCount > 0) + if (initialCapacity > 0) { - _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); + _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, initialCapacity.Value, memoryBlocksPool, blockArrayPool); if (_memoryBlocks!.Length > 0) { minBlockSize = _memoryBlocks[0].Length; @@ -383,16 +383,16 @@ namespace Recyclable.Collections.Benchmarks.POC } } - public RecyclableLongListV1(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool? memoryBlocksPool = default, ArrayPool? blockArrayPool = default) + public RecyclableLongListV1(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default, ArrayPool? memoryBlocksPool = default, ArrayPool? blockArrayPool = default) { memoryBlocksPool ??= _defaultMemoryBlocksPool; blockArrayPool ??= _defaultBlockArrayPool; _memoryBlocksPool = memoryBlocksPool; _blockArrayPool = blockArrayPool; - if (expectedItemsCount > 0) + if (initialCapacity > 0) { - _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); + _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, initialCapacity.Value, memoryBlocksPool, blockArrayPool); if (_memoryBlocks!.Length > 0) { minBlockSize = _memoryBlocks[0].Length; @@ -1241,16 +1241,16 @@ namespace Recyclable.Collections.Benchmarks.POC [MethodImpl(MethodImplOptions.AggressiveOptimization)] #pragma warning disable CS8618 // It's set by SetupBlockArrayPooling - public RecyclableLongListV3(ref OneSizeArrayPool blockArrayPool, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool? memoryBlocksPool = default) + public RecyclableLongListV3(ref OneSizeArrayPool blockArrayPool, int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default, ArrayPool? memoryBlocksPool = default) #pragma warning restore CS8618 { _memoryBlocksPool = memoryBlocksPool ?? _defaultMemoryBlocksPool; - if (expectedItemsCount > 0) + if (initialCapacity > 0) { minBlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize)); SetupBlockArrayPooling(this, minBlockSize, ref blockArrayPool); - _capacity = Resize(this, minBlockSize, _blockSizePow2Shift, expectedItemsCount.Value); + _capacity = Resize(this, minBlockSize, _blockSizePow2Shift, initialCapacity.Value); if (_blockSize != _memoryBlocks![0].Length && _capacity > 0) { SetupBlockArrayPooling(this, _memoryBlocks[0].Length, ref blockArrayPool); @@ -1270,15 +1270,15 @@ namespace Recyclable.Collections.Benchmarks.POC } #pragma warning disable CS8618 // It's set by SetupBlockArrayPooling - public RecyclableLongListV3(ref OneSizeArrayPool blockArrayPool, IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool? memoryBlocksPool = default) + public RecyclableLongListV3(ref OneSizeArrayPool blockArrayPool, IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default, ArrayPool? memoryBlocksPool = default) #pragma warning restore CS8618 { _memoryBlocksPool = memoryBlocksPool ?? _defaultMemoryBlocksPool; - if (expectedItemsCount > 0) + if (initialCapacity > 0) { minBlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize)); SetupBlockArrayPooling(this, minBlockSize, ref blockArrayPool); - _capacity = Resize(this, minBlockSize, _blockSizePow2Shift, expectedItemsCount.Value); + _capacity = Resize(this, minBlockSize, _blockSizePow2Shift, initialCapacity.Value); if (minBlockSize != _memoryBlocks![0].Length && _capacity > 0) { SetupBlockArrayPooling(this, _memoryBlocks[0].Length, ref blockArrayPool); @@ -1843,19 +1843,19 @@ namespace Recyclable.Collections.Benchmarks.POC { public void RecyclableLongList_EnsureCapacityV1_ByPowOf2() { - using var list = new RecyclableLongListV1(minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongListV1(minBlockSize: BlockSize, initialCapacity: TestObjectCount); DoNothing(_ensureCapacityV1Func.Invoke(list, new object[] { TestObjectCount })); } public void RecyclableLongList_EnsureCapacityV3_ByPowOf2() { - using var list = new RecyclableLongListV3(ref RecyclableArrayPool._pools[BlockSize], minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongListV3(ref RecyclableArrayPool._pools[BlockSize], minBlockSize: BlockSize, initialCapacity: TestObjectCount); DoNothing(_ensureCapacityV3Func.Invoke(null, new object[] { list, TestObjectCount })); } public void RecyclableLongList_EnsureCapacity_ByPowOf2() { - using var list = new RecyclableLongList(minBlockSize: BlockSize, expectedItemsCount: TestObjectCount); + using var list = new RecyclableLongList(minBlockSize: BlockSize, initialCapacity: TestObjectCount); DoNothing(_ensureCapacityNewFunc.Invoke(null, new object[] { list, TestObjectCount })); } diff --git a/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs b/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs index 83f6eec..3dbfcd2 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs @@ -72,7 +72,7 @@ namespace Recyclable.Collections.Benchmarks break; case RecyclableCollectionsBenchmarkSource.RecyclableLongList: - _testObjectsAsRecyclableLongList ??= new(TestObjects, BlockSize, expectedItemsCount: TestObjectCount); + _testObjectsAsRecyclableLongList ??= new(TestObjects, BlockSize, initialCapacity: base.TestObjectCount); break; default: diff --git a/Recyclable.Collections/MathUtils.cs b/Recyclable.Collections/MathUtils.cs index 0fe8e7a..d82edc1 100644 --- a/Recyclable.Collections/MathUtils.cs +++ b/Recyclable.Collections/MathUtils.cs @@ -5,14 +5,6 @@ namespace Recyclable.Collections { internal static class MathUtils { - // private static readonly long[] Pow2 = new long[] - // { - // 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, - // 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, - // 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, - // 536870912, 1073741824, 2147483648 - // }; - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsPow2(long value) => (value != 0) && ((value & (value - 1)) == 0); diff --git a/Recyclable.Collections/RecyclableList.Helpers.cs b/Recyclable.Collections/RecyclableList.Helpers.cs index 4b84942..dd64d43 100644 --- a/Recyclable.Collections/RecyclableList.Helpers.cs +++ b/Recyclable.Collections/RecyclableList.Helpers.cs @@ -124,7 +124,6 @@ namespace Recyclable.Collections // Array.Clear(source); //} - // // If anything, it has been already cleared above, so we don't need to repeat it. RecyclableArrayPool.ReturnShared(oldMemoryBlock, NeedsClearing); } } diff --git a/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs b/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs index 21826af..8bc01d3 100644 --- a/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs +++ b/Recyclable.Collections/RecyclableLongList.IndexOfHelpers.cs @@ -344,8 +344,6 @@ namespace Recyclable.Collections return itemIndex >= 0 ? itemIndex + ((long)list._lastBlockWithData << list._blockSizePow2BitShift) : RecyclableDefaults.ItemNotFoundIndexLong; } - - public static int Min(int a, int b, int c) => a < b ? a < c ? a : c : b < c ? b : c; } } } diff --git a/Recyclable.Collections/RecyclableLongList.cs b/Recyclable.Collections/RecyclableLongList.cs index b5310ec..6c3dfb7 100644 --- a/Recyclable.Collections/RecyclableLongList.cs +++ b/Recyclable.Collections/RecyclableLongList.cs @@ -167,14 +167,14 @@ namespace Recyclable.Collections } [MethodImpl(MethodImplOptions.AggressiveOptimization)] - public RecyclableLongList(int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) + public RecyclableLongList(int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default) { minBlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize)); - if (expectedItemsCount > 0) + if (initialCapacity > 0) { Helpers.SetupBlockArrayPooling(this, minBlockSize); - _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((long)BitOperations.RoundUpToPowerOf2((ulong)expectedItemsCount.Value))); + _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((long)BitOperations.RoundUpToPowerOf2((ulong)initialCapacity.Value))); _blockSizeMinus1 = minBlockSize - 1; } else @@ -295,12 +295,12 @@ namespace Recyclable.Collections AddRange(source); } - public RecyclableLongList(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) + public RecyclableLongList(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default) { Helpers.SetupBlockArrayPooling(this, minBlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize))); - if (expectedItemsCount > 0) + if (initialCapacity > 0) { - _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((int)BitOperations.RoundUpToPowerOf2((uint)expectedItemsCount.Value))); + _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((int)BitOperations.RoundUpToPowerOf2((uint)initialCapacity.Value))); _blockSizeMinus1 = minBlockSize - 1; } else @@ -312,12 +312,12 @@ namespace Recyclable.Collections AddRange(source); } - public RecyclableLongList(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) + public RecyclableLongList(IEnumerable source, int minBlockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default) { Helpers.SetupBlockArrayPooling(this, minBlockSize = checked((int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize))); - if (expectedItemsCount > 0) + if (initialCapacity > 0) { - _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((int)BitOperations.RoundUpToPowerOf2((uint)expectedItemsCount.Value))); + _capacity = Helpers.Resize(this, minBlockSize, _blockSizePow2BitShift, checked((int)BitOperations.RoundUpToPowerOf2((uint)initialCapacity.Value))); _blockSizeMinus1 = minBlockSize - 1; } else