Performance improvement - removed unneeded case. We never hit it in our benchmarks or tests. We'll use Memory<T> instead of Span<T> again, where applicable.

This commit is contained in:
mlemanczyk committed 2023-01-28 23:54:53 +01:00
1 parent 06ef3d4fa4
commit 1b3c24bf74
2 files changed
+59 -100

No files matched your search

+10 -26
View File
@@ -21,38 +21,22 @@ namespace Recyclable.Collections
{
ArrayPool<T> arrayPool = _arrayPool;
var sourceLength = source?.Length ?? 0;
Span<T> sourceSpan, newArraySpan;
T[] newMemoryBlock = newSize >= RecyclableDefaults.MinPooledArrayLength
? arrayPool.Rent(newSize)
: new T[newSize];
switch (newSize >= sourceLength)
if (sourceLength > 0)
{
case true:
if (sourceLength > 0)
{
sourceSpan = new Span<T>(source);
newArraySpan = new Span<T>(newMemoryBlock);
sourceSpan.CopyTo(newArraySpan);
if (sourceLength >= RecyclableDefaults.MinPooledArrayLength)
{
arrayPool.Return(source!);
}
}
return newMemoryBlock;
case false:
sourceSpan = new Span<T>(source)[..newSize];
newArraySpan = new(newMemoryBlock);
sourceSpan.CopyTo(newArraySpan);
if (sourceLength >= RecyclableDefaults.MinPooledArrayLength)
{
arrayPool.Return(source!);
}
return newMemoryBlock;
var sourceSpan = new Memory<T>(source);
var newArraySpan = new Memory<T>(newMemoryBlock);
sourceSpan.CopyTo(newArraySpan);
if (sourceLength >= RecyclableDefaults.MinPooledArrayLength)
{
arrayPool.Return(source!);
}
}
return newMemoryBlock;
}
protected int EnsureCapacity(in int requestedCapacity)
+49 -74
View File
@@ -88,89 +88,64 @@ namespace Recyclable.Collections
? arrayPool.Rent(requiredBlockCount)
: new T[requiredBlockCount][];
switch (requiredBlockCount >= sourceBlockCount)
switch (sourceBlockCount > 0)
{
case true:
switch (sourceBlockCount > 0)
{
case true:
sourceSpan = new Span<T[]>(source);
newArraySpan = new Span<T[]>(newMemoryBlocks);
sourceSpan.CopyTo(newArraySpan);
if (sourceSpan.Length >= RecyclableDefaults.MinPooledArrayLength)
{
arrayPool.Return(source!);
}
newArraySpan = newArraySpan[sourceBlockCount..];
break;
case false:
newArraySpan = new Span<T[]>(newMemoryBlocks);
break;
}
int uninitializedBlocksCount = newArraySpan.Length;
if (uninitializedBlocksCount == requiredBlockCount)
{
switch (blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
case true:
newArraySpan[0] = blockArrayPool.Rent(blockSize);
blockSize = newArraySpan[0].Length;
break;
case false:
newArraySpan[0] = new T[blockSize];
break;
}
newArraySpan = newArraySpan[1..];
uninitializedBlocksCount--;
}
switch (blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
case true:
for (int i = 0; i < uninitializedBlocksCount; i++)
{
newArraySpan[i] = blockArrayPool.Rent(blockSize);
}
break;
case false:
for (int i = 0; i < uninitializedBlocksCount; i++)
{
newArraySpan[i] = new T[blockSize];
}
break;
}
return newMemoryBlocks;
case false:
sourceSpan = new Span<T[]>(source)[..requiredBlockCount];
sourceSpan = new Span<T[]>(source);
newArraySpan = new Span<T[]>(newMemoryBlocks);
sourceSpan.CopyTo(newArraySpan);
sourceSpan = new Span<T[]>(source)[newArraySpan.Length..];
int sourceLength = sourceSpan.Length;
if (blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
for (int i = 0; i < sourceLength; i++)
{
blockArrayPool.Return(sourceSpan[i]);
}
}
if (sourceLength >= RecyclableDefaults.MinPooledArrayLength)
if (sourceSpan.Length >= RecyclableDefaults.MinPooledArrayLength)
{
arrayPool.Return(source!);
}
return newMemoryBlocks;
newArraySpan = newArraySpan[sourceBlockCount..];
break;
case false:
newArraySpan = new Span<T[]>(newMemoryBlocks);
break;
}
int uninitializedBlocksCount = newArraySpan.Length;
if (uninitializedBlocksCount == requiredBlockCount)
{
switch (blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
case true:
newArraySpan[0] = blockArrayPool.Rent(blockSize);
blockSize = newArraySpan[0].Length;
break;
case false:
newArraySpan[0] = new T[blockSize];
break;
}
newArraySpan = newArraySpan[1..];
uninitializedBlocksCount--;
}
switch (blockSize >= RecyclableDefaults.MinPooledArrayLength)
{
case true:
for (int i = 0; i < uninitializedBlocksCount; i++)
{
newArraySpan[i] = blockArrayPool.Rent(blockSize);
}
break;
case false:
for (int i = 0; i < uninitializedBlocksCount; i++)
{
newArraySpan[i] = new T[blockSize];
}
break;
}
return newMemoryBlocks;
}
protected long EnsureCapacity(in long requestedCapacity)