-
Notifications
You must be signed in to change notification settings - Fork 99
Optimised the Decode::decode for [T; N]
#299
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
Changes from all commits
ada5037
b418b6e
a5764a5
c401cec
eb7ba47
5c094c0
226be22
4137711
6a3ebfc
4a8e689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,13 @@ use core::{ | |
| iter::FromIterator, | ||
| marker::PhantomData, | ||
| mem, | ||
| mem::{ | ||
| MaybeUninit, | ||
| forget, | ||
| }, | ||
| ops::{Deref, Range, RangeInclusive}, | ||
| time::Duration, | ||
| ptr, | ||
| }; | ||
| use core::num::{ | ||
| NonZeroI8, | ||
|
|
@@ -35,7 +40,6 @@ use core::num::{ | |
| NonZeroU64, | ||
| NonZeroU128, | ||
| }; | ||
| use arrayvec::ArrayVec; | ||
|
|
||
| use byte_slice_cast::{AsByteSlice, AsMutByteSlice, ToMutByteSlice}; | ||
|
|
||
|
|
@@ -637,7 +641,91 @@ pub(crate) fn encode_slice_no_len<T: Encode, W: Output + ?Sized>(slice: &[T], de | |
| } | ||
| } | ||
|
|
||
| /// Decode the slice (without prepended the len). | ||
| /// Decode the array. | ||
| /// | ||
| /// This is equivalent to decoding all the element one by one, but it is optimized for some types. | ||
| #[inline] | ||
| pub(crate) fn decode_array<I: Input, T: Decode, const N: usize>(input: &mut I) -> Result<[T; N], Error> { | ||
| #[inline] | ||
| fn general_array_decode<I: Input, T: Decode, const N: usize>(input: &mut I) -> Result<[T; N], Error> { | ||
| let mut uninit = <MaybeUninit<[T; N]>>::uninit(); | ||
| // The following line coerces the pointer to the array to a pointer | ||
| // to the first array element which is equivalent. | ||
| let mut ptr = uninit.as_mut_ptr() as *mut T; | ||
| for _ in 0..N { | ||
| let decoded = T::decode(input)?; | ||
| // SAFETY: We do not read uninitialized array contents | ||
| // while initializing them. | ||
| unsafe { | ||
| ptr::write(ptr, decoded); | ||
| } | ||
| // SAFETY: Point to the next element after every iteration. | ||
| // We do this N times therefore this is safe. | ||
| ptr = unsafe { ptr.add(1) }; | ||
| } | ||
| // SAFETY: All array elements have been initialized above. | ||
| let init = unsafe { uninit.assume_init() }; | ||
| Ok(init) | ||
| } | ||
|
|
||
| // Description for the code below. | ||
| // It is not possible to transmute `[u8; N]` into `[T; N]` due to this issue: | ||
| // https://github.com/rust-lang/rust/issues/61956 | ||
| // | ||
| // Workaround: Transmute `&[u8; N]` into `&[T; N]` and interpret that reference as value. | ||
| // ``` | ||
| // let mut array: [u8; N] = [0; N]; | ||
| // let ref_typed: &[T; N] = unsafe { mem::transmute(&array) }; | ||
| // let typed: [T; N] = unsafe { ptr::read(ref_typed) }; | ||
| // forget(array); | ||
| // Here `array` and `typed` points on the same memory. | ||
| // Function returns `typed` -> it is not dropped, but `array` will be dropped. | ||
| // To avoid that `array` should be forgotten. | ||
| // ``` | ||
| macro_rules! decode { | ||
| ( u8 ) => {{ | ||
| let mut array: [u8; N] = [0; N]; | ||
| input.read(&mut array[..])?; | ||
|
Member
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. This is a memory leak if
Contributor
Author
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. Good catch!=) @Robbepop I returned back the usage of |
||
| let ref_typed: &[T; N] = unsafe { mem::transmute(&array) }; | ||
| let typed: [T; N] = unsafe { ptr::read(ref_typed) }; | ||
| forget(array); | ||
| Ok(typed) | ||
| }}; | ||
| ( i8 ) => {{ | ||
| let mut array: [i8; N] = [0; N]; | ||
| let bytes = unsafe { mem::transmute::<&mut [i8], &mut [u8]>(&mut array[..]) }; | ||
| input.read(bytes)?; | ||
|
Member
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. Same |
||
|
|
||
| let ref_typed: &[T; N] = unsafe { mem::transmute(&array) }; | ||
| let typed: [T; N] = unsafe { ptr::read(ref_typed) }; | ||
| forget(array); | ||
| Ok(typed) | ||
| }}; | ||
| ( $ty:ty ) => {{ | ||
| if cfg!(target_endian = "little") { | ||
| let mut array: [$ty; N] = [0; N]; | ||
| let bytes = <[$ty] as AsMutByteSlice<$ty>>::as_mut_byte_slice(&mut array[..]); | ||
| input.read(bytes)?; | ||
| let ref_typed: &[T; N] = unsafe { mem::transmute(&array) }; | ||
| let typed: [T; N] = unsafe { ptr::read(ref_typed) }; | ||
| forget(array); | ||
| Ok(typed) | ||
| } else { | ||
| general_array_decode(input) | ||
| } | ||
| }}; | ||
| } | ||
|
|
||
| with_type_info! { | ||
| <T as Decode>::TYPE_INFO, | ||
| decode, | ||
| { | ||
| general_array_decode(input) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Decode the vec (without prepended the len). | ||
| /// | ||
| /// This is equivalent to decode all elements one by one, but it is optimized in some | ||
| /// situation. | ||
|
|
@@ -706,16 +794,9 @@ impl<T: Encode, const N: usize> Encode for [T; N] { | |
| } | ||
|
|
||
| impl<T: Decode, const N: usize> Decode for [T; N] { | ||
| #[inline] | ||
| fn decode<I: Input>(input: &mut I) -> Result<Self, Error> { | ||
| let mut array = ArrayVec::new(); | ||
| for _ in 0..N { | ||
| array.push(T::decode(input)?); | ||
| } | ||
|
|
||
| match array.into_inner() { | ||
| Ok(a) => Ok(a), | ||
| Err(_) => panic!("We decode `N` elements; qed"), | ||
| } | ||
| decode_array(input) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1647,6 +1728,25 @@ mod tests { | |
| <[u32; 0]>::decode(&mut &encoded[..]).unwrap(); | ||
| } | ||
|
|
||
|
|
||
| macro_rules! test_array_encode_and_decode { | ||
| ( $( $name:ty ),* $(,)? ) => { | ||
| $( | ||
| paste::item! { | ||
| #[test] | ||
| fn [<test_array_encode_and_decode _ $name>]() { | ||
| let data: [$name; 32] = [123; 32]; | ||
| let encoded = data.encode(); | ||
| let decoded: [$name; 32] = Decode::decode(&mut &encoded[..]).unwrap(); | ||
| assert_eq!(decoded, data); | ||
| } | ||
| } | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| test_array_encode_and_decode!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128); | ||
|
|
||
| fn test_encoded_size(val: impl Encode) { | ||
| let length = val.using_encoded(|v| v.len()); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.