Pools renamed to FixedCapacityPools. Other fixed capacity collections added. Queues, concurrent queues and standard stacks replaced with fixed capacity stacks. MethodImplOptions.AggressiveInlining and .AggressiveOptimization added to simple methods and getters / setters. Variables optimized in few places.
This commit is contained in:
1 parent
547d43de84
commit
260aa19e44
25 files changed
+1372
-1300
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace PerfectNumbers.Core
|
||||
{
|
||||
public struct FixedCapacityArrayPool<T>(int poolCapacity)
|
||||
{
|
||||
private readonly T[][] _arrays = new T[poolCapacity][];
|
||||
private int _count = 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public T[] Rent(int length)
|
||||
{
|
||||
int available = _count;
|
||||
if (available == 0)
|
||||
{
|
||||
return new T[length];
|
||||
}
|
||||
|
||||
T[][] arrays = _arrays;
|
||||
available--;
|
||||
_count = available;
|
||||
|
||||
T[] candidate = arrays[available];
|
||||
return candidate.Length >= length ? candidate : new T[length];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public void Return(T[] array)
|
||||
{
|
||||
T[][] arrays = _arrays;
|
||||
int available = _count;
|
||||
|
||||
if (available < arrays.Length)
|
||||
{
|
||||
arrays[available] = array;
|
||||
_count = available + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user