Skip to content
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

Avoid Layout::array() in raw_vec.rs. #75093

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")]
#![doc(hidden)]

use core::alloc::{LayoutErr, MemoryBlock};
use core::alloc::MemoryBlock;
use core::cmp;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::Drop;
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
} else {
// We avoid `unwrap_or_else` here because it bloats the amount of
// LLVM IR generated.
let layout = match Layout::array::<T>(capacity) {
let layout = match array_layout(Layout::new::<T>(), capacity) {
Ok(layout) => layout,
Err(_) => capacity_overflow(),
};
Expand Down Expand Up @@ -423,10 +423,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
};
let cap = cmp::max(min_non_zero_cap, cap);

let new_layout = Layout::array::<T>(cap);
let elem_layout = Layout::new::<T>();

// `finish_grow` is non-generic over `T`.
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
let memory = finish_grow(cap, elem_layout, self.current_memory(), &mut self.alloc)?;
self.set_memory(memory);
Ok(())
}
Expand All @@ -442,10 +442,11 @@ impl<T, A: AllocRef> RawVec<T, A> {
}

let cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
let new_layout = Layout::array::<T>(cap);

let elem_layout = Layout::new::<T>();

// `finish_grow` is non-generic over `T`.
let memory = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?;
let memory = finish_grow(cap, elem_layout, self.current_memory(), &mut self.alloc)?;
self.set_memory(memory);
Ok(())
}
Expand Down Expand Up @@ -478,15 +479,16 @@ impl<T, A: AllocRef> RawVec<T, A> {
// significant, because the number of different `A` types seen in practice is
// much smaller than the number of `T` types.)
fn finish_grow<A>(
new_layout: Result<Layout, LayoutErr>,
cap: usize,
elem_layout: Layout,
current_memory: Option<(NonNull<u8>, Layout)>,
alloc: &mut A,
) -> Result<MemoryBlock, TryReserveError>
where
A: AllocRef,
{
// Check for the error here to minimize the size of `RawVec::grow_*`.
let new_layout = new_layout.map_err(|_| CapacityOverflow)?;
let new_layout = array_layout(elem_layout, cap)?;

alloc_guard(new_layout.size())?;

Expand All @@ -501,6 +503,15 @@ where
Ok(memory)
}

// This is equivalent to Layout::array, but is non-generic and has a different
// error type in its result. It helps reduce the amount of LLVM IR generated.
#[inline]
fn array_layout(elem_layout: Layout, n: usize) -> Result<Layout, TryReserveError> {
let (new_layout, offset) = elem_layout.repeat(n).map_err(|_| CapacityOverflow)?;
debug_assert_eq!(offset, elem_layout.size());
Ok(new_layout.pad_to_align())
}

unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
fn drop(&mut self) {
Expand Down