diff --git a/Cargo.toml b/Cargo.toml index 98da1ac3..5d124c68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,10 +27,6 @@ harness = false name = "smt" harness = false -[[bench]] -name = "smt-mutations" -harness = false - [[bench]] name = "store" harness = false diff --git a/benches/smt-mutations.rs b/benches/smt-mutations.rs deleted file mode 100644 index 77db4cce..00000000 --- a/benches/smt-mutations.rs +++ /dev/null @@ -1,145 +0,0 @@ -use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; -use miden_crypto::{ - merkle::{LeafIndex, SimpleSmt}, - Felt, Word, EMPTY_WORD, -}; -use rand::{prelude::SliceRandom, rngs::StdRng, Rng, SeedableRng}; - -const DEPTH: u8 = 64; - -fn benchmark_compute_mutations(c: &mut Criterion) { - let mut group = c.benchmark_group("compute_mutations"); - group.sample_size(10); - group.measurement_time(std::time::Duration::from_secs(300)); - - // Fixed seed for reproducibility - let rng_seed = 42; - let mut rng = StdRng::seed_from_u64(rng_seed); - - // Benchmark for various mutation set sizes - for &mutation_count in &[10_000] { - group.bench_with_input( - BenchmarkId::new("SimpleSmt: compute_mutations", mutation_count), - &mutation_count, - |b, &mutation_count| { - // Batch-based benchmarking - b.iter_batched( - || generate_tree_and_mutations(&mut rng, mutation_count), - |(smt, mutations)| { - // Compute mutations in the benchmark to measure execution time - let _mutations = smt.compute_mutations(mutations); - }, - BatchSize::SmallInput, - ); - }, - ); - } - - group.finish(); -} - -fn benchmark_apply_mutations(c: &mut Criterion) { - let mut group = c.benchmark_group("apply_mutations"); - group.sample_size(10); - group.measurement_time(std::time::Duration::from_secs(300)); - - // Fixed seed for reproducibility - let rng_seed = 42; - let mut rng = StdRng::seed_from_u64(rng_seed); - - // Benchmark for various mutation set sizes - for &mutation_count in &[10_000] { - group.bench_with_input( - BenchmarkId::new("SimpleSmt: apply_mutations", mutation_count), - &mutation_count, - |b, &mutation_count| { - // Batch-based benchmarking - b.iter_batched( - || { - let (smt, mutation_kv_pairs) = - generate_tree_and_mutations(&mut rng, mutation_count); - - // Compute mutations - let mutations = smt.compute_mutations(mutation_kv_pairs); - - (smt, mutations) - }, - |(mut smt, mutations)| { - // Apply mutations in the benchmark to measure execution time - smt.apply_mutations(mutations).unwrap(); - }, - BatchSize::SmallInput, - ); - }, - ); - } - - group.finish(); -} - -criterion_group!(benches, benchmark_compute_mutations, benchmark_apply_mutations); -criterion_main!(benches); - -// HELPER FUNCTIONS -// ================================================================================================= - -/// Helper function to generate initial `SimpleSmt` tree with random keys and values and mutation -/// key-value pairs -fn generate_tree_and_mutations( - rng: &mut StdRng, - mutation_count: usize, -) -> (SimpleSmt, Vec<(LeafIndex, Word)>) { - const INITIAL_FILL_COUNT: usize = 1_000_000; - const REMOVAL_PROBABILITY: f64 = 0.2; - - // Initialize the tree with initial random values - let initial_kv_pairs: Vec<_> = (0..INITIAL_FILL_COUNT) - .map(|_| { - let key: u64 = rng.gen(); - let value = generate_word(rng); - - (key, value) - }) - .collect(); - - // Select and change a half of pairs from the filled tree (values to be - // updated or removed with given probability) - let mut mutation_kv_pairs: Vec<_> = initial_kv_pairs - .choose_multiple(rng, mutation_count / 2) - .cloned() - .map(|(key, _value)| { - let value = if rng.gen_bool(REMOVAL_PROBABILITY) { - EMPTY_WORD - } else { - generate_word(rng) - }; - - (key, value) - }) - .collect(); - - // Append another half of new values (values to be added) - for _ in 0..mutation_count / 2 { - mutation_kv_pairs.push((rng.gen(), generate_word(rng))); - } - - let smt = SimpleSmt::::with_leaves(initial_kv_pairs).unwrap(); - - let mutations: Vec<_> = mutation_kv_pairs - .into_iter() - .map(|(key, value)| (LeafIndex::new(key).unwrap(), value)) - .collect(); - - (smt, mutations) -} - -/// Helper function to generate random `Word` -fn generate_word(rng: &mut StdRng) -> Word { - // Random Word value - [ - Felt::new(rng.gen()), - Felt::new(rng.gen()), - Felt::new(rng.gen()), - Felt::new(rng.gen()), - ] -} diff --git a/src/main.rs b/src/main.rs index cdd8b03b..24293daf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -216,7 +216,7 @@ pub fn proof_generation(tree: &mut Smt) -> Result<(), MerkleError> { println!( "An average proving time measured by {NUM_PROOFS} value proofs in an SMT with {size} \ - key-value pairs in {:.3} microseconds", + leaves in {:.3} microseconds", // calculate the average insertion_times.iter().sum::() as f64 / (NUM_PROOFS as f64), ); diff --git a/src/merkle/smt/full/tests.rs b/src/merkle/smt/full/tests.rs index 8ed5af45..6404f294 100644 --- a/src/merkle/smt/full/tests.rs +++ b/src/merkle/smt/full/tests.rs @@ -511,7 +511,7 @@ fn test_mutations_revert() { assert_eq!(revert.old_root, smt.root(), "reverse mutations old root did not match"); assert_eq!(revert.root(), original.root(), "reverse mutations new root did not match"); - let _ = smt.apply_mutations(revert).unwrap(); + smt.apply_mutations(revert).unwrap(); assert_eq!(smt, original, "SMT with applied revert mutations did not match original SMT"); }