Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
bench
merkle_root.cpp
Go to the documentation of this file.
1
// Copyright (c) 2016-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
bench/bench.h
>
6
#include <
consensus/merkle.h
>
7
#include <random.h>
8
#include <
uint256.h
>
9
10
#include <cassert>
11
#include <vector>
12
13
static
void
MerkleRoot
(
benchmark::Bench
& bench)
14
{
15
FastRandomContext
rng{
/*fDeterministic=*/
true
};
16
17
std::vector<uint256> hashes{};
18
hashes.resize(9001);
19
for
(
auto
& item : hashes) {
20
item = rng.
rand256
();
21
}
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.reserve((hashes.size() + 1) & ~1ULL);
// capacity rounded up to even
28
for
(
const
auto
& hash : hashes) {
29
leaves.push_back(hash);
30
}
31
32
bool
mutated{
false
};
33
const
uint256
root{
ComputeMerkleRoot
(std::move(leaves), mutate ? &mutated :
nullptr
)};
34
assert
(root == expected_root);
35
});
36
}
37
}
38
39
BENCHMARK
(
MerkleRoot
);
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:68
FastRandomContext
Fast randomness source.
Definition
random.h:386
RandomMixin::rand256
uint256 rand256() noexcept
generate a random uint256.
Definition
random.h:317
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:627
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition
nanobench.h:1234
ankerl::nanobench::Bench::name
ANKERL_NANOBENCH(NODISCARD) std Bench & name(char const *benchmarkName)
Gets the title of the benchmark.
ankerl::nanobench::Bench::batch
Bench & batch(T b) noexcept
Sets the batch size.
Definition
nanobench.h:1258
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
uint256
256-bit opaque blob.
Definition
uint256.h:195
ComputeMerkleRoot
uint256 ComputeMerkleRoot(std::vector< uint256 > hashes, bool *mutated)
Definition
merkle.cpp:46
merkle.h
MerkleRoot
static void MerkleRoot(benchmark::Bench &bench)
Definition
merkle_root.cpp:13
uint256.h
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0