diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 91dcc24..03a5ae2 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -299,7 +299,7 @@ namespace Recyclable.Collections _lastBlockIndex = targetBlockIndex; } - public void AddRange(in RecyclableArrayList items) + public void AddRange(RecyclableArrayList items) { long targetCapacity = _longCount + items.Count; if (_capacity < targetCapacity) @@ -307,30 +307,20 @@ namespace Recyclable.Collections _ = EnsureCapacity(targetCapacity); } - int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex, toCopy = 0; - Span itemsSpan = new(items.AsArray()); - Span memoryBlocksSpan = new(_memoryBlocks); + int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex; + Span itemsSpan = new(items.AsArray(), 0, items.Count); + Span memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length); Span targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], _nextItemIndex, blockSize - _nextItemIndex); - while (!itemsSpan.IsEmpty) + while (targetBlockArraySpan.Length <= itemsSpan.Length) { - if (targetBlockArraySpan.Length < itemsSpan.Length) - { - toCopy = targetBlockArraySpan.Length; - itemsSpan[..toCopy].CopyTo(targetBlockArraySpan); - targetBlockIndex++; - targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex]); - } - else - { - toCopy = itemsSpan.Length; - itemsSpan[..toCopy].CopyTo(targetBlockArraySpan); - targetBlockArraySpan = targetBlockArraySpan[..toCopy]; - } - - itemsSpan = itemsSpan[toCopy..]; + itemsSpan[..targetBlockArraySpan.Length].CopyTo(targetBlockArraySpan); + itemsSpan = itemsSpan[targetBlockArraySpan.Length..]; + targetBlockIndex++; + targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], 0, blockSize); } - _nextItemIndex = toCopy; + _nextItemIndex = itemsSpan.Length; + itemsSpan[..itemsSpan.Length].CopyTo(targetBlockArraySpan); _longCount = targetCapacity; _lastBlockIndex = targetBlockIndex; }