RecyclableList.IndexOf performance improvements.

This commit is contained in:
Marek Lemańczyk committed 2023-03-05 14:33:43 +01:00
1 parent 9676ee7485
commit a50ef10989
1 file changed
+16 -14
+16 -14
View File
@@ -546,23 +546,25 @@ namespace Recyclable.Collections
return ItemNotFoundIndex; return ItemNotFoundIndex;
} }
int itemIndex, blockIndex = 0, lastBlockIndex = _lastBlockIndex, blockSize = _blockSize; if (_lastBlockIndex == 0 || (_lastBlockIndex == 1 && _nextItemIndex == 0))
Span<T[]> memoryBlocksSpan = new(_memoryBlocks);
while (blockIndex < lastBlockIndex)
{ {
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex], item, 0, blockSize); return Array.IndexOf(_memoryBlocks[0], item, 0, (int)_longCount);
if (itemIndex >= 0)
{
return (blockIndex * blockSize) + itemIndex;
}
blockIndex++;
} }
itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex], item, 0, _nextItemIndex); Span<T[]> memoryBlocksSpan = new(_memoryBlocks);
return itemIndex >= 0 const int blockIndex = 0, lastBlockIndex = 1, itemIndex = 2, blockSize = 3;
? (blockIndex * blockSize) + itemIndex Span<int> localVars = stackalloc int[4] { 0, _lastBlockIndex, ItemNotFoundIndex, _blockSize };
: ItemNotFoundIndex; for (localVars[blockIndex] = 0; localVars[blockIndex] < localVars[lastBlockIndex]; localVars[blockIndex]++)
{
localVars[itemIndex] = Array.IndexOf(memoryBlocksSpan[localVars[blockIndex]], item, 0, localVars[blockSize]);
if (localVars[itemIndex] >= 0)
{
return localVars[itemIndex] + (localVars[blockIndex] * localVars[blockSize]);
}
}
localVars[itemIndex] = Array.IndexOf(memoryBlocksSpan[localVars[lastBlockIndex]], item, 0, _nextItemIndex);
return localVars[itemIndex] >= 0 ? localVars[itemIndex] + (localVars[blockIndex] * localVars[blockSize]) : ItemNotFoundIndex;
} }
public void Insert(int index, T item) => throw new NotSupportedException(); public void Insert(int index, T item) => throw new NotSupportedException();