Avoid pooling small buffers in CPU divisor pipeline
This commit is contained in:
1 parent
8e9390bc5e
commit
ad22329814
4 files changed
+102
-41
No files matched your search
@@ -63,7 +63,9 @@ public sealed class DivisorCycleCache
|
||||
int[]? rentedMissing = null;
|
||||
Span<int> missingBuffer = length <= StackBufferThreshold
|
||||
? stackalloc int[length]
|
||||
: new Span<int>(rentedMissing = ThreadLocalArrayPool<int>.Shared.Rent(length), 0, length);
|
||||
: length < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<int>(new int[length])
|
||||
: new Span<int>(rentedMissing = ThreadLocalArrayPool<int>.Shared.Rent(length), 0, length);
|
||||
|
||||
int missingCount = 0;
|
||||
|
||||
@@ -128,12 +130,16 @@ public sealed class DivisorCycleCache
|
||||
ulong[]? rentedDivisors = null;
|
||||
Span<ulong> workDivisors = count <= StackBufferThreshold
|
||||
? stackalloc ulong[count]
|
||||
: new Span<ulong>(rentedDivisors = ThreadLocalArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
: count < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<ulong>(new ulong[count])
|
||||
: new Span<ulong>(rentedDivisors = ThreadLocalArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
|
||||
ulong[]? rentedResults = null;
|
||||
Span<ulong> workResults = count <= StackBufferThreshold
|
||||
? stackalloc ulong[count]
|
||||
: new Span<ulong>(rentedResults = ThreadLocalArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
: count < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<ulong>(new ulong[count])
|
||||
: new Span<ulong>(rentedResults = ThreadLocalArrayPool<ulong>.Shared.Rent(count), 0, count);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
@@ -150,7 +156,11 @@ public sealed class DivisorCycleCache
|
||||
if (rentedDivisors is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedDivisors, clearArray: false);
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedResults!, clearArray: false);
|
||||
}
|
||||
|
||||
if (rentedResults is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedResults, clearArray: false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,22 +182,30 @@ public sealed class DivisorCycleCache
|
||||
ulong[]? rentedPow = null;
|
||||
Span<ulong> powSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedPow = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
: length < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<ulong>(new ulong[length])
|
||||
: new Span<ulong>(rentedPow = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
ulong[]? rentedOrder = null;
|
||||
Span<ulong> orderSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedOrder = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
: length < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<ulong>(new ulong[length])
|
||||
: new Span<ulong>(rentedOrder = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
ulong[]? rentedResult = null;
|
||||
Span<ulong> resultSpan = length <= StackBufferThreshold
|
||||
? stackalloc ulong[length]
|
||||
: new Span<ulong>(rentedResult = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
: length < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<ulong>(new ulong[length])
|
||||
: new Span<ulong>(rentedResult = ThreadLocalArrayPool<ulong>.Shared.Rent(length), 0, length);
|
||||
|
||||
byte[]? rentedStatus = null;
|
||||
Span<byte> statusSpan = length <= StackBufferThreshold
|
||||
? stackalloc byte[length]
|
||||
: new Span<byte>(rentedStatus = ThreadLocalArrayPool<byte>.Shared.Rent(length), 0, length);
|
||||
: length < PerfectNumberConstants.PooledArrayThreshold
|
||||
? new Span<byte>(new byte[length])
|
||||
: new Span<byte>(rentedStatus = ThreadLocalArrayPool<byte>.Shared.Rent(length), 0, length);
|
||||
|
||||
ref ulong divisorRef = ref MemoryMarshal.GetReference(divisors);
|
||||
divisorBuffer.View.CopyFromCPU(ref divisorRef, length);
|
||||
@@ -235,9 +253,21 @@ public sealed class DivisorCycleCache
|
||||
if (rentedPow is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedPow, clearArray: false);
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedOrder!, clearArray: false);
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedResult!, clearArray: false);
|
||||
ThreadLocalArrayPool<byte>.Shared.Return(rentedStatus!, clearArray: false);
|
||||
}
|
||||
|
||||
if (rentedOrder is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedOrder, clearArray: false);
|
||||
}
|
||||
|
||||
if (rentedResult is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(rentedResult, clearArray: false);
|
||||
}
|
||||
|
||||
if (rentedStatus is not null)
|
||||
{
|
||||
ThreadLocalArrayPool<byte>.Shared.Return(rentedStatus, clearArray: false);
|
||||
}
|
||||
|
||||
execution.Dispose();
|
||||
|
||||
@@ -567,7 +567,10 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
|
||||
int count = factorCounts.Count;
|
||||
ulong[] primes = ThreadLocalArrayPool<ulong>.Shared.Rent(count);
|
||||
bool rentFromPool = count >= PerfectNumberConstants.PooledArrayThreshold;
|
||||
ulong[] primes = rentFromPool
|
||||
? ThreadLocalArrayPool<ulong>.Shared.Rent(count)
|
||||
: new ulong[count];
|
||||
try
|
||||
{
|
||||
int index = 0;
|
||||
@@ -607,7 +610,10 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
finally
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(primes, clearArray: false);
|
||||
if (rentFromPool)
|
||||
{
|
||||
ThreadLocalArrayPool<ulong>.Shared.Return(primes, clearArray: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -793,7 +799,11 @@ public class MersenneDivisorCycles
|
||||
tasks[taskIndex] = Task.Run(() =>
|
||||
{
|
||||
ulong rangeLength = localEnd - localStart + 1UL;
|
||||
(ulong divisor, ulong cycle)[] localCycles = ThreadLocalArrayPool<(ulong, ulong)>.Shared.Rent(checked((int)rangeLength));
|
||||
int localLength = checked((int)rangeLength);
|
||||
bool rentLocalCycles = localLength >= PerfectNumberConstants.PooledArrayThreshold;
|
||||
(ulong divisor, ulong cycle)[] localCycles = rentLocalCycles
|
||||
? ThreadLocalArrayPool<(ulong, ulong)>.Shared.Rent(localLength)
|
||||
: new (ulong divisor, ulong cycle)[localLength];
|
||||
int localCycleIndex = 0;
|
||||
|
||||
try
|
||||
@@ -841,7 +851,10 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
finally
|
||||
{
|
||||
ThreadLocalArrayPool<(ulong, ulong)>.Shared.Return(localCycles, clearArray: false);
|
||||
if (rentLocalCycles)
|
||||
{
|
||||
ThreadLocalArrayPool<(ulong, ulong)>.Shared.Return(localCycles, clearArray: false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace PerfectNumbers.Core
|
||||
{
|
||||
public sealed class PartialFactorResult
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static PartialFactorResult? s_poolHead;
|
||||
[ThreadStatic]
|
||||
private static PartialFactorResult? s_poolHead;
|
||||
|
||||
public static readonly PartialFactorResult Empty = new()
|
||||
{
|
||||
Cofactor = 1UL,
|
||||
FullyFactored = true,
|
||||
};
|
||||
|
||||
public FactorEntry[]? Factors;
|
||||
private PartialFactorResult? _next;
|
||||
public ulong Cofactor;
|
||||
public bool FullyFactored;
|
||||
public int Count;
|
||||
|
||||
private PartialFactorResult? _next;
|
||||
private bool _factorsFromPool;
|
||||
|
||||
private PartialFactorResult()
|
||||
{
|
||||
}
|
||||
@@ -30,52 +37,58 @@ namespace PerfectNumbers.Core
|
||||
s_poolHead = instance._next;
|
||||
}
|
||||
|
||||
FactorEntry[]? factorArray = factors;
|
||||
instance._next = null;
|
||||
instance.Factors = factors;
|
||||
instance.Factors = factorArray;
|
||||
instance.Cofactor = cofactor;
|
||||
instance.FullyFactored = fullyFactored;
|
||||
instance.Count = count;
|
||||
instance._factorsFromPool = factorArray is not null && factorArray.Length >= PerfectNumberConstants.PooledArrayThreshold;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static readonly PartialFactorResult Empty = new()
|
||||
{
|
||||
Cofactor = 1UL,
|
||||
FullyFactored = true,
|
||||
};
|
||||
|
||||
public PartialFactorResult WithAdditionalPrime(ulong prime)
|
||||
{
|
||||
FactorEntry[]? source = Factors;
|
||||
if (source is null || Count == 0)
|
||||
{
|
||||
FactorEntry[] local = ThreadLocalArrayPool<FactorEntry>.Shared.Rent(1);
|
||||
local[0] = new FactorEntry(prime);
|
||||
return Rent(local, 1UL, true, 1);
|
||||
FactorEntry[] single = new FactorEntry[1];
|
||||
single[0] = new FactorEntry(prime);
|
||||
return Rent(single, 1UL, true, 1);
|
||||
}
|
||||
|
||||
FactorEntry[] extended = ThreadLocalArrayPool<FactorEntry>.Shared.Rent(Count + 1);
|
||||
int newCount = Count + 1;
|
||||
FactorEntry[] extended = newCount >= PerfectNumberConstants.PooledArrayThreshold
|
||||
? ThreadLocalArrayPool<FactorEntry>.Shared.Rent(newCount)
|
||||
: new FactorEntry[newCount];
|
||||
|
||||
Array.Copy(source, 0, extended, 0, Count);
|
||||
extended[Count] = new FactorEntry(prime);
|
||||
Span<FactorEntry> span = extended.AsSpan(0, Count + 1);
|
||||
Span<FactorEntry> span = extended.AsSpan(0, newCount);
|
||||
span.Sort(static (a, b) => a.Value.CompareTo(b.Value));
|
||||
return Rent(extended, 1UL, true, Count + 1);
|
||||
return Rent(extended, 1UL, true, newCount);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (this != Empty)
|
||||
if (this == Empty)
|
||||
{
|
||||
FactorEntry[]? factors = Factors;
|
||||
if (factors is not null)
|
||||
return;
|
||||
}
|
||||
|
||||
FactorEntry[]? factors = Factors;
|
||||
if (factors is not null)
|
||||
{
|
||||
Factors = null;
|
||||
if (_factorsFromPool)
|
||||
{
|
||||
Factors = null;
|
||||
ThreadLocalArrayPool<FactorEntry>.Shared.Return(factors, clearArray: false);
|
||||
}
|
||||
|
||||
_next = s_poolHead;
|
||||
s_poolHead = this;
|
||||
}
|
||||
|
||||
_factorsFromPool = false;
|
||||
_next = s_poolHead;
|
||||
s_poolHead = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1058,7 +1058,10 @@ internal static partial class PrimeOrderCalculator
|
||||
return PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0);
|
||||
}
|
||||
|
||||
FactorEntry[] factors = ThreadLocalArrayPool<FactorEntry>.Shared.Rent(counts.Count);
|
||||
int dictionaryCount = counts.Count;
|
||||
FactorEntry[] factors = dictionaryCount >= PerfectNumberConstants.PooledArrayThreshold
|
||||
? ThreadLocalArrayPool<FactorEntry>.Shared.Rent(dictionaryCount)
|
||||
: new FactorEntry[dictionaryCount];
|
||||
int index = 0;
|
||||
foreach (KeyValuePair<ulong, int> entry in counts)
|
||||
{
|
||||
@@ -1092,7 +1095,9 @@ internal static partial class PrimeOrderCalculator
|
||||
return PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0);
|
||||
}
|
||||
|
||||
FactorEntry[] array = ThreadLocalArrayPool<FactorEntry>.Shared.Rent(actualCount);
|
||||
FactorEntry[] array = actualCount >= PerfectNumberConstants.PooledArrayThreshold
|
||||
? ThreadLocalArrayPool<FactorEntry>.Shared.Rent(actualCount)
|
||||
: new FactorEntry[actualCount];
|
||||
int arrayIndex = 0;
|
||||
for (int i = 0; i < factorCount; i++)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user