-
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?
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #55 +/- ##
==========================================
- Coverage 58.62% 58.33% -0.29%
==========================================
Files 8 8
Lines 290 324 +34
==========================================
+ Hits 170 189 +19
- Misses 120 135 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Using size_of
and align_of
removes the need to add any bounds on T
. Other crates like bytemuck
would have required us to constrain T
quite a lot, which sort of defeats the point of a generic impl.
Even using TypeId
would have required us to add 'static
.
The other advantage of this is that it works for types that encode as u8
, like the ParticipationFlag
s in the BeaconState
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldn't T
be a bool
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should add a test for a type that is not u8
Summary of changes
unsafe
fast path for decoding lists and vectors ofu8
chunks_exact
and an explicit loop instead oftry_fold
.Benchmarks
Additional Info
Depends on a new release of
ethereum_ssz
for:Encoding is not meaningfully faster, but would need to be optimised in
ssz
itself (we need to optimise theVec
impl).