From 8b11ea6bbeaf11e22ae9d52eb7a4ce626ccadcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 17 Aug 2026 11:32:09 +0200 Subject: [PATCH] test: Fix the modarith benchmarks measuring dead code The benchmarked values are unused after the loop, so the compiler is free to delete the loop body: the addition and subtraction benchmarks measured an empty loop. The multiplication survived only because its call was not inlined. Consume the final values with DoNotOptimize. --- test/internal_benchmarks/modarith_bench.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/internal_benchmarks/modarith_bench.cpp b/test/internal_benchmarks/modarith_bench.cpp index 36032bb4af..8b2ca8e1dc 100644 --- a/test/internal_benchmarks/modarith_bench.cpp +++ b/test/internal_benchmarks/modarith_bench.cpp @@ -12,6 +12,8 @@ namespace constexpr auto bn254 = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47_u256; constexpr auto secp256k1 = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f_u256; +// Each pair of operations forms a single dependency chain, so the reported time is the latency. + template void modarith_add(benchmark::State& state) { @@ -24,6 +26,8 @@ void modarith_add(benchmark::State& state) a = m.add(a, b); b = m.add(b, a); } + benchmark::DoNotOptimize(a); + benchmark::DoNotOptimize(b); } template @@ -38,6 +42,8 @@ void modarith_sub(benchmark::State& state) a = m.sub(a, b); b = m.sub(b, a); } + benchmark::DoNotOptimize(a); + benchmark::DoNotOptimize(b); } template @@ -52,6 +58,8 @@ void modarith_mul(benchmark::State& state) a = m.mul(a, b); b = m.mul(b, a); } + benchmark::DoNotOptimize(a); + benchmark::DoNotOptimize(b); } } // namespace