From 42066f45ff5d48e78a317eda63c035809bd657c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Thu, 27 Jun 2024 15:53:08 +0200 Subject: [PATCH] Refactor SipHash_32b benchmark to improve accuracy and avoid optimization issues - Modify `SipHash_32b` benchmark to use `FastRandomContext` for generating initial values. - Cycle through and modify each byte of the `uint256` value to ensure no part of it can be optimized away. The lack of "recursion" (where the method call overwrites the used inputs partially) and the systematic modification of each input byte makes the benchmark usage more reliable and thorough. --- src/bench/crypto_hash.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/bench/crypto_hash.cpp b/src/bench/crypto_hash.cpp index 65da942ad7c..a0799aea7ad 100644 --- a/src/bench/crypto_hash.cpp +++ b/src/bench/crypto_hash.cpp @@ -192,10 +192,16 @@ static void SHA512(benchmark::Bench& bench) static void SipHash_32b(benchmark::Bench& bench) { - uint256 x; - uint64_t k1 = 0; + FastRandomContext rng{/*fDeterministic=*/true}; + auto k0{rng.rand64()}, k1{rng.rand64()}; + auto val{rng.rand256()}; + auto i{0U}; bench.run([&] { - *((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x); + ankerl::nanobench::doNotOptimizeAway(SipHashUint256(k0, k1, val)); + ++k0; + ++k1; + ++i; + val.data()[i % uint256::size()] ^= i & 0xFF; }); }