-
Notifications
You must be signed in to change notification settings - Fork 29
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
Open
michaelsproul
wants to merge
20
commits into
main
Choose a base branch
from
optimise-decode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
62f785b
Optimise FixedVector Decode
michaelsproul b729e99
Add encoding benchmarks
michaelsproul 2f52ad0
u8 encoding benchmark
michaelsproul 56fd2f8
Update benchmarks to include encoding, optimise VariableList
michaelsproul 31aee18
Remove path patch
michaelsproul b042216
Remove unnecessary pub
michaelsproul 37e9308
Fix trailing bytes bug and add more tests
michaelsproul 2eecf7a
Remove junk
michaelsproul 902121a
Merge remote-tracking branch 'origin/main' into optimise-decode
michaelsproul 6bca389
More tests for oversize FixedVector
michaelsproul 7f2505b
Remove temp ByteVector from benches
michaelsproul da45efa
Fix tests
michaelsproul d87e180
Merge remote-tracking branch 'origin/main' into optimise-decode
michaelsproul 7c89206
Merge branch 'main' into optimise-decode
michaelsproul 8784c47
Merge remote-tracking branch 'origin/main' into optimise-decode
michaelsproul f022441
Update comments
michaelsproul 29d5205
Fix Clippy
michaelsproul 7635d55
Test bool on unsafe codepath
michaelsproul 50e6061
Add test demonstrating bool UB
michaelsproul 72b60cc
Fix UB by using TypeId
michaelsproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.