Replace primality cache with lock-backed dictionary
This commit is contained in:
1 parent
e8c1069a77
commit
8d9b8b1829
1 file changed
+79
-24
@@ -5,8 +5,8 @@ using System.Diagnostics;
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
using PerfectNumbers.Core.Gpu;
|
using PerfectNumbers.Core.Gpu;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
|
|
||||||
@@ -787,14 +787,77 @@ internal static partial class PrimeOrderCalculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// [ThreadStatic]
|
// [ThreadStatic]
|
||||||
private static ConcurrentDictionary<ulong, bool>? _primalityCache = [];
|
private static Dictionary<ulong, bool>? _primalityCache;
|
||||||
|
private static readonly ReaderWriterLockSlim _primalityCacheLock = new(LockRecursionPolicy.NoRecursion);
|
||||||
|
|
||||||
// [ThreadStatic]
|
// [ThreadStatic]
|
||||||
// private static UInt128 _isPrimeHits;
|
// 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)
|
private static PartialFactorResult PartialFactor(ulong value, in PrimeOrderSearchConfig config)
|
||||||
{
|
{
|
||||||
var primalityCache = _primalityCache;// ??= [];
|
|
||||||
if (value <= 1UL)
|
if (value <= 1UL)
|
||||||
{
|
{
|
||||||
return PartialFactorResult.Empty;
|
return PartialFactorResult.Empty;
|
||||||
@@ -865,17 +928,8 @@ internal static partial class PrimeOrderCalculator
|
|||||||
// continue;
|
// continue;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (!primalityCache!.TryGetValue(composite, out bool isPrime))
|
bool isPrime = GetOrComputePrimality(composite);
|
||||||
{
|
|
||||||
// Console.WriteLine($"Checking composite {composite}");
|
|
||||||
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
|
||||||
_ = primalityCache.TryAdd(composite, isPrime);
|
|
||||||
}
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// Console.WriteLine($"isPrimeHits = {++_isPrimeHits}");
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (isPrime)
|
if (isPrime)
|
||||||
{
|
{
|
||||||
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
|
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++)
|
for (int i = 0; i < pendingCount; i++)
|
||||||
{
|
{
|
||||||
ulong composite = pending[i];
|
ulong composite = pending[i];
|
||||||
if (!primalityCache!.TryGetValue(composite, out bool isPrime))
|
bool isPrime = GetOrComputePrimality(composite);
|
||||||
{
|
|
||||||
// Console.WriteLine($"Checking composite {composite}");
|
|
||||||
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
|
||||||
_ = primalityCache.TryAdd(composite, isPrime);
|
|
||||||
}
|
|
||||||
if (isPrime)
|
if (isPrime)
|
||||||
{
|
{
|
||||||
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
|
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
|
||||||
@@ -1166,14 +1216,19 @@ internal static partial class PrimeOrderCalculator
|
|||||||
continue;
|
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;
|
int exponent = 0;
|
||||||
do
|
while (true)
|
||||||
{
|
{
|
||||||
remainingLocal /= primeCandidate;
|
ulong quotient = Math.DivRem(remainingLocal, primeCandidate, out ulong divisionRemainder);
|
||||||
|
if (divisionRemainder != 0UL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingLocal = quotient;
|
||||||
exponent++;
|
exponent++;
|
||||||
}
|
}
|
||||||
while ((remainingLocal % primeCandidate) == 0UL);
|
|
||||||
|
|
||||||
if (factorCount >= capacity)
|
if (factorCount >= capacity)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user