Streamline divisor factor pooling for CPU hot path

This commit is contained in:
Marek Lemańczyk committed 2025-10-15 21:50:11 +02:00
1 parent c99aebdf68
commit 64c3721db4
3 files changed
+46 -47

No files matched your search

+14 -2
View File
@@ -32,6 +32,18 @@ namespace PerfectNumbers.Core
}
}
[ThreadStatic]
private static ArrayPool<KeyValuePair<ulong, int>>? _factorKeyValuePairPool;
public static ArrayPool<KeyValuePair<ulong, int>> FactorKeyValuePairPool
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _factorKeyValuePairPool ??= ArrayPool<KeyValuePair<ulong, int>>.Create();
}
}
[ThreadStatic]
private static MersenneCpuDivisorScanSession? _mersenneCpuDivisorSession;
@@ -87,9 +99,9 @@ namespace PerfectNumbers.Core
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnMersenneFactorCacheDictionary(Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry> cache)
{
foreach (KeyValuePair<ulong, MersenneDivisorCycles.FactorCacheEntry> entry in cache)
for (Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>.Enumerator enumerator = cache.GetEnumerator(); enumerator.MoveNext();)
{
entry.Value.ReturnToPool();
enumerator.Current.Value.ReturnToPool();
}
cache.Clear();