use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use milhouse::{List, Value, Vector}; use ssz::{Decode, Encode}; use ssz_types::VariableList; use typenum::Unsigned; type C = typenum::U1099511627776; type D = typenum::U1000000; const N: u64 = 1_000_000; #[inline] fn encode_list(l1: &List) -> Vec { l1.as_ssz_bytes() } #[inline] fn encode_decode_list(l1: &List) -> List { let bytes = l1.as_ssz_bytes(); List::from_ssz_bytes(&bytes).unwrap() } #[inline] fn encode_vector(v1: &Vector) -> Vec { v1.as_ssz_bytes() } #[inline] fn encode_decode_vector(v1: &Vector) -> Vector { let bytes = v1.as_ssz_bytes(); Vector::from_ssz_bytes(&bytes).unwrap() } #[inline] fn encode_variable_list(l1: &VariableList) -> Vec { l1.as_ssz_bytes() } #[inline] fn encode_decode_variable_list( l1: &VariableList, ) -> VariableList { let bytes = l1.as_ssz_bytes(); VariableList::from_ssz_bytes(&bytes).unwrap() } pub fn ssz(c: &mut Criterion) { let size = N; let list = List::::try_from_iter(0..size).unwrap(); let vector = Vector::::try_from_iter(0..size).unwrap(); let variable_list = VariableList::::new((0..size).collect()).unwrap(); c.bench_with_input(BenchmarkId::new("ssz_encode_list", size), &list, |b, l1| { b.iter(|| encode_list(l1)); }); c.bench_with_input( BenchmarkId::new("ssz_encode_decode_list", size), &list, |b, l1| { b.iter(|| encode_decode_list(l1)); }, ); c.bench_with_input( BenchmarkId::new("ssz_encode_vector", size), &vector, |b, v1| { b.iter(|| encode_vector(v1)); }, ); c.bench_with_input( BenchmarkId::new("ssz_encode_decode_vector", size), &vector, |b, v1| { b.iter(|| encode_decode_vector(v1)); }, ); // Test `VariableList` as a point of comparison. c.bench_with_input( BenchmarkId::new("ssz_encode_variable_list", size), &variable_list, |b, l1| { b.iter(|| encode_variable_list(l1)); }, ); // Test `VariableList` as a point of comparison. c.bench_with_input( BenchmarkId::new("ssz_encode_decode_variable_list", size), &variable_list, |b, l1| { b.iter(|| encode_decode_variable_list(l1)); }, ); } criterion_group!(benches, ssz); criterion_main!(benches);