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