Merge pull request #135 from mlemanczyk/codex/optimize-prime-filtering-method
Optimize filtering strategy for previous Mersenne results
This commit is contained in:
1 file changed
+28
-1
@@ -38,6 +38,9 @@ public static class MersenneNumberDivisorByDivisorTester
|
|||||||
if (previousResults is not null && previousResults.Count > 0)
|
if (previousResults is not null && previousResults.Count > 0)
|
||||||
{
|
{
|
||||||
int recordedCount = previousResults.Count;
|
int recordedCount = previousResults.Count;
|
||||||
|
bool preferSortedMerge = recordedCount <= 4096 || (recordedCount * 4L) <= candidateCount;
|
||||||
|
if (preferSortedMerge)
|
||||||
|
{
|
||||||
ulong[] recordedCandidates = ArrayPool<ulong>.Shared.Rent(recordedCount);
|
ulong[] recordedCandidates = ArrayPool<ulong>.Shared.Rent(recordedCount);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -75,7 +78,7 @@ public static class MersenneNumberDivisorByDivisorTester
|
|||||||
if (writeIndex < candidateCount)
|
if (writeIndex < candidateCount)
|
||||||
{
|
{
|
||||||
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
|
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
|
||||||
candidateSpan = CollectionsMarshal.AsSpan(candidates);
|
candidateSpan = CollectionsMarshal.AsSpan(candidates); // candidateSpan now tracks the reduced list.
|
||||||
candidateCount = candidateSpan.Length;
|
candidateCount = candidateSpan.Length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +87,30 @@ public static class MersenneNumberDivisorByDivisorTester
|
|||||||
ArrayPool<ulong>.Shared.Return(recordedCandidates, clearArray: true);
|
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)
|
if (skippedByPreviousResults > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user