Optimize filtering strategy for previous Mersenne results

This commit is contained in:
Marek Lemańczyk committed 2025-10-09 05:28:59 +02:00
1 parent 77cc1fc280
commit e1d4bbf12d
1 file changed
+28 -1
@@ -38,6 +38,9 @@ public static class MersenneNumberDivisorByDivisorTester
if (previousResults is not null && previousResults.Count > 0)
{
int recordedCount = previousResults.Count;
bool preferSortedMerge = recordedCount <= 4096 || (recordedCount * 4L) <= candidateCount;
if (preferSortedMerge)
{
ulong[] recordedCandidates = ArrayPool<ulong>.Shared.Rent(recordedCount);
try
{
@@ -75,7 +78,7 @@ public static class MersenneNumberDivisorByDivisorTester
if (writeIndex < candidateCount)
{
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
candidateSpan = CollectionsMarshal.AsSpan(candidates);
candidateSpan = CollectionsMarshal.AsSpan(candidates); // candidateSpan now tracks the reduced list.
candidateCount = candidateSpan.Length;
}
}
@@ -84,6 +87,30 @@ public static class MersenneNumberDivisorByDivisorTester
ArrayPool<ulong>.Shared.Return(recordedCandidates, clearArray: true);
}
}
else
{
int writeIndex = 0;
for (int readIndex = 0; readIndex < candidateCount; readIndex++)
{
ulong candidate = candidateSpan[readIndex];
if (previousResults.ContainsKey(candidate))
{
skippedByPreviousResults++;
continue;
}
candidateSpan[writeIndex++] = candidate;
}
if (writeIndex < candidateCount)
{
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
candidateSpan = CollectionsMarshal.AsSpan(candidates); // candidateSpan now tracks the reduced list.
candidateCount = candidateSpan.Length;
}
}
}
if (skippedByPreviousResults > 0)
{