//! [`MerkleTree::get_leaves_mut`] Benchmark //! //! [`merkletree::get_leaves_mut`]: https://docs.rs/merkle-lite/latest/merkle_lite/struct.MerkleTree.html#get_leaves_mut #[macro_use] extern crate bencher; use bencher::Bencher; use digest::generic_array::{ArrayLength, GenericArray}; use digest::{typenum, Digest, OutputSizeUser}; use merkle_lite::MerkleTree; const NR_LEAVES: usize = 100_000; const NR_LEAVES_CHANGE_DENSE_STEP: usize = NR_LEAVES / 100; // 100 changes total. const NR_LEAVES_CHANGE_SPARSE_STEP: usize = NR_LEAVES / 10; // 10 changes total. benchmark_main!(sha2, sha3); // With [SHA2] hash functions. // // [sha2]: https://crates.io/crates/sha3 benchmark_group!( sha2, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, ); // With [SHA3] hash functions. // // [sha3]: https://crates.io/crates/sha3 benchmark_group!( sha3, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_all::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_dense::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, tree_get_leaves_mut_sparse::, ); fn tree_get_leaves_mut_all(b: &mut Bencher) where N: ArrayLength, B: Digest + OutputSizeUser, >::ArrayType: Copy, <::OutputSize as ArrayLength>::ArrayType: Copy, { tree_get_leaves_mut_step::(b, 1); } fn tree_get_leaves_mut_dense(b: &mut Bencher) where N: ArrayLength, B: Digest + OutputSizeUser, >::ArrayType: Copy, <::OutputSize as ArrayLength>::ArrayType: Copy, { tree_get_leaves_mut_step::(b, NR_LEAVES_CHANGE_DENSE_STEP); } fn tree_get_leaves_mut_sparse(b: &mut Bencher) where N: ArrayLength, B: Digest + OutputSizeUser, >::ArrayType: Copy, <::OutputSize as ArrayLength>::ArrayType: Copy, { tree_get_leaves_mut_step::(b, NR_LEAVES_CHANGE_SPARSE_STEP); } fn tree_get_leaves_mut_step(b: &mut Bencher, step: usize) where N: ArrayLength, B: Digest + OutputSizeUser, >::ArrayType: Copy, <::OutputSize as ArrayLength>::ArrayType: Copy, { let leaves = vec![GenericArray::::default(); NR_LEAVES]; let mut tree = MerkleTree::::from_iter(leaves.iter()); b.iter(|| { { // Calculates the Merkle root with the single leaf change. let mut leaves_mut = tree.get_leaves_mut(); for i in (0..NR_LEAVES).step_by(step) { leaves_mut[i] = GenericArray::::default(); } } assert_eq!(tree.leaf_len(), NR_LEAVES); }) }