From a226ae10cb95775771fd0c3f7146765ffc46c909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Tue, 21 Oct 2025 20:52:21 +0200 Subject: [PATCH] Stop clearing pooled buffers inside rent helpers --- PerfectNumbers.Core/MersenneDivisorCycles.cs | 3 +++ .../PrimeOrderCalculator.Cpu.cs | 5 ++++ PerfectNumbers.Core/ThreadStaticPools.cs | 27 +------------------ 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/PerfectNumbers.Core/MersenneDivisorCycles.cs b/PerfectNumbers.Core/MersenneDivisorCycles.cs index 942af1a..b0e677e 100644 --- a/PerfectNumbers.Core/MersenneDivisorCycles.cs +++ b/PerfectNumbers.Core/MersenneDivisorCycles.cs @@ -322,6 +322,7 @@ public class MersenneDivisorCycles primeOrderFailed = false; } + factorCounts.Clear(); ThreadStaticPools.ReturnFactorCountDictionary(factorCounts); return success; } @@ -339,6 +340,7 @@ public class MersenneDivisorCycles bool success = TryFactorIntoCountsInternal(value, scratch); if (!success) { + scratch.Clear(); ThreadStaticPools.ReturnFactorScratchDictionary(scratch); return false; } @@ -349,6 +351,7 @@ public class MersenneDivisorCycles AddFactor(counts, pair.Key, pair.Value); } + scratch.Clear(); ThreadStaticPools.ReturnFactorScratchDictionary(scratch); return true; } diff --git a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs index 8bf676b..abbad24 100644 --- a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs +++ b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs @@ -521,6 +521,7 @@ internal static partial class PrimeOrderCalculator BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity); if (candidates.Count == 0) { + candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates); return false; } @@ -623,6 +624,7 @@ internal static partial class PrimeOrderCalculator pool.Return(gpuPool, clearArray: false); } + candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates); ThreadStaticPools.ReturnExponentStepper(powStepper); result = candidate; @@ -637,6 +639,7 @@ internal static partial class PrimeOrderCalculator index += batchSize; } + candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates); ThreadStaticPools.ReturnExponentStepper(powStepper); // DebugLog("No candidate confirmed"); @@ -1030,6 +1033,7 @@ internal static partial class PrimeOrderCalculator } factorCount = copyIndex; + counts.Clear(); ThreadStaticPools.ReturnUlongIntDictionary(counts); counts = null; } @@ -1231,6 +1235,7 @@ internal static partial class PrimeOrderCalculator if (counts is not null) { + counts.Clear(); ThreadStaticPools.ReturnUlongIntDictionary(counts); } diff --git a/PerfectNumbers.Core/ThreadStaticPools.cs b/PerfectNumbers.Core/ThreadStaticPools.cs index 8191676..f8b07cc 100644 --- a/PerfectNumbers.Core/ThreadStaticPools.cs +++ b/PerfectNumbers.Core/ThreadStaticPools.cs @@ -129,11 +129,6 @@ namespace PerfectNumbers.Core int lastIndex = poolCount - 1; List list = pool[lastIndex]; pool.RemoveAt(lastIndex); - if (list.Count > 0) - { - list.Clear(); - } - if (list.Capacity < capacityHint) { list.Capacity = capacityHint; @@ -174,11 +169,6 @@ namespace PerfectNumbers.Core int lastIndex = poolCount - 1; List list = pool[lastIndex]; pool.RemoveAt(lastIndex); - if (list.Count > 0) - { - list.Clear(); - } - if (list.Capacity < capacityHint) { list.Capacity = capacityHint; @@ -219,14 +209,8 @@ namespace PerfectNumbers.Core int lastIndex = poolCount - 1; Stack stack = pool[lastIndex]; pool.RemoveAt(lastIndex); - int stackCount = stack.Count; - if (stackCount > 0) - { - stack.Clear(); - stackCount = 0; - } - if (stackCount < capacityHint) + if (stack.Count < capacityHint) { stack.EnsureCapacity(capacityHint); } @@ -266,11 +250,6 @@ namespace PerfectNumbers.Core int lastIndex = poolCount - 1; Dictionary dictionary = pool[lastIndex]; pool.RemoveAt(lastIndex); - if (dictionary.Count > 0) - { - dictionary.Clear(); - } - dictionary.EnsureCapacity(capacityHint); return dictionary; @@ -306,14 +285,12 @@ namespace PerfectNumbers.Core } _factorCountDictionary = null; - dictionary.Clear(); return dictionary; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ReturnFactorCountDictionary(Dictionary dictionary) { - dictionary.Clear(); _factorCountDictionary = dictionary; } @@ -330,14 +307,12 @@ namespace PerfectNumbers.Core } _factorScratchDictionary = null; - dictionary.Clear(); return dictionary; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ReturnFactorScratchDictionary(Dictionary dictionary) { - dictionary.Clear(); _factorScratchDictionary = dictionary; }