-
Notifications
You must be signed in to change notification settings - Fork 432
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
Use zerocopy to replace some unsafe code #1349
Conversation
let mut buf = [0_u8; mem::size_of::<$intrinsic>()]; | ||
rng.fill_bytes(&mut buf); | ||
// x86 is little endian so no need for conversion | ||
// SAFETY: we know [u8; N] and $intrinsic have the same size | ||
unsafe { mem::transmute_copy(&buf) } | ||
zerocopy::transmute!(buf) |
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.
Here's another option if this would be preferred. I opted not to go this route since it messes with code that's been verified to compile to SIMD instructions (although I expect the same would work here):
let mut val = $intrinsic::new_zeroed();
// On proper hardware, this should compile to SIMD instructions
// Verified on x86 Haswell with __m128i, __m256i
//
// x86 is little endian so no need for conversion
rng.fill_bytes(&mut val.as_bytes_mut());
val
The key point here is the adoption of |
The main contender would be |
With the obvious caveat that I'm biased as the author, and focusing specifically on this use case (rather than comparing the crates across all use cases), I would advocate for zerocopy in this case for a few reasons:
That said, the uses introduced in this PR are pretty small, the panic paths in bytemuck's solutions might be optimized out, and there's probably not a huge difference either way. |
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 future it also may be worth to (optionally) use |
No description provided.