Restore cross-thread primality cache
This commit is contained in:
1 parent
56e413064a
commit
b985a46ed6
1 file changed
+38
-13
@@ -3,6 +3,7 @@ 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;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
@@ -661,8 +662,41 @@ internal static partial class PrimeOrderCalculator
|
|||||||
return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL;
|
return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
[ThreadStatic]
|
// [ThreadStatic]
|
||||||
private static Dictionary<ulong, bool>? _primalityCache;
|
private static readonly Dictionary<ulong, bool> _primalityCache = [];
|
||||||
|
private static readonly ReaderWriterLockSlim _primalityCacheLock = new(LockRecursionPolicy.NoRecursion);
|
||||||
|
|
||||||
|
// [ThreadStatic]
|
||||||
|
// private static UInt128 _isPrimeHits;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static bool TryGetCachedPrimality(ulong value, out bool isPrime)
|
||||||
|
{
|
||||||
|
_primalityCacheLock.EnterReadLock();
|
||||||
|
bool result = _primalityCache.TryGetValue(value, out isPrime);
|
||||||
|
_primalityCacheLock.ExitReadLock();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static void CachePrimalityResult(ulong value, bool isPrime)
|
||||||
|
{
|
||||||
|
_primalityCacheLock.EnterWriteLock();
|
||||||
|
_primalityCache[value] = isPrime;
|
||||||
|
_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)
|
||||||
{
|
{
|
||||||
@@ -700,7 +734,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
List<ulong> pending = ThreadStaticPools.RentUlongList(2);
|
List<ulong> pending = ThreadStaticPools.RentUlongList(2);
|
||||||
Stack<ulong>? compositeStack = null;
|
Stack<ulong>? compositeStack = null;
|
||||||
PartialFactorResult result;
|
PartialFactorResult result;
|
||||||
var cache = _primalityCache ??= [];
|
|
||||||
|
|
||||||
if (!gpuFactored)
|
if (!gpuFactored)
|
||||||
{
|
{
|
||||||
@@ -739,11 +772,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
// continue;
|
// continue;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (!cache.TryGetValue(composite, out bool isPrime))
|
bool isPrime = GetOrComputePrimality(composite);
|
||||||
{
|
|
||||||
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
|
||||||
cache.Add(composite, isPrime);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isPrime)
|
if (isPrime)
|
||||||
{
|
{
|
||||||
@@ -779,11 +808,7 @@ 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 (!cache.TryGetValue(composite, out bool isPrime))
|
bool isPrime = GetOrComputePrimality(composite);
|
||||||
{
|
|
||||||
isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
|
||||||
cache.Add(composite, isPrime);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isPrime)
|
if (isPrime)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user