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 24, 2024
1 parent b0c69c9 commit f596373
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 89 deletions.
68 changes: 0 additions & 68 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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"
Expand Down Expand Up @@ -56,6 +55,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 f596373

Please sign in to comment.