Replace primality cache with lock-backed dictionary

This commit is contained in:
Marek Lemańczyk committed 2025-10-16 21:51:42 +02:00
1 parent e8c1069a77
commit 8d9b8b1829
1 file changed
+79 -24
+79 -24
View File
@@ -5,8 +5,8 @@ using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using PerfectNumbers.Core.Gpu;
using System.Collections.Concurrent;
namespace PerfectNumbers.Core;
@@ -787,14 +787,77 @@ internal static partial class PrimeOrderCalculator
}
// [ThreadStatic]
private static ConcurrentDictionary<ulong, bool>? _primalityCache = [];
private static Dictionary<ulong, bool>? _primalityCache;
private static readonly ReaderWriterLockSlim _primalityCacheLock = new(LockRecursionPolicy.NoRecursion);
// [ThreadStatic]
// private static UInt128 _isPrimeHits;
private static bool TryGetCachedPrimality(ulong value, out bool isPrime)
{
Dictionary<ulong, bool>? cache = Volatile.Read(ref _primalityCache);
if (cache is null)
{
isPrime = default;
return false;
}
return cache.TryGetValue(value, out isPrime);
}
private static void CachePrimalityResult(ulong value, bool isPrime)
{
Dictionary<ulong, bool>? cache = Volatile.Read(ref _primalityCache);
if (cache is not null && cache.TryGetValue(value, out bool existing) && existing == isPrime)
{
return;
}
_primalityCacheLock.EnterWriteLock();
try
{
Dictionary<ulong, bool>? current = _primalityCache;
if (current is null)
{
Dictionary<ulong, bool> newCache = new()
{
[value] = isPrime,
};
Volatile.Write(ref _primalityCache, newCache);
return;
}
if (current.TryGetValue(value, out bool existingValue) && existingValue == isPrime)
{
return;
}
Dictionary<ulong, bool> updated = new(current)
{
[value] = isPrime,
};
Volatile.Write(ref _primalityCache, updated);
}
finally
{
_primalityCacheLock.ExitWriteLock();
}
}
private static bool GetOrComputePrimality(ulong value)
{
if (TryGetCachedPrimality(value, out bool isPrime))
{
return isPrime;
}
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(value);
CachePrimalityResult(value, isPrime);
return isPrime;
}
private static PartialFactorResult PartialFactor(ulong value, in PrimeOrderSearchConfig config)
{
var primalityCache = _primalityCache;// ??= [];
if (value <= 1UL)
{
return PartialFactorResult.Empty;
@@ -865,17 +928,8 @@ internal static partial class PrimeOrderCalculator
// continue;
// }
if (!primalityCache!.TryGetValue(composite, out bool isPrime))
{
// Console.WriteLine($"Checking composite {composite}");
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
_ = primalityCache.TryAdd(composite, isPrime);
}
// else
// {
// Console.WriteLine($"isPrimeHits = {++_isPrimeHits}");
// }
bool isPrime = GetOrComputePrimality(composite);
if (isPrime)
{
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
@@ -906,12 +960,8 @@ internal static partial class PrimeOrderCalculator
for (int i = 0; i < pendingCount; i++)
{
ulong composite = pending[i];
if (!primalityCache!.TryGetValue(composite, out bool isPrime))
{
// Console.WriteLine($"Checking composite {composite}");
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
_ = primalityCache.TryAdd(composite, isPrime);
}
bool isPrime = GetOrComputePrimality(composite);
if (isPrime)
{
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
@@ -1166,14 +1216,19 @@ internal static partial class PrimeOrderCalculator
continue;
}
// TODO: Can we calculate this using residue stepping? We can try reusing modulo calculation for division result or the other way around
// Use DivRem to reuse a single division for the quotient and remainder.
int exponent = 0;
do
while (true)
{
remainingLocal /= primeCandidate;
ulong quotient = Math.DivRem(remainingLocal, primeCandidate, out ulong divisionRemainder);
if (divisionRemainder != 0UL)
{
break;
}
remainingLocal = quotient;
exponent++;
}
while ((remainingLocal % primeCandidate) == 0UL);
if (factorCount >= capacity)
{