Rented pools made thread static

This commit is contained in:
mlemanczyk committed 2025-10-11 14:14:28 +02:00
1 parent 1cbf49c8b5
commit 12140bca0a
1 file changed
+17 -6
+17 -6
View File
@@ -233,14 +233,24 @@ internal static partial class PrimeOrderCalculator
return order; return order;
} }
[ThreadStatic]
private static ArrayPool<FactorEntry>? _factorPool;
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
[ThreadStatic]
private static ArrayPool<ulong>? _ulongPool;
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
private static ulong ExponentLoweringCpu(ulong order, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData) private static ulong ExponentLoweringCpu(ulong order, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
{ {
ArrayPool<FactorEntry> factorPool = FactorPool;
FactorEntry[]? tempArray = null; FactorEntry[]? tempArray = null;
try try
{ {
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors; ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
int length = factors.Count; int length = factors.Count;
tempArray = ArrayPool<FactorEntry>.Shared.Rent(length + 1); tempArray = factorPool.Rent(length + 1);
Span<FactorEntry> buffer = tempArray; Span<FactorEntry> buffer = tempArray;
factorSpan.CopyTo(buffer); factorSpan.CopyTo(buffer);
@@ -282,7 +292,7 @@ internal static partial class PrimeOrderCalculator
{ {
if (tempArray is not null) if (tempArray is not null)
{ {
ArrayPool<FactorEntry>.Shared.Return(tempArray, clearArray: false); factorPool.Return(tempArray, clearArray: false);
} }
} }
} }
@@ -431,6 +441,7 @@ internal static partial class PrimeOrderCalculator
const int MaxGpuBatchSize = 1024; const int MaxGpuBatchSize = 1024;
const int StackGpuBatchSize = 1024; const int StackGpuBatchSize = 1024;
Span<ulong> stackGpuRemainders = stackalloc ulong[StackGpuBatchSize]; Span<ulong> stackGpuRemainders = stackalloc ulong[StackGpuBatchSize];
ArrayPool<ulong> uLongPool = ULongPool;
while (index < candidateCount && powUsed < powBudget) while (index < candidateCount && powUsed < powBudget)
{ {
int remaining = candidateCount - index; int remaining = candidateCount - index;
@@ -462,7 +473,7 @@ internal static partial class PrimeOrderCalculator
} }
else else
{ {
gpuPool = ArrayPool<ulong>.Shared.Rent(batchSize); gpuPool = uLongPool.Rent(batchSize);
Span<ulong> pooledRemainders = gpuPool.AsSpan(0, batchSize); Span<ulong> pooledRemainders = gpuPool.AsSpan(0, batchSize);
status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, pooledRemainders, divisorData); status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, pooledRemainders, divisorData);
if (status == GpuPow2ModStatus.Success) if (status == GpuPow2ModStatus.Success)
@@ -472,7 +483,7 @@ internal static partial class PrimeOrderCalculator
} }
else else
{ {
ArrayPool<ulong>.Shared.Return(gpuPool, clearArray: false); uLongPool.Return(gpuPool, clearArray: false);
gpuPool = null; gpuPool = null;
} }
} }
@@ -510,7 +521,7 @@ internal static partial class PrimeOrderCalculator
if (gpuPool is not null) if (gpuPool is not null)
{ {
ArrayPool<ulong>.Shared.Return(gpuPool, clearArray: false); uLongPool.Return(gpuPool, clearArray: false);
} }
result = candidate; result = candidate;
@@ -519,7 +530,7 @@ internal static partial class PrimeOrderCalculator
if (gpuPool is not null) if (gpuPool is not null)
{ {
ArrayPool<ulong>.Shared.Return(gpuPool, clearArray: false); uLongPool.Return(gpuPool, clearArray: false);
} }
index += batchSize; index += batchSize;