Skip to content

Commit 1a768c0

Browse files
l0rinchodlinator
andcommitted
bench: make MerkleRoot benchmark more representative
Two versions are run now, one with the mutation calculations, the other without. To avoid unwanted compiler optimizations, we assert the expected hash, which should inhibit aggressive optimization. To make the benchmark more similar to production `ComputeMerkleRoot` call sites, the input leaves-copying is made explicit before each run. > ./build/bin/bench_bitcoin -filter='MerkleRoot.*' -min-time=1000 | ns/leaf | leaf/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 44.26 | 22,592,347.11 | 0.0% | 1.10 | `MerkleRoot` | 44.74 | 22,352,771.64 | 0.1% | 1.10 | `MerkleRootWithMutation` Massif memory measurements show the excessive memory reservations: MB 1.332^ : | # : | # : | # : | # : | # @ : | # @ : | # @ : | # @ : | # @ : | # @ : | # @ : | # @ : | #::::@::::::::::::::::::::::::::::::::::::::::::::::::::::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: | #: ::@::::: :::::::: :: ::: :::::: : : :: ::: ::: : : : ::::@:::::@:::: 0 +----------------------------------------------------------------------->s 0 226.2 showing the reallocations clearly in the stacks: 97.87% (1,366,841B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. ->41.25% (576,064B) 0x969717: allocate (new_allocator.h:151) | ->41.25% (576,064B) 0x969717: allocate (allocator.h:203) | ->41.25% (576,064B) 0x969717: allocate (alloc_traits.h:614) | ->41.25% (576,064B) 0x969717: _M_allocate (stl_vector.h:387) | ->41.25% (576,064B) 0x969717: _M_realloc_append<const uint256&> (vector.tcc:572) | ->41.25% (576,064B) 0x969717: push_back (stl_vector.h:1427) | ->41.25% (576,064B) 0x969717: ComputeMerkleRoot(std::vector<uint256, std::allocator<uint256> >, bool*) (merkle.cpp:55) | ->41.25% (576,064B) 0x2235A7: operator() (merkle_root.cpp:31) | ->41.25% (576,064B) 0x2235A7: ankerl::nanobench::Bench& ankerl::nanobench::Bench::run<MerkleRoot(ankerl::nanobench::Bench&)::{lambda() Co-authored-by: Hodlinator <[email protected]>
1 parent f0a2183 commit 1a768c0

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/bench/merkle_root.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,34 @@
77
#include <random.h>
88
#include <uint256.h>
99

10+
#include <cassert>
1011
#include <vector>
1112

1213
static void MerkleRoot(benchmark::Bench& bench)
1314
{
14-
FastRandomContext rng(true);
15-
std::vector<uint256> leaves;
16-
leaves.resize(9001);
17-
for (auto& item : leaves) {
15+
FastRandomContext rng{/*fDeterministic=*/true};
16+
17+
std::vector<uint256> hashes{};
18+
hashes.resize(9001);
19+
for (auto& item : hashes) {
1820
item = rng.rand256();
1921
}
20-
bench.batch(leaves.size()).unit("leaf").run([&] {
21-
bool mutation = false;
22-
uint256 hash = ComputeMerkleRoot(std::vector<uint256>(leaves), &mutation);
23-
leaves[mutation] = hash;
24-
});
22+
23+
constexpr uint256 expected_root{"d8d4dfd014a533bc3941b8663fa6e7f3a8707af124f713164d75b0c3179ecb08"};
24+
for (bool mutate : {false, true}) {
25+
bench.name(mutate ? "MerkleRootWithMutation" : "MerkleRoot").batch(hashes.size()).unit("leaf").run([&] {
26+
std::vector<uint256> leaves;
27+
leaves.resize(hashes.size());
28+
for (size_t s = 0; s < hashes.size(); s++) {
29+
leaves[s] = hashes[s];
30+
}
31+
32+
bool mutated{false};
33+
const uint256 root{ComputeMerkleRoot(std::move(leaves), mutate ? &mutated : nullptr)};
34+
assert(!mutate || mutated == (root.GetUint64(0) & 1));
35+
assert(root == expected_root);
36+
});
37+
}
2538
}
2639

2740
BENCHMARK(MerkleRoot, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)