Skip to content

Commit

Permalink
simple merkle benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Mar 25, 2024
1 parent 16ee392 commit 1a950fc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
blake2 = "0.10.6"
blake3 = "1.5.0"
hex = "0.4.3"
itertools = "0.12.0"
num-traits = "0.2.17"
thiserror = "1.0.56"
merging-iterator = "1.3.0"
bytemuck = { version = "1.14.3", features = ["derive"] }
blake2 = "0.10.6"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down Expand Up @@ -56,6 +56,10 @@ name = "matrix"
name = "merkle_bench"
harness = false

[[bench]]
name = "merkle"
harness = false

[[bench]]
name = "fri"
harness = false
Expand Down
40 changes: 40 additions & 0 deletions benches/merkle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![feature(iter_array_chunks)]

use criterion::Criterion;

#[cfg(target_arch = "x86_64")]
pub fn cpu_merkle(c: &mut criterion::Criterion) {
use itertools::Itertools;
use num_traits::Zero;
use stwo::commitment_scheme::ops::MerkleOps;
use stwo::core::backend::CPUBackend;
use stwo::core::fields::m31::BaseField;

const N_COLS: usize = 1 << 8;
const LOG_SIZE: u32 = 20;
let cols = (0..N_COLS)
.map(|_| {
(0..(1 << LOG_SIZE))
.map(|_| BaseField::zero())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();

let mut group = c.benchmark_group("merkle throughput");
group.throughput(criterion::Throughput::Elements((N_COLS << LOG_SIZE) as u64));
group.throughput(criterion::Throughput::Bytes(
(N_COLS << (LOG_SIZE + 2)) as u64,
));
group.bench_function("cpu merkle", |b| {
b.iter(|| {
CPUBackend::commit_on_layer(LOG_SIZE, None, &cols.iter().collect_vec());
})
});
}

#[cfg(target_arch = "x86_64")]
criterion::criterion_group!(
name=merkle;
config = Criterion::default().sample_size(10);
targets=cpu_merkle);
criterion::criterion_main!(merkle);
21 changes: 1 addition & 20 deletions benches/merkle_bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO(Ohad): write better benchmarks. Reduce the variance in sample size.
use blake2::{Blake2s256, Digest};
use criterion::measurement::WallTime;
use criterion::{
black_box, criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion,
Expand Down Expand Up @@ -82,20 +81,6 @@ fn compare_blakes(c: &mut Criterion) {
group.finish();
}

fn single_blake2s_hash_benchmark(c: &mut Criterion) {
let input = [0u8; 1];
c.bench_function("Single blake2s hash", |b| {
b.iter_batched(
|| -> Blake2s256 { Blake2s256::new() },
|mut h| {
h.update(&input[..]);
h.finalize()
},
BatchSize::SmallInput,
)
});
}

fn single_blake3_hash_benchmark(c: &mut Criterion) {
let input = [0u8; 1];
c.bench_function("Single blake3 hash", |b| b.iter(|| blake3::hash(&input)));
Expand All @@ -109,10 +94,6 @@ criterion_group!(

criterion_group!(comparisons, compare_blakes,);

criterion_group!(
single_hash,
single_blake2s_hash_benchmark,
single_blake3_hash_benchmark,
);
criterion_group!(single_hash, single_blake3_hash_benchmark,);

criterion_main!(comparisons);

0 comments on commit 1a950fc

Please sign in to comment.