From 44c8286f85512bd03d41e41b30b1969672ef9d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sat, 11 Mar 2023 19:38:50 +0100 Subject: [PATCH] RecyclableList.RecyclableList.AddRange(RecyclableArrayList items) fixed to break the loop when we reach the last block. --- Recyclable.Collections/RecyclableList.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 6609e9e..cf9fccb 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -315,15 +315,23 @@ namespace Recyclable.Collections _ = EnsureCapacity(targetCapacity); } - int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex; + int blockSize = _blockSize, + targetBlockIndex = _lastBlockIndex, + memoryBlockCount = _memoryBlocks.Length; + Span itemsSpan = new(items.AsArray(), 0, items.Count); - Span memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length); + Span memoryBlocksSpan = new(_memoryBlocks, 0, memoryBlockCount); Span targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], _nextItemIndex, blockSize - _nextItemIndex); while (targetBlockArraySpan.Length <= itemsSpan.Length) { itemsSpan[..targetBlockArraySpan.Length].CopyTo(targetBlockArraySpan); itemsSpan = itemsSpan[targetBlockArraySpan.Length..]; targetBlockIndex++; + if (targetBlockIndex == memoryBlockCount) + { + break; + } + targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], 0, blockSize); }