Use NonNull::new_unchecked and NonNull::len in

`rustc_arena`.
This commit is contained in:
John Bobbo 2023-04-27 19:48:37 -07:00
parent 1a6ae3d692
commit 618841b815
No known key found for this signature in database
GPG Key ID: D5B1CA85E46C4709

View File

@ -74,7 +74,7 @@ impl<T> ArenaChunk<T> {
#[inline] #[inline]
unsafe fn new(capacity: usize) -> ArenaChunk<T> { unsafe fn new(capacity: usize) -> ArenaChunk<T> {
ArenaChunk { ArenaChunk {
storage: NonNull::new(Box::into_raw(Box::new_uninit_slice(capacity))).unwrap(), storage: NonNull::new_unchecked(Box::into_raw(Box::new_uninit_slice(capacity))),
entries: 0, entries: 0,
} }
} }
@ -85,7 +85,7 @@ unsafe fn destroy(&mut self, len: usize) {
// The branch on needs_drop() is an -O1 performance optimization. // The branch on needs_drop() is an -O1 performance optimization.
// Without the branch, dropping TypedArena<u8> takes linear time. // Without the branch, dropping TypedArena<u8> takes linear time.
if mem::needs_drop::<T>() { if mem::needs_drop::<T>() {
let slice = &mut *(self.storage.as_mut()); let slice = self.storage.as_mut();
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(&mut slice[..len])); ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(&mut slice[..len]));
} }
} }
@ -104,7 +104,7 @@ fn end(&mut self) -> *mut T {
// A pointer as large as possible for zero-sized elements. // A pointer as large as possible for zero-sized elements.
ptr::invalid_mut(!0) ptr::invalid_mut(!0)
} else { } else {
self.start().add((*self.storage.as_ptr()).len()) self.start().add(self.storage.len())
} }
} }
} }
@ -288,7 +288,7 @@ fn grow(&self, additional: usize) {
// If the previous chunk's len is less than HUGE_PAGE // If the previous chunk's len is less than HUGE_PAGE
// bytes, then this chunk will be least double the previous // bytes, then this chunk will be least double the previous
// chunk's size. // chunk's size.
new_cap = (*last_chunk.storage.as_ptr()).len().min(HUGE_PAGE / elem_size / 2); new_cap = last_chunk.storage.len().min(HUGE_PAGE / elem_size / 2);
new_cap *= 2; new_cap *= 2;
} else { } else {
new_cap = PAGE / elem_size; new_cap = PAGE / elem_size;
@ -396,7 +396,7 @@ fn grow(&self, additional: usize) {
// If the previous chunk's len is less than HUGE_PAGE // If the previous chunk's len is less than HUGE_PAGE
// bytes, then this chunk will be least double the previous // bytes, then this chunk will be least double the previous
// chunk's size. // chunk's size.
new_cap = (*last_chunk.storage.as_ptr()).len().min(HUGE_PAGE / 2); new_cap = last_chunk.storage.len().min(HUGE_PAGE / 2);
new_cap *= 2; new_cap *= 2;
} else { } else {
new_cap = PAGE; new_cap = PAGE;