From faab5aa1f8dc4d641a2fee707a33875ff26b3f45 Mon Sep 17 00:00:00 2001 From: Mac L Date: Wed, 30 Aug 2023 13:44:05 +1000 Subject: [PATCH 1/3] Add bench for tree_hash_root --- Cargo.toml | 4 ++++ benches/tree_hash_root.rs | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 benches/tree_hash_root.rs diff --git a/Cargo.toml b/Cargo.toml index c93ac12..37cd146 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,7 @@ criterion = "0.3" [[bench]] name = "rebase" harness = false + +[[bench]] +name = "tree_hash_root" +harness = false diff --git a/benches/tree_hash_root.rs b/benches/tree_hash_root.rs new file mode 100644 index 0000000..ea7e460 --- /dev/null +++ b/benches/tree_hash_root.rs @@ -0,0 +1,46 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use milhouse::{List, Value, Vector}; +use tree_hash::{Hash256, TreeHash}; +use typenum::Unsigned; + +type C = typenum::U1099511627776; +type D = typenum::U1000000; +const N: u64 = 800_000; +const M: u64 = 1_000_000; + +#[inline] +fn tree_hash_root_list(l1: &List) -> Hash256 { + l1.tree_hash_root() +} + +#[inline] +fn tree_hash_root_vector(v1: &Vector) -> Hash256 { + v1.tree_hash_root() +} + +pub fn tree_hash_root(c: &mut Criterion) { + let list_size = N; + let vector_size = M; + + let list_1 = List::::try_from_iter(0..list_size).unwrap(); + let vector_1 = Vector::::try_from_iter(0..vector_size).unwrap(); + + c.bench_with_input( + BenchmarkId::new("tree_hash_root_list", list_size), + &(list_1), + |b, l1| { + b.iter(|| tree_hash_root_list(l1)); + }, + ); + + c.bench_with_input( + BenchmarkId::new("tree_hash_root_vector", vector_size), + &(vector_1), + |b, l1| { + b.iter(|| tree_hash_root_vector(l1)); + }, + ); +} + +criterion_group!(benches, tree_hash_root); +criterion_main!(benches); From a81f91cac29ebda5cd613ef8f8669e4ecd96d9b8 Mon Sep 17 00:00:00 2001 From: Mac L Date: Wed, 30 Aug 2023 13:48:51 +1000 Subject: [PATCH 2/3] Fix naming --- benches/tree_hash_root.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benches/tree_hash_root.rs b/benches/tree_hash_root.rs index ea7e460..393bb3d 100644 --- a/benches/tree_hash_root.rs +++ b/benches/tree_hash_root.rs @@ -36,8 +36,8 @@ pub fn tree_hash_root(c: &mut Criterion) { c.bench_with_input( BenchmarkId::new("tree_hash_root_vector", vector_size), &(vector_1), - |b, l1| { - b.iter(|| tree_hash_root_vector(l1)); + |b, v1| { + b.iter(|| tree_hash_root_vector(v1)); }, ); } From 876b5a87ab3f2d35b82aabaae66f0da49ecc942b Mon Sep 17 00:00:00 2001 From: Mac L Date: Wed, 30 Aug 2023 16:21:26 +1000 Subject: [PATCH 3/3] Fix benches --- benches/tree_hash_root.rs | 57 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/benches/tree_hash_root.rs b/benches/tree_hash_root.rs index 393bb3d..e331fc1 100644 --- a/benches/tree_hash_root.rs +++ b/benches/tree_hash_root.rs @@ -1,43 +1,46 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; -use milhouse::{List, Value, Vector}; -use tree_hash::{Hash256, TreeHash}; -use typenum::Unsigned; +use milhouse::{List, Vector}; +use ssz_types::VariableList; +use tree_hash::TreeHash; type C = typenum::U1099511627776; type D = typenum::U1000000; -const N: u64 = 800_000; -const M: u64 = 1_000_000; - -#[inline] -fn tree_hash_root_list(l1: &List) -> Hash256 { - l1.tree_hash_root() -} - -#[inline] -fn tree_hash_root_vector(v1: &Vector) -> Hash256 { - v1.tree_hash_root() -} +const N: u64 = 1_000_000; pub fn tree_hash_root(c: &mut Criterion) { - let list_size = N; - let vector_size = M; + let size = N; - let list_1 = List::::try_from_iter(0..list_size).unwrap(); - let vector_1 = Vector::::try_from_iter(0..vector_size).unwrap(); + c.bench_with_input( + BenchmarkId::new("tree_hash_root_list", size), + &size, + |b, &size| { + b.iter(|| { + let l1 = List::::try_from_iter(0..size).unwrap(); + l1.tree_hash_root() + }); + }, + ); c.bench_with_input( - BenchmarkId::new("tree_hash_root_list", list_size), - &(list_1), - |b, l1| { - b.iter(|| tree_hash_root_list(l1)); + BenchmarkId::new("tree_hash_root_vector", size), + &size, + |b, &size| { + b.iter(|| { + let v1 = Vector::::try_from_iter(0..size).unwrap(); + v1.tree_hash_root() + }); }, ); + // Test `VariableList` as a point of comparison. c.bench_with_input( - BenchmarkId::new("tree_hash_root_vector", vector_size), - &(vector_1), - |b, v1| { - b.iter(|| tree_hash_root_vector(v1)); + BenchmarkId::new("tree_hash_root_ssz_types_list", size), + &size, + |b, &size| { + b.iter(|| { + let l1 = VariableList::::new((0..size).collect()).unwrap(); + l1.tree_hash_root() + }) }, ); }