Skip to content

Commit 380e1f4

Browse files
committed
Merge bitcoin/bitcoin#30349: benchmark: Improve SipHash_32b accuracy to avoid potential optimization issues
42066f4 Refactor SipHash_32b benchmark to improve accuracy and avoid optimization issues (Lőrinc) Pull request description: This PR stems from the discussions in bitcoin/bitcoin#30317 (comment) 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 42066f4 achow101: ACK 42066f4 hodlinator: ACK 42066f4 Tree-SHA512: 6bbe9d725d4c3396642e55ce48c31baa5339e56838d6d5fb377fb1069daa9292375e7020ceff7da0d78befffc1e984f717b5232217fe911989613480adaa937e
2 parents 1a8f51e + 42066f4 commit 380e1f4

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)