RecyclableList.AddRange(List<T> items) performance improvements
This commit is contained in:
1 parent
8dfdc6f088
commit
a1187a06a4
1 file changed
+60
-28
@@ -390,7 +390,6 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
|
|
||||||
_longCount = targetCapacity;
|
_longCount = targetCapacity;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddRange(List<T> items)
|
public void AddRange(List<T> items)
|
||||||
@@ -401,39 +400,72 @@ namespace Recyclable.Collections
|
|||||||
_ = EnsureCapacity(targetCapacity);
|
_ = EnsureCapacity(targetCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex, toCopy = 0,
|
int blockSize = _blockSize,
|
||||||
copiedCount = 0, targetItemIndex = _nextItemIndex, totalItemsCount = items.Count;
|
targetBlockIndex = _lastBlockIndex,
|
||||||
|
copied = blockSize - _nextItemIndex,
|
||||||
|
itemsCount = items.Count;
|
||||||
|
|
||||||
Span<T[]> memoryBlocksSpan = new(_memoryBlocks);
|
Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length);
|
||||||
T[] targetBlockArray = memoryBlocksSpan[targetBlockIndex];
|
items.CopyTo(0, memoryBlocksSpan[targetBlockIndex++], _nextItemIndex, copied);
|
||||||
while (true)
|
while (blockSize <= itemsCount - copied)
|
||||||
{
|
{
|
||||||
if (blockSize - targetItemIndex <= totalItemsCount - copiedCount)
|
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex++], 0, blockSize);
|
||||||
{
|
copied += blockSize;
|
||||||
toCopy = blockSize - targetItemIndex;
|
}
|
||||||
items.CopyTo(copiedCount, targetBlockArray, targetItemIndex, toCopy);
|
|
||||||
targetItemIndex = 0;
|
|
||||||
targetBlockIndex++;
|
|
||||||
if (targetBlockIndex > memoryBlocksSpan.Length)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
targetBlockArray = memoryBlocksSpan[targetBlockIndex];
|
if (itemsCount - copied != 0)
|
||||||
copiedCount += toCopy;
|
{
|
||||||
}
|
_nextItemIndex = itemsCount - copied;
|
||||||
else
|
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex], 0, _nextItemIndex);
|
||||||
{
|
}
|
||||||
toCopy = totalItemsCount - copiedCount;
|
else
|
||||||
items.CopyTo(copiedCount, targetBlockArray, targetItemIndex, toCopy);
|
{
|
||||||
targetItemIndex = toCopy;
|
_nextItemIndex = 0;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_longCount = targetCapacity;
|
|
||||||
_lastBlockIndex = targetBlockIndex;
|
_lastBlockIndex = targetBlockIndex;
|
||||||
_nextItemIndex = targetItemIndex;
|
_longCount = targetCapacity;
|
||||||
|
|
||||||
|
// VERSION 2
|
||||||
|
//long targetCapacity = _longCount + items.Count;
|
||||||
|
//if (_capacity < targetCapacity)
|
||||||
|
//{
|
||||||
|
// _ = EnsureCapacity(targetCapacity);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex, toCopy,
|
||||||
|
// copiedCount = 0, targetItemIndex = _nextItemIndex, totalItemsCount = items.Count;
|
||||||
|
|
||||||
|
//Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length);
|
||||||
|
//T[] targetBlockArray = memoryBlocksSpan[targetBlockIndex];
|
||||||
|
//while (true)
|
||||||
|
//{
|
||||||
|
// if (blockSize - targetItemIndex <= totalItemsCount - copiedCount)
|
||||||
|
// {
|
||||||
|
// toCopy = blockSize - targetItemIndex;
|
||||||
|
// items.CopyTo(copiedCount, targetBlockArray, targetItemIndex, toCopy);
|
||||||
|
// targetItemIndex = 0;
|
||||||
|
// targetBlockIndex++;
|
||||||
|
// if (targetBlockIndex > memoryBlocksSpan.Length)
|
||||||
|
// {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// targetBlockArray = memoryBlocksSpan[targetBlockIndex];
|
||||||
|
// copiedCount += toCopy;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// toCopy = totalItemsCount - copiedCount;
|
||||||
|
// items.CopyTo(copiedCount, targetBlockArray, targetItemIndex, toCopy);
|
||||||
|
// targetItemIndex = toCopy;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//_longCount = targetCapacity;
|
||||||
|
//_lastBlockIndex = targetBlockIndex;
|
||||||
|
//_nextItemIndex = targetItemIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddRange(in IList<T> items)
|
public void AddRange(in IList<T> items)
|
||||||
|
|||||||
Reference in new issue
Block a user