-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore!: remove mutable mmr #5954
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ fn create_smt() -> SparseMerkleTree<Blake2b<U32>> { | |
SparseMerkleTree::<Blake2b<U32>>::new() | ||
} | ||
|
||
pub fn benchmark_sparse_merkle_trees(c: &mut Criterion) { | ||
pub fn benchmark_smt_insert(c: &mut Criterion) { | ||
let sizes = [100, 10_000]; | ||
for size in sizes { | ||
c.bench_function(&format!("SMT: Insert {size} keys"), move |b| { | ||
|
@@ -38,5 +38,57 @@ pub fn benchmark_sparse_merkle_trees(c: &mut Criterion) { | |
} | ||
} | ||
|
||
criterion_group!(smt, benchmark_sparse_merkle_trees); | ||
fn insert_into_smt(keys: &[NodeKey], tree: &mut SparseMerkleTree<Blake2b<U32>>) { | ||
keys.iter().for_each(|key| { | ||
tree.upsert(key.clone(), ValueHash::default()).unwrap(); | ||
}); | ||
} | ||
|
||
fn delete_from_smt(keys: &[NodeKey], tree: &mut SparseMerkleTree<Blake2b<U32>>) { | ||
keys.iter().for_each(|key| { | ||
tree.delete(key).unwrap(); | ||
}); | ||
} | ||
|
||
fn time_function(header: &str, f: impl FnOnce()) -> std::time::Duration { | ||
println!("Starting: {header}"); | ||
let now = std::time::Instant::now(); | ||
f(); | ||
let t = now.elapsed(); | ||
println!("Finished: {header} - {t:?}"); | ||
t | ||
} | ||
|
||
pub fn root_hash(_c: &mut Criterion) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these operations be refactored to use Criterion's |
||
let size = 1_000_000; | ||
let half_size = size / 2; | ||
let keys = get_keys(size); | ||
let mut tree = create_smt(); | ||
time_function(&format!("SMT: Inserting {size} keys"), || { | ||
insert_into_smt(&keys, &mut tree); | ||
}); | ||
time_function("SMT: Calculating root hash", || { | ||
let size = tree.size(); | ||
let hash = tree.hash(); | ||
println!("Tree size: {size}. Root hash: {hash:x}"); | ||
}); | ||
time_function(&format!("SMT: Deleting {half_size} keys"), || { | ||
delete_from_smt(&keys[0..half_size], &mut tree); | ||
}); | ||
time_function("SMT: Calculating root hash", || { | ||
let size = tree.size(); | ||
let hash = tree.hash(); | ||
println!("Tree size: {size}. Root hash: {hash:x}"); | ||
}); | ||
time_function(&format!("SMT: Deleting another {half_size} keys"), || { | ||
delete_from_smt(&keys[half_size..], &mut tree); | ||
}); | ||
time_function("SMT: Calculating root hash", || { | ||
let size = tree.size(); | ||
let hash = tree.hash(); | ||
println!("Tree size: {size}. Root hash: {hash:x}"); | ||
}); | ||
} | ||
|
||
criterion_group!(smt, benchmark_smt_insert, root_hash); | ||
criterion_main!(smt); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this more closely, I think there's a subtle error in this benchmark. A fresh tree is created for each sample, but then each iteration of that sample applies the
upsert
operations to the same tree. This means the first iteration will perform inserts, and subsequent iterations will perform updates. The timing data will only be accurate if an insert and update take the same amount of time.