HigherOrderCO / HVM

A massively parallel, optimal functional runtime in Rust

Home Page:https://higherorderco.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unnecessary unsafe in `new_atomic_u64_array`

Boscop opened this issue · comments

commented

pub fn new_atomic_u64_array(size: usize) -> Box<[AtomicU64]> {
return unsafe {
Box::from_raw(AtomicU64::from_mut_slice(Box::leak(vec![0u64; size].into_boxed_slice())))
};
}

This could just be vec![0; n].into_iter().map(|x| AtomicU64::new(x)).collect::<Vec<_>>().into_boxed_slice(), it compiles to a call to alloc_zeroed without any memset (it reuses the allocated Vec):
https://godbolt.org/z/Ebrn4aaW7

(Btw, when using Box::from_raw, it's more recommended to pair it with Box::into_raw:
https://users.rust-lang.org/t/can-box-leak-be-complemented-with-box-from-raw/64478/2
So it would be written as Box::into_raw(...) as *mut [AtomicU64] and a static assertion that they have the same alignment requirement.)

Or just vec![0u64; size].into_iter().map(AtomicU64::new).collect()