diff --git a/hash256-std-hasher/Cargo.toml b/hash256-std-hasher/Cargo.toml index 8649a54c..addd32fb 100644 --- a/hash256-std-hasher/Cargo.toml +++ b/hash256-std-hasher/Cargo.toml @@ -16,7 +16,7 @@ harness = false crunchy = "0.2.1" [dev-dependencies] -criterion = "0.3.3" +criterion = "0.4.0" [features] default = ["std"] diff --git a/hash256-std-hasher/benches/bench.rs b/hash256-std-hasher/benches/bench.rs index 993ac254..87949c38 100644 --- a/hash256-std-hasher/benches/bench.rs +++ b/hash256-std-hasher/benches/bench.rs @@ -12,17 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[macro_use] -extern crate criterion; -use criterion::{black_box, Criterion}; -criterion_group!(benches, write_hash256_std_hasher, write_default_hasher); -criterion_main!(benches); - -extern crate hash256_std_hasher; - +use criterion::{black_box, criterion_group, criterion_main, Criterion}; use hash256_std_hasher::Hash256StdHasher; use std::{collections::hash_map::DefaultHasher, hash::Hasher}; +criterion_group!(benches, write_hash256_std_hasher, write_default_hasher); +criterion_main!(benches); + fn write_hash256_std_hasher(b: &mut Criterion) { b.bench_function("write_hash256_std_hasher", |b| { b.iter(|| { diff --git a/memory-db/Cargo.toml b/memory-db/Cargo.toml index a81f2ee1..f8d61eea 100644 --- a/memory-db/Cargo.toml +++ b/memory-db/Cargo.toml @@ -13,7 +13,7 @@ hashbrown = { version = "0.12.0", default-features = false, features = [ "ahash" [dev-dependencies] keccak-hasher = { path = "../test-support/keccak-hasher" } -criterion = "0.3.3" +criterion = "0.4.0" [features] default = ["std"] diff --git a/memory-db/benches/bench.rs b/memory-db/benches/bench.rs index 8bc400de..b3e0fd9a 100644 --- a/memory-db/benches/bench.rs +++ b/memory-db/benches/bench.rs @@ -12,9 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[macro_use] -extern crate criterion; -use criterion::{black_box, Criterion}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use hash_db::{HashDB, Hasher, EMPTY_PREFIX}; +use keccak_hasher::KeccakHasher; +use memory_db::{HashKey, MemoryDB}; + criterion_group!( benches, instantiation, @@ -25,14 +27,6 @@ criterion_group!( ); criterion_main!(benches); -extern crate hash_db; -extern crate keccak_hasher; -extern crate memory_db; - -use hash_db::{HashDB, Hasher, EMPTY_PREFIX}; -use keccak_hasher::KeccakHasher; -use memory_db::{HashKey, MemoryDB}; - fn instantiation(b: &mut Criterion) { b.bench_function("instantiation", move |b| { b.iter(|| { diff --git a/test-support/reference-trie/Cargo.toml b/test-support/reference-trie/Cargo.toml index 11513a1c..98a350ce 100644 --- a/test-support/reference-trie/Cargo.toml +++ b/test-support/reference-trie/Cargo.toml @@ -9,7 +9,6 @@ edition = "2018" [dependencies] hash-db = { path = "../../hash-db" , version = "0.15.2"} -hash256-std-hasher = { path = "../../hash256-std-hasher", version = "0.15.2" } keccak-hasher = { path = "../keccak-hasher", version = "0.15.3" } trie-db = { path = "../../trie-db", default-features = false, version = "0.24.0" } trie-root = { path = "../../trie-root", default-features = false, version = "0.17.0" } @@ -18,7 +17,7 @@ hashbrown = { version = "0.12.0", default-features = false, features = ["ahash"] [dev-dependencies] trie-bench = { path = "../trie-bench" } -criterion = "0.3.3" +criterion = "0.4.0" [[bench]] name = "bench" diff --git a/test-support/trie-bench/Cargo.toml b/test-support/trie-bench/Cargo.toml index 359e86d4..7de1d8ed 100644 --- a/test-support/trie-bench/Cargo.toml +++ b/test-support/trie-bench/Cargo.toml @@ -14,5 +14,5 @@ hash-db = { path = "../../hash-db" , version = "0.15.2"} memory-db = { path = "../../memory-db", version = "0.31.0" } trie-root = { path = "../../trie-root", version = "0.17.0" } trie-db = { path = "../../trie-db", version = "0.24.0" } -criterion = "0.3.3" +criterion = "0.4.0" parity-scale-codec = "3.0.0" diff --git a/trie-db/fuzz/Cargo.toml b/trie-db/fuzz/Cargo.toml index cf6b6798..e2e66ebc 100644 --- a/trie-db/fuzz/Cargo.toml +++ b/trie-db/fuzz/Cargo.toml @@ -12,7 +12,6 @@ cargo-fuzz = true hash-db = { path = "../../hash-db", version = "0.15.2" } memory-db = { path = "../../memory-db", version = "0.29.0" } reference-trie = { path = "../../test-support/reference-trie", version = "0.26.0" } -keccak-hasher = { path = "../../test-support/keccak-hasher", version = "0.15.2" } [dependencies.trie-db] path = ".." diff --git a/trie-db/test/Cargo.toml b/trie-db/test/Cargo.toml index cb0ee773..5f191dea 100644 --- a/trie-db/test/Cargo.toml +++ b/trie-db/test/Cargo.toml @@ -14,14 +14,11 @@ harness = false [dependencies] trie-db = { path = "..", version = "0.24.0"} hash-db = { path = "../../hash-db", version = "0.15.2"} -rustc-hex = { version = "2.1.0" } memory-db = { path = "../../memory-db", version = "0.31.0" } rand = { version = "0.8", default-features = false, features = ["small_rng"] } -trie-root = { path = "../../trie-root", version = "0.17.0"} trie-standardmap = { path = "../../test-support/trie-standardmap", version = "0.15.2" } -keccak-hasher = { path = "../../test-support/keccak-hasher", version = "0.15.2" } reference-trie = { path = "../../test-support/reference-trie", version = "0.26.0" } hex-literal = "0.3" -criterion = "0.3" -env_logger = "0.9" +criterion = "0.4.0" +env_logger = { version = "0.10", default-features = false } log = "0.4" diff --git a/trie-db/test/benches/bench.rs b/trie-db/test/benches/bench.rs index 670a07fb..bd2611d5 100644 --- a/trie-db/test/benches/bench.rs +++ b/trie-db/test/benches/bench.rs @@ -12,8 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; +use std::collections::BTreeMap; +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; use reference_trie::ExtensionLayout as Layout; use trie_db::{ proof::{generate_proof, verify_proof}, @@ -25,18 +26,12 @@ criterion_group!( benches, root_old, root_new, - root_a_big_v, - root_b_big_v, - root_a_small_v, - root_b_small_v, - trie_mut_ref_root_a, - trie_mut_ref_root_b, - trie_mut_root_a, - trie_mut_root_b, - trie_mut_a, - trie_mut_b, - trie_mut_build_a, - trie_mut_build_b, + root_big_v, + root_small_v, + trie_mut_ref_root, + trie_mut_root, + trie_mut, + trie_mut_build, trie_iteration, nibble_common_prefix, trie_proof_verification, @@ -67,128 +62,82 @@ fn nibble_common_prefix(b: &mut Criterion) { }); } -fn root_a_big_v(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input2(29, 204800 / 2, 512 * 2)]; +fn root_big_v(c: &mut Criterion) { + let params = vec![(29, 204800 / 2, 512 * 2), (29, 204800, 512)]; - c.bench_function_over_inputs( - "root_a_big_v", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); - - reference_trie::calc_root::(inputc); - }) - }, - data, - ); -} - -fn root_b_big_v(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input2(29, 204800, 512)]; + let mut group = c.benchmark_group("root_big_v"); - c.bench_function_over_inputs( - "root_b_big_v", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { + for (seed, len, value_length) in params { + let input = input2(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("root_big_v", param), &input, |b, i| { b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); - + let inputc = i.iter().map(|v| (&v.0, &v.1)).collect::>(); reference_trie::calc_root::(inputc); }) - }, - data, - ); -} - -fn root_a_small_v(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input2(29, 204800, 32)]; - - c.bench_function_over_inputs( - "root_a_small_v", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + }); + } - reference_trie::calc_root::(inputc); - }) - }, - data, - ); + group.finish(); } -fn root_b_small_v(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input2(29, 204800 / 2, 32 * 2)]; +fn root_small_v(c: &mut Criterion) { + let params = vec![(29, 204800, 32), (29, 204800 / 2, 32 * 2)]; + + let mut group = c.benchmark_group("root_small_v"); - c.bench_function_over_inputs( - "root_b_small_v", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { + for (seed, len, value_length) in params { + let input = input2(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("root_small_v", param), &input, |b, i| { b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); - + let inputc = i.iter().map(|v| (&v.0, &v.1)).collect::>(); reference_trie::calc_root::(inputc); }) - }, - data, - ); + }); + } + + group.finish(); } fn root_old(c: &mut Criterion) { - let data: Vec, Vec)>> = - vec![input(1, 5120), input(41, 10240), input(18, 102400), input(29, 204800)]; + let params = vec![(1, 5120), (41, 10240), (18, 102400), (29, 204800)]; - c.bench_function_over_inputs( - "root_old", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); - let inputc = datac.iter().map(|v| (&v.0, &v.1)); + let mut group = c.benchmark_group("root_old"); + for (seed, len) in params { + let input = input(seed, len); + let param = format!("seed({}), len({})", seed, len); + group.bench_with_input(BenchmarkId::new("root_old", param), &input, |b, i| { + b.iter(|| { + let inputc = i.iter().map(|v| (&v.0, &v.1)); reference_trie::reference_trie_root::(inputc); }) - }, - data, - ); + }); + } + + group.finish(); } fn root_new(c: &mut Criterion) { - let data: Vec, Vec)>> = - vec![input(1, 5120), input(41, 10240), input(18, 102400), input(29, 204800)]; + let params = vec![(1, 5120), (41, 10240), (18, 102400), (29, 204800)]; - c.bench_function_over_inputs( - "root_new", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + let mut group = c.benchmark_group("root_new"); - reference_trie::calc_root::(inputc); + for (seed, len) in params { + let input = input(seed, len); + let param = format!("seed({}), len({})", seed, len); + group.bench_with_input(BenchmarkId::new("root_new", param), &input, |b, i| { + b.iter(|| { + let inputc = i.iter().map(|v| (&v.0, &v.1)); + reference_trie::reference_trie_root::(inputc); }) - }, - data, - ); + }); + } + + group.finish(); } fn fuzz_to_data(input: Vec) -> Vec<(Vec, Vec)> { @@ -264,196 +213,97 @@ fn input_unsorted(seed: u64, len: usize, value_length: usize) -> Vec<(Vec, V fuzz_to_data2(data, value_length) } -fn trie_mut_root_a(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input_unsorted(29, 204800 / 2, 512 * 2)]; - - c.bench_function_over_inputs( - "trie_mut_root_a", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data_sorted_unique(data.clone()); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); - - reference_trie::calc_root::(inputc); - }) - }, - data, - ); -} +fn trie_mut_root(c: &mut Criterion) { + let params = vec![(29, 204800 / 2, 512 * 2), (29, 204800, 32)]; -fn trie_mut_root_b(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input_unsorted(29, 204800, 32)]; + let mut group = c.benchmark_group("trie_mut_root"); - c.bench_function_over_inputs( - "trie_mut_root_b", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { + for (seed, len, value_length) in params { + let input = input_unsorted(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("trie_mut_root", param), &input, |b, i| { b.iter(|| { - let datac: Vec<(Vec, Vec)> = data_sorted_unique(data.clone()); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); - - reference_trie::calc_root::(inputc); + let i = data_sorted_unique(i.clone()); + let inputc = i.iter().map(|v| (&v.0, &v.1)).collect::>(); + reference_trie::reference_trie_root::(inputc); }) - }, - data, - ); -} - -fn trie_mut_ref_root_a(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![input_unsorted(29, 204800 / 2, 512 * 2)]; - - c.bench_function_over_inputs( - "trie_mut_ref_root_a", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); // no need to sort for trie_root, see implementation - - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + }); + } - reference_trie::reference_trie_root_iter_build::(inputc); - }) - }, - data, - ); + group.finish(); } -fn trie_mut_ref_root_b(c: &mut Criterion) { - let data: Vec, Vec)>> = vec![ - //input_unsorted(29, 204800, 512), - input_unsorted(29, 204800, 32), - ]; +fn trie_mut_ref_root(c: &mut Criterion) { + let params = vec![(29, 204800 / 2, 512 * 2), (29, 204800, 32)]; - c.bench_function_over_inputs( - "trie_mut_ref_root_b", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); // no need to sort for trie_root, see implementation - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + let mut group = c.benchmark_group("trie_mut_ref_root"); + for (seed, len, value_length) in params { + let input = input_unsorted(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("trie_mut_ref_root", param), &input, |b, i| { + b.iter(|| { + let inputc = i.iter().map(|v| (&v.0, &v.1)).collect::>(); reference_trie::reference_trie_root_iter_build::(inputc); }) - }, - data, - ); + }); + } + + group.finish(); } -fn trie_mut_a(c: &mut Criterion) { +fn trie_mut(c: &mut Criterion) { use memory_db::HashKey; use trie_db::TrieMut; - let data: Vec, Vec)>> = vec![input_unsorted(29, 204800 / 2, 512 * 2)]; - c.bench_function_over_inputs( - "trie_mut_a", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); + let params = vec![(29, 204800 / 2, 512 * 2), (29, 204800, 32)]; - let mut root = Default::default(); - let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - let mut trie = - trie_db::TrieDBMutBuilder::::new(&mut mdb, &mut root).build(); - for (key, value) in datac { - trie.insert(&key, &value).expect( - "changes trie: insertion to trie is not allowed to fail within runtime", - ); - } - }) - }, - data, - ); -} + let mut group = c.benchmark_group("trie_mut"); -fn trie_mut_b(c: &mut Criterion) { - use memory_db::HashKey; - use trie_db::TrieMut; - let data: Vec, Vec)>> = vec![ - //input_unsorted(29, 204800, 512), - input_unsorted(29, 204800, 32), - ]; - - c.bench_function_over_inputs( - "trie_mut_b", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { + for (seed, len, value_length) in params { + let input = input_unsorted(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("trie_mut", param), &input, |b, i| { b.iter(|| { - let datac: Vec<(Vec, Vec)> = data.clone(); - let mut root = Default::default(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); let mut trie = trie_db::TrieDBMutBuilder::::new(&mut mdb, &mut root).build(); - for (key, value) in datac { + for (key, value) in i { trie.insert(&key, &value).expect( "changes trie: insertion to trie is not allowed to fail within runtime", ); } }) - }, - data, - ); + }); + } + + group.finish(); } -fn trie_mut_build_a(c: &mut Criterion) { +fn trie_mut_build(c: &mut Criterion) { use memory_db::HashKey; - let data: Vec, Vec)>> = vec![input_unsorted(29, 204800 / 2, 512 * 2)]; - c.bench_function_over_inputs( - "trie_mut_build_a", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { - b.iter(|| { - let datac: Vec<(Vec, Vec)> = data_sorted_unique(data.clone()); - // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + let params = vec![(29, 204800 / 2, 512 * 2), (29, 204800, 32)]; - let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); - reference_trie::calc_root_build::(inputc, &mut mdb); - }) - }, - data, - ); -} + let mut group = c.benchmark_group("trie_mut_build"); -fn trie_mut_build_b(c: &mut Criterion) { - use memory_db::HashKey; - let data: Vec, Vec)>> = vec![ - //input_unsorted(29, 204800, 512), - input_unsorted(29, 204800, 32), - ]; - - c.bench_function_over_inputs( - "trie_mut_build_b", - |b: &mut Bencher, data: &Vec<(Vec, Vec)>| { + for (seed, len, value_length) in params { + let input = input_unsorted(seed, len, value_length); + let param = format!("seed({}), len({}), value_length({})", seed, len, value_length); + group.bench_with_input(BenchmarkId::new("trie_mut_build", param), &input, |b, i| { b.iter(|| { - let datac: Vec<(Vec, Vec)> = data_sorted_unique(data.clone()); + let i = data_sorted_unique(i.clone()); // this is in `reference_trie_root` added here to make things comparable - let inputc = datac - .iter() - .map(|v| (&v.0, &v.1)) - .collect::>(); + let inputc = i.iter().map(|v| (&v.0, &v.1)).collect::>(); let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); reference_trie::calc_root_build::(inputc, &mut mdb); }) - }, - data, - ); + }); + } + + group.finish(); } fn trie_iteration(c: &mut Criterion) { @@ -464,7 +314,7 @@ fn trie_iteration(c: &mut Criterion) { let mut mdb = memory_db::MemoryDB::<_, HashKey<_>, _>::default(); let root = reference_trie::calc_root_build::(input, &mut mdb); - c.bench_function("trie_iteration", move |b: &mut Bencher| { + c.bench_function("trie_iteration", move |b| { b.iter(|| { let trie = trie_db::TrieDBBuilder::::new(&mdb, &root).build(); let mut iter = trie_db::TrieDBNodeIterator::new(&trie).unwrap(); @@ -497,7 +347,7 @@ fn trie_proof_verification(c: &mut Criterion) { }) .collect::>(); - c.bench_function("trie_proof_verification", move |b: &mut Bencher| { + c.bench_function("trie_proof_verification", move |b| { b.iter(|| { verify_proof::(&root, &proof, items.iter()).unwrap(); }) diff --git a/trie-eip1186/Cargo.toml b/trie-eip1186/Cargo.toml index d59dda07..0f7d51af 100644 --- a/trie-eip1186/Cargo.toml +++ b/trie-eip1186/Cargo.toml @@ -8,8 +8,6 @@ license = "Apache-2.0" edition = "2018" [dependencies] -log = "0.4" -smallvec = "1.0.0" trie-db = { path = "../trie-db", default-features = false, version = "0.24"} hash-db = { path = "../hash-db", default-features = false, version = "0.15.2"} diff --git a/trie-root/test/Cargo.toml b/trie-root/test/Cargo.toml index 7985f140..70ae4b4b 100644 --- a/trie-root/test/Cargo.toml +++ b/trie-root/test/Cargo.toml @@ -10,8 +10,6 @@ edition = "2018" [dependencies] trie-root = { path = "..", version = "0.17.0" } -hash-db = { path = "../../hash-db", version = "0.15.2" } hex-literal = "0.3" keccak-hasher = { path = "../../test-support/keccak-hasher", version = "0.15.2" } -trie-standardmap = { path = "../../test-support/trie-standardmap", version = "0.15.2" } reference-trie = { path = "../../test-support/reference-trie", version = "0.26.0" }