miden-crypto/benches/smt-with-entries.rs
benches/smt-with-entries.rsRaw
1use std::{fmt::Debug, hint, mem, time::Duration};
2
3use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
4use miden_crypto::{hash::rpo::RpoDigest, merkle::Smt, Felt, Word, ONE};
5use rand_utils::prng_array;
6use winter_utils::Randomizable;
7
8// 2^0, 2^4, 2^8, 2^12, 2^16
9const PAIR_COUNTS: [u64; 6] = [1, 16, 256, 4096, 65536, 1_048_576];
10
11fn smt_with_entries(c: &mut Criterion) {
12 let mut seed = [0u8; 32];
13
14 let mut group = c.benchmark_group("smt-with-entries");
15
16 for pair_count in PAIR_COUNTS {
17 let bench_id = BenchmarkId::from_parameter(pair_count);
18 group.bench_with_input(bench_id, &pair_count, |b, &pair_count| {
19 b.iter_batched(
20 || {
21 // Setup.
22 prepare_entries(pair_count, &mut seed)
23 },
24 |entries| {
25 // Benchmarked function.
26 Smt::with_entries(hint::black_box(entries)).unwrap();
27 },
28 BatchSize::SmallInput,
29 );
30 });
31 }
32}
33
34criterion_group! {
35 name = smt_with_entries_group;
36 config = Criterion::default()
37 //.measurement_time(Duration::from_secs(960))
38 .measurement_time(Duration::from_secs(60))
39 .sample_size(10)
40 .configure_from_args();
41 targets = smt_with_entries
42}
43criterion_main!(smt_with_entries_group);
44
45// HELPER FUNCTIONS
46// --------------------------------------------------------------------------------------------
47
48fn prepare_entries(pair_count: u64, seed: &mut [u8; 32]) -> Vec<(RpoDigest, [Felt; 4])> {
49 let entries: Vec<(RpoDigest, Word)> = (0..pair_count)
50 .map(|i| {
51 let count = pair_count as f64;
52 let idx = ((i as f64 / count) * (count)) as u64;
53 let key = RpoDigest::new([generate_value(seed), ONE, Felt::new(i), Felt::new(idx)]);
54 let value = generate_word(seed);
55 (key, value)
56 })
57 .collect();
58 entries
59}
60
61fn generate_value<T: Copy + Debug + Randomizable>(seed: &mut [u8; 32]) -> T {
62 mem::swap(seed, &mut prng_array(*seed));
63 let value: [T; 1] = rand_utils::prng_array(*seed);
64 value[0]
65}
66
67fn generate_word(seed: &mut [u8; 32]) -> Word {
68 mem::swap(seed, &mut prng_array(*seed));
69 let nums: [u64; 4] = prng_array(*seed);
70 [Felt::new(nums[0]), Felt::new(nums[1]), Felt::new(nums[2]), Felt::new(nums[3])]
71}