From 82737b99fd40ac61b47dcb97d255c43dc492cde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Thu, 11 Sep 2025 19:07:50 +0200 Subject: [PATCH] Micro-optimizations for the best performance in BuildDivisorCandidates.. --- EvenPerfectBitScanner/Program.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/EvenPerfectBitScanner/Program.cs b/EvenPerfectBitScanner/Program.cs index d3144eb..0fc5a23 100644 --- a/EvenPerfectBitScanner/Program.cs +++ b/EvenPerfectBitScanner/Program.cs @@ -916,10 +916,10 @@ internal static class Program private static (ulong divisor, uint cycle)[] BuildDivisorCandidates() { uint[] snapshot = MersenneDivisorCycles.Shared.ExportSmallCyclesSnapshot(); - List<(ulong divisor, uint cycle)> list = new(snapshot.Length / 2); - ulong d; + Span<(ulong divisor, uint cycle)> list = stackalloc (ulong divisor, uint cycle)[snapshot.Length / 2];// new(snapshot.Length / 2); uint cycle; - for (int i = 3; i < snapshot.Length; i += 2) + int count = 0, i, snapshotLength = snapshot.Length; + for (i = 3; i < snapshotLength; i += 2) { cycle = snapshot[i]; if (cycle == 0U) @@ -927,11 +927,10 @@ internal static class Program continue; } - d = (ulong)i; - list.Add((d, cycle)); + list[count++] = ((ulong)i, cycle); } - return list.ToArray(); + return list[..count].ToArray(); } private static bool IsDivisible(ulong exponent, ulong divisor)