-
Notifications
You must be signed in to change notification settings - Fork 28
Optimise SSZ encoding and decoding #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
62f785b
b729e99
2f52ad0
56fd2f8
31aee18
b042216
37e9308
2eecf7a
902121a
6bca389
7f2505b
da45efa
d87e180
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use ssz::{Decode, DecodeError, Encode}; | ||
use ssz_types::{FixedVector, VariableList}; | ||
use std::hint::black_box; | ||
use std::time::Duration; | ||
use typenum::{Unsigned, U1048576, U131072}; | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq, ssz_derive::Encode)] | ||
#[ssz(struct_behaviour = "transparent")] | ||
pub struct ByteVector<N: Unsigned>(FixedVector<u8, N>); | ||
|
||
impl<N: Unsigned> ssz::Decode for ByteVector<N> { | ||
fn is_ssz_fixed_len() -> bool { | ||
true | ||
} | ||
|
||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> { | ||
FixedVector::new(bytes.to_vec()) | ||
.map(Self) | ||
.map_err(|e| DecodeError::BytesInvalid(format!("{e:?}"))) | ||
} | ||
|
||
fn ssz_fixed_len() -> usize { | ||
<FixedVector<u8, N> as ssz::Decode>::ssz_fixed_len() | ||
} | ||
} | ||
|
||
fn benchmark_fixed_vector(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("fixed_vector"); | ||
|
||
let fixed_vector_u8 = FixedVector::<u8, U1048576>::new(vec![255u8; 1048576]).unwrap(); | ||
let fixed_vector_u64 = FixedVector::<u64, U131072>::new(vec![255u64; 131072]).unwrap(); | ||
let fixed_vector_bytes = fixed_vector_u8.as_ssz_bytes(); | ||
|
||
group.warm_up_time(Duration::from_secs(15)); | ||
group.measurement_time(Duration::from_secs(10)); | ||
|
||
group.bench_function("decode_byte_u8_1m", |b| { | ||
b.iter(|| { | ||
let vector = ByteVector::<U1048576>::from_ssz_bytes(&fixed_vector_bytes).unwrap(); | ||
black_box(vector); | ||
}); | ||
}); | ||
|
||
group.bench_function("decode_u8_1m", |b| { | ||
b.iter(|| { | ||
let vector = FixedVector::<u8, U1048576>::from_ssz_bytes(&fixed_vector_bytes).unwrap(); | ||
black_box(vector); | ||
}); | ||
}); | ||
|
||
group.bench_function("encode_u8_1m", |b| { | ||
b.iter(|| { | ||
let bytes = fixed_vector_u8.as_ssz_bytes(); | ||
black_box(bytes); | ||
}); | ||
}); | ||
|
||
group.bench_function("decode_u64_128k", |b| { | ||
b.iter(|| { | ||
let vector = FixedVector::<u64, U131072>::from_ssz_bytes(&fixed_vector_bytes).unwrap(); | ||
black_box(vector); | ||
}); | ||
}); | ||
group.bench_function("encode_u64_128k", |b| { | ||
b.iter(|| { | ||
let bytes = fixed_vector_u64.as_ssz_bytes(); | ||
black_box(bytes); | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
fn benchmark_variable_list(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("variable_list"); | ||
|
||
let variable_list_u8 = VariableList::<u8, U1048576>::new(vec![255u8; 1048576]).unwrap(); | ||
let variable_list_u64 = VariableList::<u64, U131072>::new(vec![255u64; 131072]).unwrap(); | ||
let variable_list_bytes = variable_list_u8.as_ssz_bytes(); | ||
|
||
group.warm_up_time(Duration::from_secs(15)); | ||
group.measurement_time(Duration::from_secs(10)); | ||
|
||
group.bench_function("decode_u8_1m", |b| { | ||
b.iter(|| { | ||
let vector = | ||
VariableList::<u8, U1048576>::from_ssz_bytes(&variable_list_bytes).unwrap(); | ||
black_box(vector); | ||
}); | ||
}); | ||
|
||
group.bench_function("encode_u8_1m", |b| { | ||
b.iter(|| { | ||
let bytes = variable_list_u8.as_ssz_bytes(); | ||
black_box(bytes); | ||
}); | ||
}); | ||
|
||
group.bench_function("decode_u64_128k", |b| { | ||
b.iter(|| { | ||
let vector = | ||
VariableList::<u64, U131072>::from_ssz_bytes(&variable_list_bytes).unwrap(); | ||
black_box(vector); | ||
}); | ||
}); | ||
group.bench_function("encode_u64_128k", |b| { | ||
b.iter(|| { | ||
let bytes = variable_list_u64.as_ssz_bytes(); | ||
black_box(bytes); | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, benchmark_fixed_vector, benchmark_variable_list); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use crate::Error; | |
use serde::Deserialize; | ||
use serde_derive::Serialize; | ||
use std::marker::PhantomData; | ||
use std::mem; | ||
use std::ops::{Deref, DerefMut, Index, IndexMut}; | ||
use std::slice::SliceIndex; | ||
use tree_hash::Hash256; | ||
|
@@ -275,6 +276,13 @@ impl<T, N: Unsigned> ssz::TryFromIter<T> for FixedVector<T, N> { | |
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn from_ssz_bytes_u8_only<N: Unsigned>( | ||
bytes: &[u8], | ||
) -> Result<FixedVector<u8, N>, ssz::DecodeError> { | ||
Ok(FixedVector::new(bytes.to_vec()).unwrap()) | ||
} | ||
|
||
impl<T, N: Unsigned> ssz::Decode for FixedVector<T, N> | ||
where | ||
T: ssz::Decode, | ||
|
@@ -299,6 +307,25 @@ where | |
len: 0, | ||
expected: 1, | ||
}) | ||
} else if mem::size_of::<T>() == 1 && mem::align_of::<T>() == 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Even using The other advantage of this is that it works for types that encode as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. couldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I guess it could be, but we never use bools in any consensus data structures, do we? Would be a good test case |
||
if bytes.len() != fixed_len { | ||
return Err(ssz::DecodeError::BytesInvalid(format!( | ||
"FixedVector of {} items has {} items", | ||
fixed_len, | ||
bytes.len(), | ||
))); | ||
} | ||
|
||
// Safety: We've verified T is u8, so Vec<T> is Vec<u8> | ||
// and bytes.to_vec() produces Vec<u8> | ||
let vec_u8 = bytes.to_vec(); | ||
let vec_t = unsafe { std::mem::transmute::<Vec<u8>, Vec<T>>(vec_u8) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add a test for a type that is not |
||
Self::new(vec_t).map_err(|e| { | ||
ssz::DecodeError::BytesInvalid(format!( | ||
"Wrong number of FixedVector elements: {:?}", | ||
e | ||
)) | ||
}) | ||
} else if T::is_ssz_fixed_len() { | ||
let num_items = bytes | ||
.len() | ||
|
@@ -312,13 +339,19 @@ where | |
))); | ||
} | ||
|
||
let vec = bytes.chunks(T::ssz_fixed_len()).try_fold( | ||
Vec::with_capacity(num_items), | ||
|mut vec, chunk| { | ||
vec.push(T::from_ssz_bytes(chunk)?); | ||
Ok(vec) | ||
}, | ||
)?; | ||
if bytes.len() != num_items * T::ssz_fixed_len() { | ||
return Err(ssz::DecodeError::BytesInvalid(format!( | ||
"FixedVector of {} items has {} bytes", | ||
num_items, | ||
bytes.len() | ||
))); | ||
} | ||
|
||
let mut vec = Vec::with_capacity(num_items); | ||
for chunk in bytes.chunks_exact(T::ssz_fixed_len()) { | ||
vec.push(T::from_ssz_bytes(chunk)?); | ||
} | ||
|
||
Self::new(vec).map_err(|e| { | ||
ssz::DecodeError::BytesInvalid(format!( | ||
"Wrong number of FixedVector elements: {:?}", | ||
|
Uh oh!
There was an error while loading. Please reload this page.