Small optimizations to limit code branches
This commit is contained in:
1 parent
b5f8ed786c
commit
140c291344
1 file changed
+9
-31
@@ -787,50 +787,27 @@ internal static partial class PrimeOrderCalculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// [ThreadStatic]
|
// [ThreadStatic]
|
||||||
private static Dictionary<ulong, bool>? _primalityCache;
|
private static readonly Dictionary<ulong, bool> _primalityCache = [];
|
||||||
private static readonly ReaderWriterLockSlim _primalityCacheLock = new(LockRecursionPolicy.NoRecursion);
|
private static readonly ReaderWriterLockSlim _primalityCacheLock = new(LockRecursionPolicy.NoRecursion);
|
||||||
|
|
||||||
// [ThreadStatic]
|
// [ThreadStatic]
|
||||||
// private static UInt128 _isPrimeHits;
|
// private static UInt128 _isPrimeHits;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static bool TryGetCachedPrimality(ulong value, out bool isPrime)
|
private static bool TryGetCachedPrimality(ulong value, out bool isPrime)
|
||||||
{
|
{
|
||||||
_primalityCacheLock.EnterReadLock();
|
_primalityCacheLock.EnterReadLock();
|
||||||
try
|
bool result = _primalityCache.TryGetValue(value, out isPrime);
|
||||||
{
|
_primalityCacheLock.ExitReadLock();
|
||||||
Dictionary<ulong, bool>? cache = _primalityCache;
|
return result;
|
||||||
if (cache is null)
|
|
||||||
{
|
|
||||||
isPrime = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return cache.TryGetValue(value, out isPrime);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_primalityCacheLock.ExitReadLock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static void CachePrimalityResult(ulong value, bool isPrime)
|
private static void CachePrimalityResult(ulong value, bool isPrime)
|
||||||
{
|
{
|
||||||
_primalityCacheLock.EnterWriteLock();
|
_primalityCacheLock.EnterWriteLock();
|
||||||
try
|
_primalityCache[value] = isPrime;
|
||||||
{
|
_primalityCacheLock.ExitWriteLock();
|
||||||
Dictionary<ulong, bool>? cache = _primalityCache;
|
|
||||||
if (cache is null)
|
|
||||||
{
|
|
||||||
cache = new Dictionary<ulong, bool>();
|
|
||||||
_primalityCache = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
cache[value] = isPrime;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_primalityCacheLock.ExitWriteLock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool GetOrComputePrimality(ulong value)
|
private static bool GetOrComputePrimality(ulong value)
|
||||||
@@ -1220,6 +1197,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
exponent++;
|
exponent++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Console.WriteLine($"Exponent: {exponent}");
|
||||||
if (factorCount >= capacity)
|
if (factorCount >= capacity)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Capacity is smaller than factor count");
|
throw new InvalidOperationException($"Capacity is smaller than factor count");
|
||||||
|
|||||||
Reference in new issue
Block a user