Skip to content

Commit efcaf2b

Browse files
committed
Add update benches
1 parent f6efda6 commit efcaf2b

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

benches/leanbridgetree.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
use std::rc::Rc;
13
use std::time::Duration;
24

35
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
@@ -151,9 +153,80 @@ fn append(c: &mut Criterion) {
151153
);
152154
}
153155

156+
fn update(c: &mut Criterion) {
157+
for tree_size in [1000u64, 10_000, 50_000, 100_000] {
158+
c.bench_function(
159+
&format!("build the tree ({tree_size} leaves) by calling append"),
160+
|b| {
161+
let marked_pos = Rc::new(
162+
(0u64..tree_size)
163+
.filter(|pos| pos & 0b11 == 0)
164+
.map(incrementalmerkletree::Position::from)
165+
.collect::<HashSet<_>>(),
166+
);
167+
168+
b.iter_batched(
169+
|| marked_pos.clone(),
170+
|marked_pos| {
171+
let mut tree = BridgeTree::<String, 32>::new();
172+
173+
for position in (0u64..tree_size).map(incrementalmerkletree::Position::from)
174+
{
175+
tree.append("a".to_string()).unwrap();
176+
if marked_pos.contains(&position) {
177+
tree.mark().unwrap();
178+
}
179+
}
180+
},
181+
BatchSize::SmallInput,
182+
)
183+
},
184+
);
185+
c.bench_function(
186+
&format!("build the tree ({tree_size} leaves) by calling update"),
187+
|b| {
188+
let marked_pos = Rc::new(
189+
(0u64..tree_size)
190+
.filter(|pos| pos & 0b11 == 0)
191+
.map(incrementalmerkletree::Position::from)
192+
.collect::<Vec<_>>(),
193+
);
194+
let mut frontiers = Vec::with_capacity(tree_size.try_into().unwrap());
195+
let mut frontier = incrementalmerkletree::frontier::NonEmptyFrontier::from_parts(
196+
0u64.into(),
197+
"a".to_string(),
198+
vec![],
199+
)
200+
.unwrap();
201+
202+
// prefill tree
203+
for _ in 1..tree_size {
204+
frontiers.push(frontier.clone());
205+
frontier.append("a".to_string());
206+
}
207+
208+
drop(frontier);
209+
let frontiers = Rc::new(frontiers);
210+
211+
b.iter_batched(
212+
|| (frontiers.clone(), marked_pos.clone()),
213+
|(frontiers, marked_pos)| {
214+
let mut tree = BridgeTree::<String, 32>::new();
215+
216+
unsafe {
217+
tree.update(&frontiers, &marked_pos).unwrap();
218+
}
219+
},
220+
BatchSize::SmallInput,
221+
)
222+
},
223+
);
224+
}
225+
}
226+
154227
criterion_group!(
155228
name = benches;
156229
config = Criterion::default().warm_up_time(Duration::from_secs(10));
157-
targets = append, remove_mark
230+
targets = append, remove_mark, update
158231
);
159232
criterion_main!(benches);

0 commit comments

Comments
 (0)