Skip to content

Commit

Permalink
Refactor SipHash_32b benchmark to improve accuracy and avoid optimiza…
Browse files Browse the repository at this point in the history
…tion 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.
  • Loading branch information
l0rinc committed Nov 3, 2024
1 parent 2123c94 commit 42066f4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/bench/crypto_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

Expand Down

0 comments on commit 42066f4

Please sign in to comment.