Pool factor cache entries to keep CPU factoring allocation-free

This commit is contained in:
Marek Lemańczyk committed 2025-10-15 20:48:33 +02:00
1 parent 34fcd62c23
commit b0ce7152f7
4 files changed
+150 -79

No files matched your search

+24
View File
@@ -72,6 +72,30 @@ namespace PerfectNumbers.Core
_mersenneFactorCacheDictionary = cache;
}
[ThreadStatic]
private static MersenneDivisorCycles.FactorCacheEntry? _factorCacheEntryPoolHead;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MersenneDivisorCycles.FactorCacheEntry RentFactorCacheEntry()
{
MersenneDivisorCycles.FactorCacheEntry? entry = _factorCacheEntryPoolHead;
if (entry is null)
{
return new MersenneDivisorCycles.FactorCacheEntry();
}
_factorCacheEntryPoolHead = entry.Next;
entry.Next = null;
return entry;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnFactorCacheEntry(MersenneDivisorCycles.FactorCacheEntry entry)
{
entry.Next = _factorCacheEntryPoolHead;
_factorCacheEntryPoolHead = entry;
}
[ThreadStatic]
private static Dictionary<ulong, int>? _factorCountDictionary;