add a parallel construction benchmark to src/main.rs

This commit is contained in:
Qyriad 2024-11-12 14:05:07 -07:00
parent 8fa9ad5b48
commit fc68d01d2b

View file

@ -3,7 +3,7 @@ use std::time::Instant;
use clap::Parser;
use miden_crypto::{
hash::rpo::{Rpo256, RpoDigest},
merkle::{MerkleError, Smt},
merkle::{MerkleError, NodeIndex, Smt},
Felt, Word, ONE,
};
use rand_utils::rand_value;
@ -33,7 +33,9 @@ pub fn benchmark_smt() {
entries.push((key, value));
}
let mut tree = construction(entries, tree_size).unwrap();
let mut tree = construction(entries.clone(), tree_size).unwrap();
let parallel = parallel_construction(entries, tree_size).unwrap();
assert_eq!(tree, parallel);
insertion(&mut tree, tree_size).unwrap();
batched_insertion(&mut tree, tree_size).unwrap();
proof_generation(&mut tree, tree_size).unwrap();
@ -56,6 +58,31 @@ pub fn construction(entries: Vec<(RpoDigest, Word)>, size: u64) -> Result<Smt, M
Ok(tree)
}
pub fn parallel_construction(
entries: Vec<(RpoDigest, Word)>,
size: u64,
) -> Result<Smt, MerkleError> {
println!("Running a parallel construction benchmark:");
let now = Instant::now();
let (inner_nodes, leaves) = Smt::build_subtrees(entries);
let root = inner_nodes.get(&NodeIndex::root()).unwrap().hash();
let leaves = leaves.into_iter().map(|(key, value)| (key.value(), value)).collect();
let tree = Smt::from_raw_parts(inner_nodes, leaves, root)?;
let elapsed = now.elapsed();
println!(
"Parallel-constructed an SMT with {} key-value pairs in {:.3} seconds",
size,
elapsed.as_secs_f32(),
);
println!("Number of leaf nodes: {}\n", tree.leaves().count());
Ok(tree)
}
/// Runs the insertion benchmark for the [`Smt`].
pub fn insertion(tree: &mut Smt, size: u64) -> Result<(), MerkleError> {
println!("Running an insertion benchmark:");