From 36068f0cb6e2f331a38f45730f19deaa8e6ad23c Mon Sep 17 00:00:00 2001 From: Shahar Papini Date: Fri, 22 Mar 2024 15:28:55 +0200 Subject: [PATCH] simple merkle benchmark --- Cargo.lock | 68 ----------------------------------------- Cargo.toml | 5 ++- benches/merkle.rs | 40 ++++++++++++++++++++++++ benches/merkle_bench.rs | 21 +------------ 4 files changed, 45 insertions(+), 89 deletions(-) create mode 100644 benches/merkle.rs diff --git a/Cargo.lock b/Cargo.lock index 420074800..5d1d80827 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,15 +41,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "blake2" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" -dependencies = [ - "digest", -] - [[package]] name = "blake3" version = "1.5.0" @@ -63,15 +54,6 @@ dependencies = [ "constant_time_eq", ] -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "bumpalo" version = "3.15.3" @@ -241,43 +223,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", - "subtle", -] - [[package]] name = "either" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.12" @@ -581,7 +532,6 @@ dependencies = [ name = "stwo" version = "0.1.1" dependencies = [ - "blake2", "blake3", "bytemuck", "criterion", @@ -593,12 +543,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "subtle" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" - [[package]] name = "syn" version = "2.0.52" @@ -640,24 +584,12 @@ dependencies = [ "serde_json", ] -[[package]] -name = "typenum" -version = "1.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" - [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - [[package]] name = "walkdir" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index 43bad56e4..ef9be7051 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -56,6 +55,10 @@ name = "matrix" name = "merkle_bench" harness = false +[[bench]] +name = "merkle" +harness = false + [[bench]] name = "fri" harness = false diff --git a/benches/merkle.rs b/benches/merkle.rs new file mode 100644 index 000000000..c465f4c93 --- /dev/null +++ b/benches/merkle.rs @@ -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::>() + }) + .collect::>(); + + 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); diff --git a/benches/merkle_bench.rs b/benches/merkle_bench.rs index d85bc8e8f..2a442aa44 100644 --- a/benches/merkle_bench.rs +++ b/benches/merkle_bench.rs @@ -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, @@ -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))); @@ -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);