Align divisor pooling with review feedback
This commit is contained in:
1 parent
e44662413a
commit
c8f8132db7
3 files changed
+20
-48
No files matched your search
@@ -112,7 +112,7 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
||||
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? current = cache;
|
||||
if (current is null)
|
||||
{
|
||||
current = ThreadStaticPools.TakeMersenneFactorCacheDictionary();
|
||||
current = ThreadStaticPools.RentMersenneFactorCacheDictionary();
|
||||
cache = current;
|
||||
}
|
||||
|
||||
@@ -149,11 +149,6 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
||||
// }
|
||||
|
||||
UInt128 step = (UInt128)prime << 1;
|
||||
if (step == UInt128.Zero)
|
||||
{
|
||||
processedAll = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
UInt128 limit = allowedMax;
|
||||
UInt128 divisor = step + UInt128.One;
|
||||
|
||||
@@ -23,26 +23,6 @@ public class MersenneDivisorCycles
|
||||
{
|
||||
}
|
||||
|
||||
private static Dictionary<ulong, int> RentFactorCountDictionary()
|
||||
{
|
||||
return ThreadStaticPools.TakeFactorCountDictionary();
|
||||
}
|
||||
|
||||
private static void ReturnFactorCountDictionary(Dictionary<ulong, int> dictionary)
|
||||
{
|
||||
ThreadStaticPools.ReturnFactorCountDictionary(dictionary);
|
||||
}
|
||||
|
||||
private static Dictionary<ulong, int> RentFactorScratchDictionary()
|
||||
{
|
||||
return ThreadStaticPools.TakeFactorScratchDictionary();
|
||||
}
|
||||
|
||||
private static void ReturnFactorScratchDictionary(Dictionary<ulong, int> dictionary)
|
||||
{
|
||||
ThreadStaticPools.ReturnFactorScratchDictionary(dictionary);
|
||||
}
|
||||
|
||||
public void LoadFrom(string path)
|
||||
{
|
||||
EnsureSmallBuffer();
|
||||
@@ -317,10 +297,6 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
|
||||
ulong phi = divisor - 1UL;
|
||||
if (phi == 0UL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int twoCount = BitOperations.TrailingZeroCount(phi);
|
||||
ulong reducedPhi = phi >> twoCount;
|
||||
@@ -330,12 +306,8 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
|
||||
ulong k = reducedPhi / exponent;
|
||||
if (k == 0UL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Dictionary<ulong, int> factorCounts = RentFactorCountDictionary();
|
||||
Dictionary<ulong, int> factorCounts = ThreadStaticPools.RentFactorCountDictionary();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -354,7 +326,7 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReturnFactorCountDictionary(factorCounts);
|
||||
ThreadStaticPools.ReturnFactorCountDictionary(factorCounts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,7 +369,7 @@ public class MersenneDivisorCycles
|
||||
return true;
|
||||
}
|
||||
|
||||
Dictionary<ulong, int> scratch = RentFactorScratchDictionary();
|
||||
Dictionary<ulong, int> scratch = ThreadStaticPools.RentFactorScratchDictionary();
|
||||
|
||||
try
|
||||
{
|
||||
@@ -430,18 +402,12 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
finally
|
||||
{
|
||||
ReturnFactorScratchDictionary(scratch);
|
||||
ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryFactorIntoCountsInternal(ulong value, Dictionary<ulong, int> counts)
|
||||
{
|
||||
// This guard terminates the recursion once Pollard-Rho factorization has reduced the input to 1.
|
||||
if (value <= 1UL)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
ulong remaining = value;
|
||||
uint[] smallPrimes = PrimesGenerator.SmallPrimes;
|
||||
ulong[] smallPrimesSquared = PrimesGenerator.SmallPrimesPow2;
|
||||
@@ -480,7 +446,18 @@ public class MersenneDivisorCycles
|
||||
}
|
||||
|
||||
ulong quotient = remaining / factor;
|
||||
return TryFactorIntoCountsInternal(factor, counts) && TryFactorIntoCountsInternal(quotient, counts);
|
||||
if (factor > 1UL && !TryFactorIntoCountsInternal(factor, counts))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Guard the recursive calls to terminate once Pollard-Rho reduces either branch to 1.
|
||||
if (quotient > 1UL && !TryFactorIntoCountsInternal(quotient, counts))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void AddFactor(Dictionary<ulong, int> counts, ulong factor)
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace PerfectNumbers.Core
|
||||
private static Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? _mersenneFactorCacheDictionary;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry> TakeMersenneFactorCacheDictionary()
|
||||
public static Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry> RentMersenneFactorCacheDictionary()
|
||||
{
|
||||
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? cache = _mersenneFactorCacheDictionary;
|
||||
if (cache is null)
|
||||
@@ -71,7 +71,7 @@ namespace PerfectNumbers.Core
|
||||
private static Dictionary<ulong, int>? _factorCountDictionary;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Dictionary<ulong, int> TakeFactorCountDictionary()
|
||||
public static Dictionary<ulong, int> RentFactorCountDictionary()
|
||||
{
|
||||
Dictionary<ulong, int>? dictionary = _factorCountDictionary;
|
||||
if (dictionary is null)
|
||||
@@ -95,7 +95,7 @@ namespace PerfectNumbers.Core
|
||||
private static Dictionary<ulong, int>? _factorScratchDictionary;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Dictionary<ulong, int> TakeFactorScratchDictionary()
|
||||
public static Dictionary<ulong, int> RentFactorScratchDictionary()
|
||||
{
|
||||
Dictionary<ulong, int>? dictionary = _factorScratchDictionary;
|
||||
if (dictionary is null)
|
||||
|
||||
Reference in new issue
Block a user