Merge bitcoin/bitcoin#30349: benchmark: Improve SipHash_32b accuracy to avoid potential optimization issues

42066f45ff Refactor SipHash_32b benchmark to improve accuracy and avoid optimization issues (Lőrinc)

Pull request description:

  This PR stems from the discussions in https://github.com/bitcoin/bitcoin/pull/30317#discussion_r1649187336

  The previous benchmark for `SipHash` was slightly less accurate in representing real-world usage and allowed for potential compiler optimizations that could invalidate the benchmark.
  This change aims to ensure the benchmark produces more realistic results.

  By modifying the initial values and only incrementing the bytes of `val`, the benchmark should reflects a more typical usage patterns - and prevent the compiler from optimizing away the calculations.

  -------

  On my M1 processor the benchmark's speed changed significantly (but the CI seems to produce the same result as before):

  > cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_BENCH=ON && cmake --build build -j10 &&
  ./build/src/bench/bench_bitcoin --filter=SipHash_32b --min-time=1000

  Before:
  |               ns/op |                op/s |    err% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------:|:----------
  |               35.15 |       28,445,856.66 |    0.2% |      1.10 | `SipHash_32b`

  After (note that only the benchmark changed):
  |               ns/op |                op/s |    err% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------:|:----------
  |               22.05 |       45,350,886.64 |    0.3% |      1.10 | `SipHash_32b`

ACKs for top commit:
  maflcko:
    review ACK 42066f45ff
  achow101:
    ACK 42066f45ff
  hodlinator:
    ACK 42066f45ff

Tree-SHA512: 6bbe9d725d4c3396642e55ce48c31baa5339e56838d6d5fb377fb1069daa9292375e7020ceff7da0d78befffc1e984f717b5232217fe911989613480adaa937e
This commit is contained in:
Ava Chow 2024-11-14 16:36:33 -05:00
commit 380e1f44e8
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41

View File

@ -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;
});
}