benches: benchmark merkle root computation
What changed, and why it matters
This commit only adds a new performance benchmark for calculating Bitcoin merkle tree roots. It does not change any library code, fix bugs, or alter behavior. There is no security relevance.
No action required; this is a benign benchmark addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change registers a new Criterion benchmark target in benches/Cargo.toml and adds benches/bitcoin/merkle_tree.rs, which benchmarks TxMerkleNode::calculate_root over synthetic Txid leaf sets of sizes 1000, 1001, 9000, 9001, and 64000. It is purely additive benchmarking code with no modifications to the rust-bitcoin library implementation.
Changed components
benches/Cargo.tomlbenches/bitcoin/merkle_tree.rsInspect captured patch +39 / −0
diff --git a/benches/Cargo.toml b/benches/Cargo.toml
index 3a71c72c..4e93f541 100644
--- a/benches/Cargo.toml
+++ b/benches/Cargo.toml
@@ -37,6 +37,11 @@ name = "duplicate_inputs"
path = "bitcoin/duplicate_inputs.rs"
harness = false
+[[bench]]
+name = "merkle_tree"
+path = "bitcoin/merkle_tree.rs"
+harness = false
+
[[bench]]
name = "chacha20poly1305"
diff --git a/benches/bitcoin/merkle_tree.rs b/benches/bitcoin/merkle_tree.rs
new file mode 100644
index 00000000..de70d17c
--- /dev/null
+++ b/benches/bitcoin/merkle_tree.rs
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: CC0-1.0
+
+use std::hint::black_box;
+
+use bitcoin::{Txid, TxMerkleNode};
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+
+fn make_leaves(count: usize) -> Vec<Txid> {
+ (0..count as u32)
+ .map(|i| {
+ let mut buf = [0u8; 32];
+ buf[..4].copy_from_slice(&i.to_le_bytes());
+ Txid::from_byte_array(buf)
+ })
+ .collect()
+}
+
+fn bench_merkle_root_computation(c: &mut Criterion) {
+ let mut g = c.benchmark_group("merkle_root");
+
+ for &size in &[1000, 1001, 9000, 9001, 64000] {
+ let leaves = make_leaves(size);
+
+ g.throughput(Throughput::Elements(size as u64));
+ g.bench_function(BenchmarkId::new("compute", size), |b| {
+ b.iter(|| black_box(TxMerkleNode::calculate_root(leaves.iter().copied())));
+ });
+ }
+
+ g.finish();
+}
+
+criterion_group!(benches, bench_merkle_root_computation);
+criterion_main!(benches);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.