Streamline factor cache lease usage in CPU tester

This commit is contained in:
Marek Lemańczyk committed 2025-10-15 21:55:30 +02:00
1 parent 64c3721db4
commit 9ab87a8b9b
1 file changed
+10 -16
@@ -132,12 +132,6 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
FactorCacheLease factorCacheLease = new FactorCacheLease(); FactorCacheLease factorCacheLease = new FactorCacheLease();
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? factorCache = null; Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? factorCache = null;
bool foundDivisor = false;
bool requireLargeCycleCache = allowedMax > PerfectNumberConstants.MaxQForDivisorCycles;
if (requireLargeCycleCache)
{
factorCacheLease.EnsureInitialized(ref factorCache);
}
DivisorCycleCache cycleCache = DivisorCycleCache.Shared; DivisorCycleCache cycleCache = DivisorCycleCache.Shared;
@@ -178,12 +172,16 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
} }
else else
{ {
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? cacheForCycle = requireLargeCycleCache ? factorCache : null; if (factorCache is null)
{
factorCacheLease.EnsureInitialized(ref factorCache);
}
if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponent( if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponent(
candidate, candidate,
prime, prime,
divisorData, divisorData,
cacheForCycle, factorCache,
out ulong computedCycle) || computedCycle == 0UL) out ulong computedCycle) || computedCycle == 0UL)
{ {
divisorCycle = cycleCache.GetCycleLength(candidate); divisorCycle = cycleCache.GetCycleLength(candidate);
@@ -198,8 +196,10 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
{ {
// A cycle equal to the tested exponent (which is prime in this path) guarantees that the candidate divides // A cycle equal to the tested exponent (which is prime in this path) guarantees that the candidate divides
// the corresponding Mersenne number because the order of 2 modulo the divisor is exactly p. // the corresponding Mersenne number because the order of 2 modulo the divisor is exactly p.
foundDivisor = true; factorCacheLease.Dispose();
break; factorCache = null;
processedAll = true;
return true;
} }
if (divisorCycle == 0UL) if (divisorCycle == 0UL)
@@ -220,12 +220,6 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
factorCacheLease.Dispose(); factorCacheLease.Dispose();
factorCache = null; // The pooled dictionary was returned to the thread-static slot. factorCache = null; // The pooled dictionary was returned to the thread-static slot.
if (foundDivisor)
{
processedAll = true;
return true;
}
processedAll = divisor > limit; processedAll = divisor > limit;
return false; return false;
} }