|
| 1 | +use std::collections::HashSet; |
| 2 | +use std::rc::Rc; |
1 | 3 | use std::time::Duration;
|
2 | 4 |
|
3 | 5 | use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
|
@@ -151,9 +153,80 @@ fn append(c: &mut Criterion) {
|
151 | 153 | );
|
152 | 154 | }
|
153 | 155 |
|
| 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 | + |
154 | 227 | criterion_group!(
|
155 | 228 | name = benches;
|
156 | 229 | config = Criterion::default().warm_up_time(Duration::from_secs(10));
|
157 |
| - targets = append, remove_mark |
| 230 | + targets = append, remove_mark, update |
158 | 231 | );
|
159 | 232 | criterion_main!(benches);
|
0 commit comments