Optimize indexing helpers
This commit is contained in:
1 parent
c2affa4ad7
commit
25bf466f77
1 file changed
+9
-8
@@ -1,4 +1,5 @@
|
|||||||
using Recyclable.Collections.Pools;
|
using Recyclable.Collections.Pools;
|
||||||
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Recyclable.Collections
|
namespace Recyclable.Collections
|
||||||
@@ -76,11 +77,11 @@ namespace Recyclable.Collections
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadOnlySpan<T> sourceBlockMemory;
|
ReadOnlySpan<T> sourceBlockMemory;
|
||||||
Span<T> destinationArrayMemory;
|
Span<T> destinationArrayMemory;
|
||||||
// TODO: Replace with bit-shifting
|
int blockSizePow2Shift = BitOperations.TrailingZeroCount((uint)blockSize);
|
||||||
int startingBlockIndex = (int)(startingIndex / blockSize);
|
int startingBlockIndex = (int)(startingIndex >> blockSizePow2Shift);
|
||||||
int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize != 0 ? 1 : 0)) - 1;
|
int lastBlockIndex = (int)((itemsCount >> blockSizePow2Shift) + ((itemsCount & (blockSize - 1)) != 0 ? 1 : 0)) - 1;
|
||||||
ReadOnlySpan<T[]> sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1);
|
ReadOnlySpan<T[]> sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1);
|
||||||
destinationArrayMemory = new Span<T>(destinationArray, destinationArrayIndex, (int)Math.Min(destinationArray.Length - destinationArrayIndex, itemsCount));
|
destinationArrayMemory = new Span<T>(destinationArray, destinationArrayIndex, (int)Math.Min(destinationArray.Length - destinationArrayIndex, itemsCount));
|
||||||
int memoryBlockIndex;
|
int memoryBlockIndex;
|
||||||
@@ -105,9 +106,9 @@ namespace Recyclable.Collections
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Replace with bit-shifting
|
int blockSizePow2Shift = BitOperations.TrailingZeroCount((uint)blockSize);
|
||||||
int startingBlockIndex = (int)(startingIndex / blockSize);
|
int startingBlockIndex = (int)(startingIndex >> blockSizePow2Shift);
|
||||||
int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize != 0 ? 1 : 0)) - 1;
|
int lastBlockIndex = (int)((itemsCount >> blockSizePow2Shift) + ((itemsCount & (blockSize - 1)) != 0 ? 1 : 0)) - 1;
|
||||||
|
|
||||||
var destinationIndex = destinationArrayIndex;
|
var destinationIndex = destinationArrayIndex;
|
||||||
ReadOnlySpan<T[]> sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1);
|
ReadOnlySpan<T[]> sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1);
|
||||||
|
|||||||
Reference in new issue
Block a user