We'll now use Span<T> instead of memory to eliminate 1 more local var.
This commit is contained in:
1 parent
3d6536beea
commit
3cfe0b7e79
1 file changed
+7
-6
@@ -83,6 +83,7 @@ namespace Recyclable.Collections
|
|||||||
var sourceBlockCount = source?.Length ?? 0;
|
var sourceBlockCount = source?.Length ?? 0;
|
||||||
int requiredBlockCount = (int)(newCapacity / blockSize) + (newCapacity % blockSize > 0 ? 1 : 0);
|
int requiredBlockCount = (int)(newCapacity / blockSize) + (newCapacity % blockSize > 0 ? 1 : 0);
|
||||||
|
|
||||||
|
Span<T[]> sourceSpan;
|
||||||
Span<T[]> newArraySpan;
|
Span<T[]> newArraySpan;
|
||||||
T[][] newMemoryBlocks = requiredBlockCount >= RecyclableDefaults.MinPooledArrayLength
|
T[][] newMemoryBlocks = requiredBlockCount >= RecyclableDefaults.MinPooledArrayLength
|
||||||
? arrayPool.Rent(requiredBlockCount)
|
? arrayPool.Rent(requiredBlockCount)
|
||||||
@@ -94,15 +95,15 @@ namespace Recyclable.Collections
|
|||||||
switch (sourceBlockCount > 0)
|
switch (sourceBlockCount > 0)
|
||||||
{
|
{
|
||||||
case true:
|
case true:
|
||||||
Memory<T[]> sourceMemory = new(source);
|
sourceSpan = new Span<T[]>(source);
|
||||||
Memory<T[]> newArrayMemory = new(newMemoryBlocks);
|
newArraySpan = new Span<T[]>(newMemoryBlocks);
|
||||||
sourceMemory.CopyTo(newArrayMemory);
|
sourceSpan.CopyTo(newArraySpan);
|
||||||
if (sourceMemory.Length >= RecyclableDefaults.MinPooledArrayLength)
|
if (sourceSpan.Length >= RecyclableDefaults.MinPooledArrayLength)
|
||||||
{
|
{
|
||||||
arrayPool.Return(source!);
|
arrayPool.Return(source!);
|
||||||
}
|
}
|
||||||
|
|
||||||
newArraySpan = new Span<T[]>(newMemoryBlocks)[sourceBlockCount..];
|
newArraySpan = newArraySpan[sourceBlockCount..];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case false:
|
case false:
|
||||||
@@ -151,7 +152,7 @@ namespace Recyclable.Collections
|
|||||||
return newMemoryBlocks;
|
return newMemoryBlocks;
|
||||||
|
|
||||||
case false:
|
case false:
|
||||||
var sourceSpan = new Span<T[]>(source)[..requiredBlockCount];
|
sourceSpan = new Span<T[]>(source)[..requiredBlockCount];
|
||||||
newArraySpan = new Span<T[]>(newMemoryBlocks);
|
newArraySpan = new Span<T[]>(newMemoryBlocks);
|
||||||
sourceSpan.CopyTo(newArraySpan);
|
sourceSpan.CopyTo(newArraySpan);
|
||||||
sourceSpan = new Span<T[]>(source)[newArraySpan.Length..];
|
sourceSpan = new Span<T[]>(source)[newArraySpan.Length..];
|
||||||
|
|||||||
Reference in new issue
Block a user