Stop clearing pooled buffers inside rent helpers

This commit is contained in:
Marek Lemańczyk committed 2025-10-21 20:52:21 +02:00
1 parent 7baf3b619f
commit a226ae10cb
3 files changed
+9 -26

No files matched your search

@@ -322,6 +322,7 @@ public class MersenneDivisorCycles
primeOrderFailed = false; primeOrderFailed = false;
} }
factorCounts.Clear();
ThreadStaticPools.ReturnFactorCountDictionary(factorCounts); ThreadStaticPools.ReturnFactorCountDictionary(factorCounts);
return success; return success;
} }
@@ -339,6 +340,7 @@ public class MersenneDivisorCycles
bool success = TryFactorIntoCountsInternal(value, scratch); bool success = TryFactorIntoCountsInternal(value, scratch);
if (!success) if (!success)
{ {
scratch.Clear();
ThreadStaticPools.ReturnFactorScratchDictionary(scratch); ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
return false; return false;
} }
@@ -349,6 +351,7 @@ public class MersenneDivisorCycles
AddFactor(counts, pair.Key, pair.Value); AddFactor(counts, pair.Key, pair.Value);
} }
scratch.Clear();
ThreadStaticPools.ReturnFactorScratchDictionary(scratch); ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
return true; return true;
} }
@@ -521,6 +521,7 @@ internal static partial class PrimeOrderCalculator
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity); BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
if (candidates.Count == 0) if (candidates.Count == 0)
{ {
candidates.Clear();
ThreadStaticPools.ReturnUlongList(candidates); ThreadStaticPools.ReturnUlongList(candidates);
return false; return false;
} }
@@ -623,6 +624,7 @@ internal static partial class PrimeOrderCalculator
pool.Return(gpuPool, clearArray: false); pool.Return(gpuPool, clearArray: false);
} }
candidates.Clear();
ThreadStaticPools.ReturnUlongList(candidates); ThreadStaticPools.ReturnUlongList(candidates);
ThreadStaticPools.ReturnExponentStepper(powStepper); ThreadStaticPools.ReturnExponentStepper(powStepper);
result = candidate; result = candidate;
@@ -637,6 +639,7 @@ internal static partial class PrimeOrderCalculator
index += batchSize; index += batchSize;
} }
candidates.Clear();
ThreadStaticPools.ReturnUlongList(candidates); ThreadStaticPools.ReturnUlongList(candidates);
ThreadStaticPools.ReturnExponentStepper(powStepper); ThreadStaticPools.ReturnExponentStepper(powStepper);
// DebugLog("No candidate confirmed"); // DebugLog("No candidate confirmed");
@@ -1030,6 +1033,7 @@ internal static partial class PrimeOrderCalculator
} }
factorCount = copyIndex; factorCount = copyIndex;
counts.Clear();
ThreadStaticPools.ReturnUlongIntDictionary(counts); ThreadStaticPools.ReturnUlongIntDictionary(counts);
counts = null; counts = null;
} }
@@ -1231,6 +1235,7 @@ internal static partial class PrimeOrderCalculator
if (counts is not null) if (counts is not null)
{ {
counts.Clear();
ThreadStaticPools.ReturnUlongIntDictionary(counts); ThreadStaticPools.ReturnUlongIntDictionary(counts);
} }
+1 -26
View File
@@ -129,11 +129,6 @@ namespace PerfectNumbers.Core
int lastIndex = poolCount - 1; int lastIndex = poolCount - 1;
List<ulong> list = pool[lastIndex]; List<ulong> list = pool[lastIndex];
pool.RemoveAt(lastIndex); pool.RemoveAt(lastIndex);
if (list.Count > 0)
{
list.Clear();
}
if (list.Capacity < capacityHint) if (list.Capacity < capacityHint)
{ {
list.Capacity = capacityHint; list.Capacity = capacityHint;
@@ -174,11 +169,6 @@ namespace PerfectNumbers.Core
int lastIndex = poolCount - 1; int lastIndex = poolCount - 1;
List<PrimeOrderCalculator.PendingEntry> list = pool[lastIndex]; List<PrimeOrderCalculator.PendingEntry> list = pool[lastIndex];
pool.RemoveAt(lastIndex); pool.RemoveAt(lastIndex);
if (list.Count > 0)
{
list.Clear();
}
if (list.Capacity < capacityHint) if (list.Capacity < capacityHint)
{ {
list.Capacity = capacityHint; list.Capacity = capacityHint;
@@ -219,14 +209,8 @@ namespace PerfectNumbers.Core
int lastIndex = poolCount - 1; int lastIndex = poolCount - 1;
Stack<ulong> stack = pool[lastIndex]; Stack<ulong> stack = pool[lastIndex];
pool.RemoveAt(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); stack.EnsureCapacity(capacityHint);
} }
@@ -266,11 +250,6 @@ namespace PerfectNumbers.Core
int lastIndex = poolCount - 1; int lastIndex = poolCount - 1;
Dictionary<ulong, int> dictionary = pool[lastIndex]; Dictionary<ulong, int> dictionary = pool[lastIndex];
pool.RemoveAt(lastIndex); pool.RemoveAt(lastIndex);
if (dictionary.Count > 0)
{
dictionary.Clear();
}
dictionary.EnsureCapacity(capacityHint); dictionary.EnsureCapacity(capacityHint);
return dictionary; return dictionary;
@@ -306,14 +285,12 @@ namespace PerfectNumbers.Core
} }
_factorCountDictionary = null; _factorCountDictionary = null;
dictionary.Clear();
return dictionary; return dictionary;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnFactorCountDictionary(Dictionary<ulong, int> dictionary) public static void ReturnFactorCountDictionary(Dictionary<ulong, int> dictionary)
{ {
dictionary.Clear();
_factorCountDictionary = dictionary; _factorCountDictionary = dictionary;
} }
@@ -330,14 +307,12 @@ namespace PerfectNumbers.Core
} }
_factorScratchDictionary = null; _factorScratchDictionary = null;
dictionary.Clear();
return dictionary; return dictionary;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnFactorScratchDictionary(Dictionary<ulong, int> dictionary) public static void ReturnFactorScratchDictionary(Dictionary<ulong, int> dictionary)
{ {
dictionary.Clear();
_factorScratchDictionary = dictionary; _factorScratchDictionary = dictionary;
} }