-
Notifications
You must be signed in to change notification settings - Fork 61
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
stats: fix UB in from_slice #83
Conversation
b742a26
to
7170c73
Compare
for item in &mut data[..] { | ||
item.write(reader(cursor)); | ||
} | ||
data.map(|x| unsafe { x.assume_init() }) |
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.
this copies the data unfortunately
using transmute currently not possible with const generics: rust-lang/rust#61956
but I can change it to a macro instead
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.
nvm, I think it's a noop: https://godbolt.org/z/YcxonWxPa
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.
The following functions are unstable, but could be used in the future:
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.
Might as well get rid of unsafe here.
let plain: [u32; 1024] = [Default::default(); 1024];
let mut atomic: [AtomicU32; 1024] = a.map(AtomicU32::new);
// Read into `atomic` below.
Shoudl be optimized away 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.
Not sure it's optimized this way though. The produced code seems different: https://godbolt.org/z/GGnfTro49
Also to sanity check, created a bench:
from_slice/Unsafe time: [1.0590 µs 1.0613 µs 1.0636 µs]
from_slice/Safe time: [2.1197 µs 2.1255 µs 2.1326 µs]
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.
Ok, let's leave it as it is then.
As documented in https://doc.rust-lang.org/std/mem/union.MaybeUninit.html,