From f80f21b917ab430fee6b094a8150fee064ceace0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 00:06:35 +0200 Subject: [PATCH 1/6] poseidon and pedersen extended benchmarks --- .gitignore | 1 + crates/starknet-types-core/Cargo.toml | 3 +- .../benches/criterion_hashes.rs | 108 ++++++++++++++++++ .../benches/criterion_pedersen.rs | 24 ---- 4 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 crates/starknet-types-core/benches/criterion_hashes.rs delete mode 100644 crates/starknet-types-core/benches/criterion_pedersen.rs diff --git a/.gitignore b/.gitignore index 2d01884..b77ac50 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +.idea/* # Added by cargo diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 4406dd0..34a3b67 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -58,5 +58,6 @@ rand = "0.8.5" lazy_static = { version = "1.4.0", default-features = false } [[bench]] -name = "criterion_pedersen" +name = "criterion_hashes" harness = false + diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs new file mode 100644 index 0000000..69b9773 --- /dev/null +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -0,0 +1,108 @@ +use criterion::{BatchSize, BenchmarkId, black_box, Criterion, criterion_group, criterion_main}; +use rand::{RngCore, SeedableRng}; +use rand_chacha::ChaCha8Rng; + +use starknet_types_core::felt::Felt; +use starknet_types_core::hash::{Pedersen, Poseidon}; +use starknet_types_core::hash::StarkHash; + +const SEED: u64 = 3; + +fn rnd_felt(rng: &mut ChaCha8Rng) -> Felt { + let mut felt: [u8; 32] = Default::default(); + rng.fill_bytes(&mut felt); + Felt::from_bytes_be(&felt) +} + +fn rnd_felts(rng: &mut ChaCha8Rng, n: usize) -> Vec { + (0..n).map(|_| rnd_felt(rng)).collect() +} + +fn rnd_pair_of_felts(rng: &mut ChaCha8Rng) -> (Felt, Felt) { + (rnd_felt(rng), rnd_felt(rng)) +} + +fn poseidon_hash_single_input(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Poseidon"); + + let (x, y) = rnd_pair_of_felts(&mut rng); + + group.bench_function("hash - single input", |bench| { + bench.iter(|| black_box(Poseidon::hash(&x, &y))) + }); +} + +fn poseidon_hash_multiple_inputs(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Poseidon"); + + group.bench_function("hash - multiple inputs", |bench| { + bench.iter_batched( + || rnd_pair_of_felts(&mut rng), + |(x, y)| black_box(Poseidon::hash(&x, &y)), + BatchSize::SmallInput + ); + }); +} + +fn poseidon_hash_array(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + + let mut group = c.benchmark_group("Poseidon"); + + for n in [2, 4, 8, 16].iter() { + group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { + bench.iter_batched( + || rnd_felts(&mut rng, n), + |felts| black_box(Poseidon::hash_array(&felts)), + BatchSize::SmallInput + ); + }); + } +} + +fn pedersen_hash_single_input(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Pedersen"); + + let (x, y) = rnd_pair_of_felts(&mut rng); + + group.bench_function("hash - single input", |bench| { + bench.iter(|| black_box(Pedersen::hash(&x, &y))) + }); +} +fn pedersen_hash_multiple_inputs(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + let mut group = c.benchmark_group("Pedersen"); + + group.bench_function("hash - multiple inputs", |bench| { + bench.iter_batched( + || rnd_pair_of_felts(&mut rng), + |(x, y)| black_box(Pedersen::hash(&x, &y)), + BatchSize::SmallInput + ); + }); +} + +fn pedersen_hash_array(c: &mut Criterion) { + let mut rng = ChaCha8Rng::seed_from_u64(SEED); + + let mut group = c.benchmark_group("Pedersen"); + + for n in [2, 4, 8, 16].iter() { + group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { + bench.iter_batched( + || rnd_felts(&mut rng, n), + |felts| black_box(Pedersen::hash_array(&felts)), + BatchSize::SmallInput + ); + }); + } +} + +criterion_group!(hashes, + pedersen_hash_single_input, pedersen_hash_multiple_inputs, pedersen_hash_array, + poseidon_hash_single_input, poseidon_hash_multiple_inputs, poseidon_hash_array +); +criterion_main!(hashes); diff --git a/crates/starknet-types-core/benches/criterion_pedersen.rs b/crates/starknet-types-core/benches/criterion_pedersen.rs deleted file mode 100644 index 5821d7f..0000000 --- a/crates/starknet-types-core/benches/criterion_pedersen.rs +++ /dev/null @@ -1,24 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use rand::{RngCore, SeedableRng}; -use rand_chacha::ChaCha8Rng; -use starknet_types_core::felt::Felt; -use starknet_types_core::hash::Pedersen; -use starknet_types_core::hash::StarkHash; - -fn pedersen_benchmarks(c: &mut Criterion) { - let mut rng = ChaCha8Rng::seed_from_u64(2); - let mut felt1: [u8; 32] = Default::default(); - rng.fill_bytes(&mut felt1); - let mut felt2: [u8; 32] = Default::default(); - rng.fill_bytes(&mut felt2); - - let x = Felt::from_bytes_be(&felt1); - let y = Felt::from_bytes_be(&felt2); - let mut group = c.benchmark_group("Pedersen Benchmark"); - - group.bench_function("Hashing with black_box", |bench| { - bench.iter(|| black_box(Pedersen::hash(&x, &y))) - }); -} -criterion_group!(pedersen, pedersen_benchmarks); -criterion_main!(pedersen); From fe31232bd53a1b5244ca557b3ac7fa4856f80696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 13:44:18 +0200 Subject: [PATCH 2/6] increase input sizes --- crates/starknet-types-core/benches/criterion_hashes.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs index 69b9773..285e2f6 100644 --- a/crates/starknet-types-core/benches/criterion_hashes.rs +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -51,7 +51,7 @@ fn poseidon_hash_array(c: &mut Criterion) { let mut group = c.benchmark_group("Poseidon"); - for n in [2, 4, 8, 16].iter() { + for n in [2, 10, 100, 1000].iter() { group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { bench.iter_batched( || rnd_felts(&mut rng, n), @@ -90,7 +90,7 @@ fn pedersen_hash_array(c: &mut Criterion) { let mut group = c.benchmark_group("Pedersen"); - for n in [2, 4, 8, 16].iter() { + for n in [2, 10, 100, 1000].iter() { group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { bench.iter_batched( || rnd_felts(&mut rng, n), From f0a81280f6315a145496658128e23ea22d1c117e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 13:46:17 +0200 Subject: [PATCH 3/6] fix Makefile --- crates/starknet-types-core/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/starknet-types-core/Makefile b/crates/starknet-types-core/Makefile index 5f8628a..1fdb0dc 100644 --- a/crates/starknet-types-core/Makefile +++ b/crates/starknet-types-core/Makefile @@ -7,4 +7,4 @@ test: cargo test bench: - cargo bench --bench criterion_pedersen --features hash + cargo bench --bench criterion_hashes --features hash From cc2032960b49ae294a426a80ebe20763ec6523d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 14:02:51 +0200 Subject: [PATCH 4/6] uniform distribution for rnd_felt --- crates/starknet-types-core/benches/criterion_hashes.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs index 285e2f6..f9d5234 100644 --- a/crates/starknet-types-core/benches/criterion_hashes.rs +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -1,5 +1,5 @@ use criterion::{BatchSize, BenchmarkId, black_box, Criterion, criterion_group, criterion_main}; -use rand::{RngCore, SeedableRng}; +use rand::{Rng, RngCore, SeedableRng}; use rand_chacha::ChaCha8Rng; use starknet_types_core::felt::Felt; @@ -10,7 +10,8 @@ const SEED: u64 = 3; fn rnd_felt(rng: &mut ChaCha8Rng) -> Felt { let mut felt: [u8; 32] = Default::default(); - rng.fill_bytes(&mut felt); + let first_non_zero = rng.gen_range(0..32); + rng.fill_bytes(&mut felt[first_non_zero..32]); Felt::from_bytes_be(&felt) } From 8d7c43fc7b924edeaf957b0bf3f0c824eae30b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 18:23:58 +0200 Subject: [PATCH 5/6] increase hash_array sizes --- crates/starknet-types-core/benches/criterion_hashes.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs index f9d5234..79e3bd5 100644 --- a/crates/starknet-types-core/benches/criterion_hashes.rs +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -52,7 +52,7 @@ fn poseidon_hash_array(c: &mut Criterion) { let mut group = c.benchmark_group("Poseidon"); - for n in [2, 10, 100, 1000].iter() { + for n in [10, 100, 1000, 10000].iter() { group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { bench.iter_batched( || rnd_felts(&mut rng, n), @@ -91,7 +91,7 @@ fn pedersen_hash_array(c: &mut Criterion) { let mut group = c.benchmark_group("Pedersen"); - for n in [2, 10, 100, 1000].iter() { + for n in [10, 100, 1000, 10000].iter() { group.bench_with_input(BenchmarkId::new("hash_array", n), n, |bench, &n| { bench.iter_batched( || rnd_felts(&mut rng, n), From 42c9d4dec6c290e095bf6e4a341e2dbd025873a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kami=C5=84ski?= Date: Wed, 10 Apr 2024 18:28:50 +0200 Subject: [PATCH 6/6] remove single_input versions --- .../benches/criterion_hashes.rs | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/crates/starknet-types-core/benches/criterion_hashes.rs b/crates/starknet-types-core/benches/criterion_hashes.rs index 79e3bd5..3fd5c22 100644 --- a/crates/starknet-types-core/benches/criterion_hashes.rs +++ b/crates/starknet-types-core/benches/criterion_hashes.rs @@ -1,10 +1,10 @@ -use criterion::{BatchSize, BenchmarkId, black_box, Criterion, criterion_group, criterion_main}; +use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; use rand::{Rng, RngCore, SeedableRng}; use rand_chacha::ChaCha8Rng; use starknet_types_core::felt::Felt; -use starknet_types_core::hash::{Pedersen, Poseidon}; use starknet_types_core::hash::StarkHash; +use starknet_types_core::hash::{Pedersen, Poseidon}; const SEED: u64 = 3; @@ -23,26 +23,15 @@ fn rnd_pair_of_felts(rng: &mut ChaCha8Rng) -> (Felt, Felt) { (rnd_felt(rng), rnd_felt(rng)) } -fn poseidon_hash_single_input(c: &mut Criterion) { +fn poseidon_hash(c: &mut Criterion) { let mut rng = ChaCha8Rng::seed_from_u64(SEED); let mut group = c.benchmark_group("Poseidon"); - let (x, y) = rnd_pair_of_felts(&mut rng); - - group.bench_function("hash - single input", |bench| { - bench.iter(|| black_box(Poseidon::hash(&x, &y))) - }); -} - -fn poseidon_hash_multiple_inputs(c: &mut Criterion) { - let mut rng = ChaCha8Rng::seed_from_u64(SEED); - let mut group = c.benchmark_group("Poseidon"); - - group.bench_function("hash - multiple inputs", |bench| { + group.bench_function("hash", |bench| { bench.iter_batched( || rnd_pair_of_felts(&mut rng), |(x, y)| black_box(Poseidon::hash(&x, &y)), - BatchSize::SmallInput + BatchSize::SmallInput, ); }); } @@ -57,31 +46,21 @@ fn poseidon_hash_array(c: &mut Criterion) { bench.iter_batched( || rnd_felts(&mut rng, n), |felts| black_box(Poseidon::hash_array(&felts)), - BatchSize::SmallInput + BatchSize::SmallInput, ); }); } } -fn pedersen_hash_single_input(c: &mut Criterion) { - let mut rng = ChaCha8Rng::seed_from_u64(SEED); - let mut group = c.benchmark_group("Pedersen"); - - let (x, y) = rnd_pair_of_felts(&mut rng); - - group.bench_function("hash - single input", |bench| { - bench.iter(|| black_box(Pedersen::hash(&x, &y))) - }); -} -fn pedersen_hash_multiple_inputs(c: &mut Criterion) { +fn pedersen_hash(c: &mut Criterion) { let mut rng = ChaCha8Rng::seed_from_u64(SEED); let mut group = c.benchmark_group("Pedersen"); - group.bench_function("hash - multiple inputs", |bench| { + group.bench_function("hash", |bench| { bench.iter_batched( || rnd_pair_of_felts(&mut rng), |(x, y)| black_box(Pedersen::hash(&x, &y)), - BatchSize::SmallInput + BatchSize::SmallInput, ); }); } @@ -96,14 +75,17 @@ fn pedersen_hash_array(c: &mut Criterion) { bench.iter_batched( || rnd_felts(&mut rng, n), |felts| black_box(Pedersen::hash_array(&felts)), - BatchSize::SmallInput + BatchSize::SmallInput, ); }); } } -criterion_group!(hashes, - pedersen_hash_single_input, pedersen_hash_multiple_inputs, pedersen_hash_array, - poseidon_hash_single_input, poseidon_hash_multiple_inputs, poseidon_hash_array +criterion_group!( + hashes, + pedersen_hash, + pedersen_hash_array, + poseidon_hash, + poseidon_hash_array ); criterion_main!(hashes);