Merge pull request #66 from mlemanczyk/codex/analyze-locking-mechanisms-in-evenperfectbitscanner

Trim PrintResult critical work further
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-09-22 20:16:47 +02:00
commit a8c0e5537f
1 file changed
+81 -27
+81 -27
View File
@@ -23,6 +23,7 @@ internal static class Program
private const string PrimeFoundSuffix = " (FOUND VALID CANDIDATES)"; private const string PrimeFoundSuffix = " (FOUND VALID CANDIDATES)";
private static StringBuilder? _outputBuilder; private static StringBuilder? _outputBuilder;
private static readonly object Sync = new(); private static readonly object Sync = new();
private static readonly object FileWriteSync = new();
private static int _consoleCounter; private static int _consoleCounter;
private static int _writeIndex; private static int _writeIndex;
private static int _primeCount; private static int _primeCount;
@@ -1349,6 +1350,51 @@ internal static class Program
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryAcquireConsoleSlot(bool primeCandidate, out bool primeFlag)
{
int consoleInterval = PerfectNumberConstants.ConsoleInterval;
int counterValue = Interlocked.Increment(ref _consoleCounter);
if (counterValue >= consoleInterval)
{
int previous = Interlocked.Exchange(ref _consoleCounter, 0);
if (previous >= consoleInterval)
{
primeFlag = primeCandidate;
return true;
}
}
primeFlag = false;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static StringBuilder? AppendToOutputBuffer(StringBuilder source)
{
StringBuilder? builderToFlush = null;
lock (Sync)
{
StringBuilder outputBuilder = _outputBuilder!;
_ = outputBuilder.Append(source);
int nextIndex = _writeIndex + 1;
if (nextIndex >= _writeBatchSize)
{
builderToFlush = outputBuilder;
_outputBuilder = StringBuilderPool.Rent();
_writeIndex = 0;
}
else
{
_writeIndex = nextIndex;
}
}
return builderToFlush;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void PrintResult(ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests) private static void PrintResult(ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests)
{ {
@@ -1362,7 +1408,7 @@ internal static class Program
} }
bool lastWasComposite = _lastCompositeP; bool lastWasComposite = _lastCompositeP;
bool primeFlag = false, printToConsole = false; bool primeCandidate = passedAllTests && !lastWasComposite && Volatile.Read(ref _primeFoundAfterInit);
StringBuilder localBuilder = StringBuilderPool.Rent(); StringBuilder localBuilder = StringBuilderPool.Rent();
_ = localBuilder _ = localBuilder
@@ -1371,26 +1417,12 @@ internal static class Program
.Append(detailedCheck).Append(',') .Append(detailedCheck).Append(',')
.Append(passedAllTests).Append('\n'); .Append(passedAllTests).Append('\n');
lock (Sync) bool printToConsole = TryAcquireConsoleSlot(primeCandidate, out bool primeFlag);
{ StringBuilder? builderToFlush = AppendToOutputBuffer(localBuilder);
if (_consoleCounter >= PerfectNumberConstants.ConsoleInterval)
{
printToConsole = true;
primeFlag = passedAllTests && !lastWasComposite && _primeFoundAfterInit;
_consoleCounter = 0;
}
else
{
_consoleCounter++;
}
_ = _outputBuilder!.Append(localBuilder); if (builderToFlush is not null)
_writeIndex++;
if (_writeIndex >= _writeBatchSize)
{ {
FlushBuffer(); FlushBuffer(builderToFlush);
}
} }
if (printToConsole) if (printToConsole)
@@ -1413,19 +1445,41 @@ internal static class Program
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void FlushBuffer() private static void FlushBuffer()
{ {
var stream = new FileStream(ResultsFileName, FileMode.Append, FileAccess.Write, FileShare.Read); StringBuilder? builderToFlush = null;
var writer = new StreamWriter(stream) { AutoFlush = false };
if (_writeIndex > 0) lock (Sync)
{ {
writer.Write(_outputBuilder); if (_writeIndex == 0 || _outputBuilder is null || _outputBuilder.Length == 0)
writer.Flush(); {
_outputBuilder!.Clear(); return;
}
builderToFlush = _outputBuilder;
_outputBuilder = StringBuilderPool.Rent();
_writeIndex = 0; _writeIndex = 0;
} }
writer.Dispose(); FlushBuffer(builderToFlush!);
stream.Dispose(); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void FlushBuffer(StringBuilder builderToFlush)
{
try
{
lock (FileWriteSync)
{
using FileStream stream = new(ResultsFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
using StreamWriter writer = new(stream) { AutoFlush = false };
writer.Write(builderToFlush);
writer.Flush();
}
}
finally
{
builderToFlush.Clear();
StringBuilderPool.Return(builderToFlush);
}
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]