Skip to content

Commit 42066f4

Browse files
committed
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.
1 parent 2123c94 commit 42066f4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/bench/crypto_hash.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,16 @@ static void SHA512(benchmark::Bench& bench)
192192

193193
static void SipHash_32b(benchmark::Bench& bench)
194194
{
195-
uint256 x;
196-
uint64_t k1 = 0;
195+
FastRandomContext rng{/*fDeterministic=*/true};
196+
auto k0{rng.rand64()}, k1{rng.rand64()};
197+
auto val{rng.rand256()};
198+
auto i{0U};
197199
bench.run([&] {
198-
*((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
200+
ankerl::nanobench::doNotOptimizeAway(SipHashUint256(k0, k1, val));
201+
++k0;
202+
++k1;
203+
++i;
204+
val.data()[i % uint256::size()] ^= i & 0xFF;
199205
});
200206
}
201207

0 commit comments

Comments
 (0)