Restore pooled small factor buffers

This commit is contained in:
Marek Lemańczyk committed 2025-10-16 17:18:19 +02:00
1 parent 949b5f891c
commit fb08a61c66
2 files changed
+27 -4

No files matched your search

+15 -4
View File
@@ -781,8 +781,10 @@ internal static partial class PrimeOrderCalculator
} }
const int FactorSlotCount = GpuSmallPrimeFactorSlots; const int FactorSlotCount = GpuSmallPrimeFactorSlots;
Span<ulong> primeSlots = stackalloc ulong[FactorSlotCount]; ulong[] primeSlotsArray = ThreadStaticPools.UlongPool.Rent(FactorSlotCount);
Span<int> exponentSlots = stackalloc int[FactorSlotCount]; int[] exponentSlotsArray = ThreadStaticPools.IntPool.Rent(FactorSlotCount);
Span<ulong> primeSlots = primeSlotsArray.AsSpan(0, FactorSlotCount);
Span<int> exponentSlots = exponentSlotsArray.AsSpan(0, FactorSlotCount);
primeSlots.Clear(); primeSlots.Clear();
exponentSlots.Clear(); exponentSlots.Clear();
@@ -951,6 +953,9 @@ internal static partial class PrimeOrderCalculator
result = PartialFactorResult.Rent(array, cofactor, fullyFactoredArray, arrayIndex); result = PartialFactorResult.Rent(array, cofactor, fullyFactoredArray, arrayIndex);
ReturnResult: ReturnResult:
ThreadStaticPools.UlongPool.Return(primeSlotsArray);
ThreadStaticPools.IntPool.Return(exponentSlotsArray);
if (counts is not null) if (counts is not null)
{ {
ThreadStaticPools.ReturnUlongIntDictionary(counts); ThreadStaticPools.ReturnUlongIntDictionary(counts);
@@ -1081,14 +1086,18 @@ internal static partial class PrimeOrderCalculator
private static bool TryPopulateSmallPrimeFactorsGpu(ulong value, uint limit, Dictionary<ulong, int> counts, out ulong remaining) private static bool TryPopulateSmallPrimeFactorsGpu(ulong value, uint limit, Dictionary<ulong, int> counts, out ulong remaining)
{ {
Span<ulong> primeBuffer = stackalloc ulong[GpuSmallPrimeFactorSlots]; ulong[] primeBufferArray = ThreadStaticPools.UlongPool.Rent(GpuSmallPrimeFactorSlots);
Span<int> exponentBuffer = stackalloc int[GpuSmallPrimeFactorSlots]; int[] exponentBufferArray = ThreadStaticPools.IntPool.Rent(GpuSmallPrimeFactorSlots);
Span<ulong> primeBuffer = primeBufferArray.AsSpan(0, GpuSmallPrimeFactorSlots);
Span<int> exponentBuffer = exponentBufferArray.AsSpan(0, GpuSmallPrimeFactorSlots);
primeBuffer.Clear(); primeBuffer.Clear();
exponentBuffer.Clear(); exponentBuffer.Clear();
if (!PrimeOrderGpuHeuristics.TryPartialFactor(value, limit, primeBuffer, exponentBuffer, out int factorCount, out ulong gpuRemaining, out _)) if (!PrimeOrderGpuHeuristics.TryPartialFactor(value, limit, primeBuffer, exponentBuffer, out int factorCount, out ulong gpuRemaining, out _))
{ {
remaining = value; remaining = value;
ThreadStaticPools.UlongPool.Return(primeBufferArray);
ThreadStaticPools.IntPool.Return(exponentBufferArray);
return false; return false;
} }
@@ -1105,6 +1114,8 @@ internal static partial class PrimeOrderCalculator
counts[primeValue] = exponent; counts[primeValue] = exponent;
} }
ThreadStaticPools.UlongPool.Return(primeBufferArray);
ThreadStaticPools.IntPool.Return(exponentBufferArray);
return true; return true;
} }
private static long CreateDeadlineTimestamp(int milliseconds) private static long CreateDeadlineTimestamp(int milliseconds)
+12
View File
@@ -79,6 +79,18 @@ namespace PerfectNumbers.Core
} }
} }
[ThreadStatic]
private static ArrayPool<int>? _intPool;
public static ArrayPool<int> IntPool
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _intPool ??= ArrayPool<int>.Create();
}
}
[ThreadStatic] [ThreadStatic]
private static List<List<ulong>>? _ulongListPool; private static List<List<ulong>>? _ulongListPool;