-
Notifications
You must be signed in to change notification settings - Fork 15
Implement perf optimization using MaybeUninit
#230
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: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -97,6 +97,7 @@ pub mod prelude { | |
|
|
||
| #[cfg(feature = "alloc")] | ||
| use alloc::vec::Vec; | ||
| use core::mem::MaybeUninit; | ||
|
|
||
| pub(crate) use table::Table; | ||
|
|
||
|
|
@@ -137,10 +138,20 @@ pub fn decode_to_vec(hex: &str) -> Result<Vec<u8>, DecodeVariableLengthBytesErro | |
| /// `N * 2`.) | ||
| pub fn decode_to_array<const N: usize>(hex: &str) -> Result<[u8; N], DecodeFixedLengthBytesError> { | ||
| if hex.len() == N * 2 { | ||
| let mut ret = [0u8; N]; | ||
| // SAFETY: `[MaybeUninit<u8>; N]` has no initialization requirement, | ||
| // so an uninitialized array of them is sound. This is the standard | ||
| // `uninit_array` pattern. | ||
| let mut ret: [MaybeUninit<u8>; N] = unsafe { MaybeUninit::uninit().assume_init() }; | ||
|
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. In 1705412: Can you find a citation for this being the "standard AFAICT, by reading for example this rust-lang thread, the correct way to do this on stable Rust is |
||
|
|
||
| // checked above | ||
| HexToBytesIter::new_unchecked(hex).drain_to_slice(&mut ret)?; | ||
| Ok(ret) | ||
| HexToBytesIter::new_unchecked(hex).drain_to_uninit_slice(&mut ret)?; | ||
|
|
||
| // SAFETY: `drain_to_uninit_slice` returning `Ok` means all N bytes | ||
| // were written. `[MaybeUninit<u8>; N]` and `[u8; N]` have identical | ||
| // layout, so the transmute is sound. | ||
| #[allow(clippy::borrow_as_ptr)] | ||
| #[allow(clippy::ptr_as_ptr)] | ||
| Ok(unsafe { (&ret as *const _ as *const [u8; N]).read() }) | ||
| } else { | ||
| Err(InvalidLengthError { invalid: hex.len(), expected: 2 * N }.into()) | ||
| } | ||
|
|
@@ -273,4 +284,19 @@ mod tests { | |
| assert_eq!(HASH[0], 0x00); | ||
| assert_eq!(HASH[31], 0x6f); | ||
| } | ||
|
|
||
| // This implicitly test `drain_to_uninit_slice()`. | ||
| // In `iter::hex_to_bytes_slice_drain` we test `drain_to_slice()` | ||
| #[test] | ||
| fn decode_to_vec() { | ||
| let hex = "deadbeef"; | ||
| let want = [0xde, 0xad, 0xbe, 0xef]; | ||
| let got = crate::decode_to_vec(hex).unwrap(); | ||
| assert_eq!(got, want); | ||
|
|
||
| let hex = ""; | ||
| let want: [u8; 0] = []; | ||
| let got = crate::decode_to_vec(hex).unwrap(); | ||
| assert_eq!(got, want); | ||
| } | ||
| } | ||
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.
In 1705412:
Why use
ptr::writeinstead ofMaybeuninit::write? That would save you unsafety, casts, and pointer arithmetic.