|
| 1 | +//! Merkle root computation benchmarks for the AVX2-capable reducer. |
| 2 | +// PERF: Criterion emits public harness items whose docs are irrelevant to the benchmark report. |
| 3 | +#![allow(missing_docs)] |
| 4 | + |
| 5 | +use std::hint::black_box; |
| 6 | + |
| 7 | +use bitcoin::consensus::Encodable as _; |
| 8 | +use bitcoin::hashes::Hash as _; |
| 9 | +use bitcoin::{TxMerkleNode, Txid}; |
| 10 | +use bitcoin_rs_consensus::verify_block::block_merkle_root_matches_txids; |
| 11 | +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; |
| 12 | + |
| 13 | +fn make_txids(count: usize) -> Vec<Txid> { |
| 14 | + (0..count) |
| 15 | + .map(|i| { |
| 16 | + let mut bytes = [0u8; 32]; |
| 17 | + let value = match u32::try_from(i) { |
| 18 | + Ok(value) => value, |
| 19 | + Err(error) => panic!("benchmark size must fit u32: {error}"), |
| 20 | + }; |
| 21 | + bytes[0..4].copy_from_slice(&value.to_le_bytes()); |
| 22 | + Txid::from_byte_array(bytes) |
| 23 | + }) |
| 24 | + .collect() |
| 25 | +} |
| 26 | + |
| 27 | +fn scalar_merkle(level: &mut Vec<Txid>) -> Option<(Txid, bool)> { |
| 28 | + if level.is_empty() { |
| 29 | + return None; |
| 30 | + } |
| 31 | + let mut mutated = false; |
| 32 | + while level.len() > 1 { |
| 33 | + mutated |= level.chunks_exact(2).any(|pair| pair[0] == pair[1]); |
| 34 | + let original_len = level.len(); |
| 35 | + for parent in 0..original_len.div_ceil(2) { |
| 36 | + let left = level[2 * parent]; |
| 37 | + let right = level[(2 * parent + 1).min(original_len - 1)]; |
| 38 | + let mut engine = Txid::engine(); |
| 39 | + assert!( |
| 40 | + left.consensus_encode(&mut engine).is_ok(), |
| 41 | + "in-memory hash engine write failed" |
| 42 | + ); |
| 43 | + assert!( |
| 44 | + right.consensus_encode(&mut engine).is_ok(), |
| 45 | + "in-memory hash engine write failed" |
| 46 | + ); |
| 47 | + level[parent] = Txid::from_engine(engine); |
| 48 | + } |
| 49 | + level.truncate(original_len.div_ceil(2)); |
| 50 | + } |
| 51 | + Some((level[0], mutated)) |
| 52 | +} |
| 53 | + |
| 54 | +fn benchmark_root(input: &[Txid]) -> TxMerkleNode { |
| 55 | + match bitcoin::merkle_tree::calculate_root(input.iter().copied()) { |
| 56 | + Some(root) => TxMerkleNode::from(root), |
| 57 | + None => panic!("benchmark inputs must be nonempty"), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn benchmark_block(merkle_root: TxMerkleNode) -> bitcoin::Block { |
| 62 | + bitcoin::Block { |
| 63 | + header: bitcoin::block::Header { |
| 64 | + version: bitcoin::block::Version::ONE, |
| 65 | + prev_blockhash: bitcoin::BlockHash::all_zeros(), |
| 66 | + merkle_root, |
| 67 | + time: 0, |
| 68 | + bits: bitcoin::CompactTarget::from_consensus(0), |
| 69 | + nonce: 0, |
| 70 | + }, |
| 71 | + txdata: Vec::new(), |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn validate_benchmark_input(block: &bitcoin::Block, input: &[Txid]) { |
| 76 | + let mut candidate = input.to_vec(); |
| 77 | + assert!(block_merkle_root_matches_txids(block, &mut candidate)); |
| 78 | + |
| 79 | + let mut scalar = input.to_vec(); |
| 80 | + let expected = Txid::from_byte_array(block.header.merkle_root.to_byte_array()); |
| 81 | + assert_eq!(scalar_merkle(&mut scalar), Some((expected, false))); |
| 82 | +} |
| 83 | + |
| 84 | +fn merkle_tree(c: &mut Criterion) { |
| 85 | + let mut group = c.benchmark_group("merkle"); |
| 86 | + for &leaf_count in &[1, 2, 15, 16, 17, 31, 32, 33] { |
| 87 | + let input = make_txids(leaf_count); |
| 88 | + let root = benchmark_root(&input); |
| 89 | + let block = benchmark_block(root); |
| 90 | + validate_benchmark_input(&block, &input); |
| 91 | + let mut scratch = input.clone(); |
| 92 | + group.bench_function(BenchmarkId::new("avx2_dispatch_leaves", leaf_count), |b| { |
| 93 | + b.iter(|| { |
| 94 | + scratch.clone_from(&input); |
| 95 | + black_box(block_merkle_root_matches_txids(&block, &mut scratch)); |
| 96 | + }); |
| 97 | + }); |
| 98 | + group.bench_function(BenchmarkId::new("scalar_leaves", leaf_count), |b| { |
| 99 | + b.iter(|| { |
| 100 | + scratch.clone_from(&input); |
| 101 | + black_box(scalar_merkle(&mut scratch)); |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + for &parent_count in &[8, 64, 1024] { |
| 106 | + let leaf_count = parent_count * 2; |
| 107 | + let input = make_txids(leaf_count); |
| 108 | + let root = benchmark_root(&input); |
| 109 | + let block = benchmark_block(root); |
| 110 | + validate_benchmark_input(&block, &input); |
| 111 | + let mut scratch = input.clone(); |
| 112 | + group.bench_function( |
| 113 | + BenchmarkId::new("avx2_dispatch_parents", parent_count), |
| 114 | + |b| { |
| 115 | + b.iter(|| { |
| 116 | + scratch.clone_from(&input); |
| 117 | + black_box(block_merkle_root_matches_txids(&block, &mut scratch)); |
| 118 | + }); |
| 119 | + }, |
| 120 | + ); |
| 121 | + group.bench_function(BenchmarkId::new("scalar_parents", parent_count), |b| { |
| 122 | + b.iter(|| { |
| 123 | + scratch.clone_from(&input); |
| 124 | + black_box(scalar_merkle(&mut scratch)); |
| 125 | + }); |
| 126 | + }); |
| 127 | + } |
| 128 | + group.finish(); |
| 129 | +} |
| 130 | + |
| 131 | +criterion_group!(benches, merkle_tree); |
| 132 | +criterion_main!(benches); |
0 commit comments