diff --git a/Recyclable.Collections.Benchmarks/ListVsLinkedListBenchmarks.cs b/Recyclable.Collections.Benchmarks/BenchmarkBase.cs similarity index 85% rename from Recyclable.Collections.Benchmarks/ListVsLinkedListBenchmarks.cs rename to Recyclable.Collections.Benchmarks/BenchmarkBase.cs index d2d36f7..f5dcafe 100644 --- a/Recyclable.Collections.Benchmarks/ListVsLinkedListBenchmarks.cs +++ b/Recyclable.Collections.Benchmarks/BenchmarkBase.cs @@ -1,10 +1,12 @@ -namespace Recyclable.Collections.Benchmarks +using BenchmarkDotNet.Attributes; + +namespace Recyclable.Collections.Benchmarks { public class BenchmarkBase { - //[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)] - public const int TestObjectCount = 1_000_000; - public static int BlockSize = TestObjectCount switch + [Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)] + public int TestObjectCount; + public int BlockSize => TestObjectCount switch { 0 => 1, > 0 and <= 10 => TestObjectCount, @@ -14,12 +16,12 @@ protected object[]? _testObjects; protected object[] TestObjects => _testObjects ??= Enumerable.Range(1, TestObjectCount).Select(x => new object()).ToArray(); - private List? _testObjectsAsList; + protected List? _testObjectsAsList; protected List TestObjectsAsList => _testObjectsAsList ??= TestObjects.ToList(); - private RecyclableArrayList? _testObjectsAsRecyclableArrayList; + protected RecyclableArrayList? _testObjectsAsRecyclableArrayList; protected RecyclableArrayList TestObjectsAsRecyclableArrayList => _testObjectsAsRecyclableArrayList ??= TestObjects.ToRecyclableArrayList(); - protected IEnumerable TestObjectsAsIEnumerable = Enumerable.Range(1, TestObjectCount).Select(static x => new object()); + protected IEnumerable TestObjectsAsIEnumerable => Enumerable.Range(1, TestObjectCount).Select(static x => new object()); protected static void DoNothing(T obj) { diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.Array.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.Array.cs index 2712b7c..cab42a8 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.Array.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.Array.cs @@ -5,16 +5,16 @@ namespace Recyclable.Collections.Benchmarks [MemoryDiagnoser] public partial class RecyclableCollectionsBenchmarks : BenchmarkBase { - private readonly object[] _testArray; + private object[] _testArray; - //[Benchmark(Baseline = false)] + [Benchmark(Baseline = true)] public void Array_Create_WithCapacity() { var data = new object[TestObjectCount]; DoNothing(data); } - [Benchmark(Baseline = true)] + //[Benchmark(Baseline = true)] public void Array_SetItem() { var data = TestObjects; diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs index 83ee075..84387a1 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs @@ -4,9 +4,7 @@ namespace Recyclable.Collections.Benchmarks { public partial class RecyclableCollectionsBenchmarks : BenchmarkBase { - private readonly List _testList; - - //[Benchmark(Baseline = false)] + //[Benchmark(Baseline = true)] public void List_Create() { var list = new List(); @@ -20,7 +18,7 @@ namespace Recyclable.Collections.Benchmarks DoNothing(list); } - //[Benchmark] + [Benchmark] public void List_Create_WithCapacity() { var list = new List(TestObjectCount); @@ -75,7 +73,7 @@ namespace Recyclable.Collections.Benchmarks list.AddRange(data); } - [Benchmark] + //[Benchmark] public void List_AddRangeWhenSourceIsIEnumerable() { var data = TestObjectsAsIEnumerable; @@ -117,7 +115,7 @@ namespace Recyclable.Collections.Benchmarks //[Benchmark] public void List_Count() { - var data = _testList; + var data = TestObjectsAsList; DoNothing(data.Count); } } diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableArrayList.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableArrayList.cs index a405506..fac626f 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableArrayList.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableArrayList.cs @@ -4,23 +4,32 @@ namespace Recyclable.Collections.Benchmarks { public partial class RecyclableCollectionsBenchmarks : BenchmarkBase { - private readonly RecyclableArrayList _testRecyclableArrayList; + private RecyclableArrayList _testRecyclableArrayList; - public RecyclableCollectionsBenchmarks() + [GlobalSetup] + public void Setup() { _testRecyclableArrayList = new(TestObjects, TestObjectCount); _testRecyclableList = new(TestObjects, TestObjectCount); _testArray = TestObjects.ToArray(); - _testList = new(TestObjects); _ = TestObjectsAsList; _ = TestObjectsAsRecyclableArrayList; + _ = TestObjectsAsIEnumerable; } - ~RecyclableCollectionsBenchmarks() + + [GlobalCleanup] + public void Cleanup() { _testRecyclableArrayList?.Dispose(); + _testRecyclableArrayList = default; _testRecyclableList?.Dispose(); + _testRecyclableList = default; TestObjectsAsRecyclableArrayList?.Dispose(); + _testObjectsAsRecyclableArrayList = default; + _testArray = default; + _testObjectsAsList = default; + _testObjects = default; } //[Benchmark] @@ -37,7 +46,7 @@ namespace Recyclable.Collections.Benchmarks DoNothing(list); } - //[Benchmark] + [Benchmark] public void RecyclableArrayList_Create_WithCapacity() { using var list = new RecyclableArrayList(TestObjectCount); @@ -92,7 +101,7 @@ namespace Recyclable.Collections.Benchmarks list.AddRange(data); } - [Benchmark] + //[Benchmark] public void RecyclableArrayList_AddRangeWhenSourceIsIEnumerable() { var data = TestObjectsAsIEnumerable; @@ -142,7 +151,7 @@ namespace Recyclable.Collections.Benchmarks //[Benchmark] public void RecyclableArrayList_Count() { - var data = _testRecyclableArrayList; + var data = TestObjectsAsRecyclableArrayList; DoNothing(data.Count); } } diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs index d6a166c..89f305b 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs @@ -4,7 +4,7 @@ namespace Recyclable.Collections.Benchmarks { public partial class RecyclableCollectionsBenchmarks : BenchmarkBase { - private readonly RecyclableList _testRecyclableList; + private RecyclableList _testRecyclableList; //[Benchmark] public void RecyclableList_Create() diff --git a/Recyclable.Collections/RecyclableArrayList_CreateBenchmarks.png b/Recyclable.Collections/RecyclableArrayList_CreateBenchmarks.png index b70bdfd..fd59fd8 100644 Binary files a/Recyclable.Collections/RecyclableArrayList_CreateBenchmarks.png and b/Recyclable.Collections/RecyclableArrayList_CreateBenchmarks.png differ diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 1c49bdd..45e6182 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -70,6 +70,11 @@ namespace Recyclable.Collections private static RecyclableArrayList SetupMemoryBlocks(RecyclableList owner, int blockSize, long? totalItemsCount) { + if (blockSize <= 0) + { + return new(); + } + totalItemsCount ??= 8 * blockSize; int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0; int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray;