From fa2e76d397a4be6d98d3a43f4df923fa592523ea Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 6 Aug 2026 14:43:09 +0200 Subject: [PATCH] bench: Add base_blob compare bench via uint256 Contributed by Sjors in https://github.com/bitcoin/bitcoin/pull/35896#discussion_r3722884353 This benchmark can be run on top of any earlier commit by applying the diff of this commit before building. --- src/bench/CMakeLists.txt | 1 + src/bench/uint256_blob.cpp | 91 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/bench/uint256_blob.cpp diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt index 3c81e7986da..45ba7c4058f 100644 --- a/src/bench/CMakeLists.txt +++ b/src/bench/CMakeLists.txt @@ -52,6 +52,7 @@ add_executable(bench_bitcoin strencodings.cpp txgraph.cpp txorphanage.cpp + uint256_blob.cpp util_time.cpp verify_script.cpp ) diff --git a/src/bench/uint256_blob.cpp b/src/bench/uint256_blob.cpp new file mode 100644 index 00000000000..fba0a0e8388 --- /dev/null +++ b/src/bench/uint256_blob.cpp @@ -0,0 +1,91 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://opensource.org/license/mit. + +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +enum class Difference { + NONE, + FIRST_BYTE, + LAST_BYTE, +}; + +constexpr size_t NUM_PAIRS{4'096}; + +std::vector> MakePairs(Difference difference) +{ + FastRandomContext rng{/*fDeterministic=*/true}; + std::vector> pairs; + pairs.reserve(NUM_PAIRS); + + for (size_t i{0}; i < NUM_PAIRS; ++i) { + uint256 lhs{rng.rand256()}; + uint256 rhs{lhs}; + if (difference != Difference::NONE) { + const size_t position{difference == Difference::FIRST_BYTE ? 0 : uint256::size() - 1}; + lhs.begin()[position] = i % 2 == 0 ? 0 : 255; + rhs.begin()[position] = i % 2 == 0 ? 255 : 0; + } + pairs.emplace_back(lhs, rhs); + } + return pairs; +} + +template +void Comparison(benchmark::Bench& bench, Difference difference, Comparator comparator) +{ + const auto pairs{MakePairs(difference)}; + bench.batch(pairs.size()).unit("comparison").run([&] { + for (const auto& [lhs, rhs] : pairs) { + ankerl::nanobench::doNotOptimizeAway(comparator(lhs, rhs)); + } + }); +} + +void Uint256EqualIdentical(benchmark::Bench& bench) +{ + Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; }); +} + +void Uint256EqualFirstByteDifferent(benchmark::Bench& bench) +{ + Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; }); +} + +void Uint256EqualLastByteDifferent(benchmark::Bench& bench) +{ + Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs == rhs; }); +} + +void Uint256LessIdentical(benchmark::Bench& bench) +{ + Comparison(bench, Difference::NONE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; }); +} + +void Uint256LessFirstByteDifferent(benchmark::Bench& bench) +{ + Comparison(bench, Difference::FIRST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; }); +} + +void Uint256LessLastByteDifferent(benchmark::Bench& bench) +{ + Comparison(bench, Difference::LAST_BYTE, [](const uint256& lhs, const uint256& rhs) { return lhs < rhs; }); +} + +} // namespace + +BENCHMARK(Uint256EqualIdentical); +BENCHMARK(Uint256EqualFirstByteDifferent); +BENCHMARK(Uint256EqualLastByteDifferent); +BENCHMARK(Uint256LessIdentical); +BENCHMARK(Uint256LessFirstByteDifferent); +BENCHMARK(Uint256LessLastByteDifferent);