From 1691f07fbeb133d45740908cfbcabe55318f0225 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sun, 22 Jan 2023 23:45:54 +0100 Subject: [PATCH] RecyclableList.AddRange will only call EnsureCapacity, if there is a need for it. Small source code cleanup. --- Recyclable.Collections/RecyclableList.cs | 45 +++++------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 9e3e4dc..0765cb4 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -1,5 +1,4 @@ -using System; -using System.Buffers; +using System.Buffers; using System.Collections; using System.Runtime.CompilerServices; @@ -68,14 +67,6 @@ namespace Recyclable.Collections { return source!; } - //else if (requiredBlockCount < sourceBlockCount) - //{ - // requiredBlockCount = requiredBlockCount > 0 ? requiredBlockCount : 1; - // while (requiredBlockCount < sourceBlockCount) - // { - // requiredBlockCount *= 2; - // } - //} Span newArraySpan; T[][] newMemoryBlocks = requiredBlockCount.RentArrayFromPool(arrayPool); @@ -150,9 +141,7 @@ namespace Recyclable.Collections return newCapacity; } -#pragma warning disable CS8618 // _memoryBlocks will be initialized when the 1st item is added public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default) -#pragma warning restore CS8618 { _blockSize = blockSize; if (initialCapacity > 0) @@ -166,9 +155,7 @@ namespace Recyclable.Collections } } -#pragma warning disable CS8618 // _memoryBlocks will be initialized when the 1st item is added public RecyclableList(IEnumerable source, int blockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default) -#pragma warning restore CS8618 { _blockSize = blockSize; if (expectedItemsCount > 0) @@ -351,7 +338,13 @@ namespace Recyclable.Collections Span blockArraySpan; if (source.TryGetNonEnumeratedCount(out var requiredAdditionalCapacity)) { - _ = EnsureCapacity(targetItemIdx + requiredAdditionalCapacity); + + int requiredCapacity = targetItemIdx + requiredAdditionalCapacity; + if (capacity < requiredCapacity) + { + _ = EnsureCapacity(requiredCapacity); + } + memoryBlocksSpan = new(_memoryBlocks); var memoryBlocksCount = memoryBlocksSpan.Length; blockArraySpan = new Span(memoryBlocksSpan[targetBlockIdx]); @@ -480,28 +473,6 @@ 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; - // RecyclableArrayList memoryBlocks = new(memoryBlockCount); - // for (var blockIdx = 0; blockIdx < memoryBlockCount; blockIdx++) - // { - // memoryBlocks[blockIdx] = RentArrayFromPool(blockSize); - // } - - // memoryBlocks.Count = memoryBlockCount; - // owner.Capacity = totalItemsCount.Value; - // return memoryBlocks; - //} - - public void Dispose() { if (_capacity > 0)