Merge pull request #66 from mlemanczyk/codex/analyze-locking-mechanisms-in-evenperfectbitscanner
Trim PrintResult critical work further
This commit is contained in:
1 file changed
+81
-27
@@ -23,6 +23,7 @@ internal static class Program
|
||||
private const string PrimeFoundSuffix = " (FOUND VALID CANDIDATES)";
|
||||
private static StringBuilder? _outputBuilder;
|
||||
private static readonly object Sync = new();
|
||||
private static readonly object FileWriteSync = new();
|
||||
private static int _consoleCounter;
|
||||
private static int _writeIndex;
|
||||
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)]
|
||||
private static void PrintResult(ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests)
|
||||
{
|
||||
@@ -1362,7 +1408,7 @@ internal static class Program
|
||||
}
|
||||
|
||||
bool lastWasComposite = _lastCompositeP;
|
||||
bool primeFlag = false, printToConsole = false;
|
||||
bool primeCandidate = passedAllTests && !lastWasComposite && Volatile.Read(ref _primeFoundAfterInit);
|
||||
|
||||
StringBuilder localBuilder = StringBuilderPool.Rent();
|
||||
_ = localBuilder
|
||||
@@ -1371,26 +1417,12 @@ internal static class Program
|
||||
.Append(detailedCheck).Append(',')
|
||||
.Append(passedAllTests).Append('\n');
|
||||
|
||||
lock (Sync)
|
||||
{
|
||||
if (_consoleCounter >= PerfectNumberConstants.ConsoleInterval)
|
||||
{
|
||||
printToConsole = true;
|
||||
primeFlag = passedAllTests && !lastWasComposite && _primeFoundAfterInit;
|
||||
_consoleCounter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_consoleCounter++;
|
||||
}
|
||||
bool printToConsole = TryAcquireConsoleSlot(primeCandidate, out bool primeFlag);
|
||||
StringBuilder? builderToFlush = AppendToOutputBuffer(localBuilder);
|
||||
|
||||
_ = _outputBuilder!.Append(localBuilder);
|
||||
|
||||
_writeIndex++;
|
||||
if (_writeIndex >= _writeBatchSize)
|
||||
if (builderToFlush is not null)
|
||||
{
|
||||
FlushBuffer();
|
||||
}
|
||||
FlushBuffer(builderToFlush);
|
||||
}
|
||||
|
||||
if (printToConsole)
|
||||
@@ -1413,19 +1445,41 @@ internal static class Program
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void FlushBuffer()
|
||||
{
|
||||
var stream = new FileStream(ResultsFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||
var writer = new StreamWriter(stream) { AutoFlush = false };
|
||||
StringBuilder? builderToFlush = null;
|
||||
|
||||
if (_writeIndex > 0)
|
||||
lock (Sync)
|
||||
{
|
||||
writer.Write(_outputBuilder);
|
||||
writer.Flush();
|
||||
_outputBuilder!.Clear();
|
||||
if (_writeIndex == 0 || _outputBuilder is null || _outputBuilder.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
builderToFlush = _outputBuilder;
|
||||
_outputBuilder = StringBuilderPool.Rent();
|
||||
_writeIndex = 0;
|
||||
}
|
||||
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
FlushBuffer(builderToFlush!);
|
||||
}
|
||||
|
||||
[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)]
|
||||
|
||||
Reference in new issue
Block a user