Skip to content

Make Vec::push as non-generic as possible #91848

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove RawVec::capacity_from_bytes and RawVec::set_ptr.
It reduces generated code size a bit more.
  • Loading branch information
nnethercote committed Dec 13, 2021
commit 3ee89331ca83ee2f30fdf6ec62a748f4b51e077c
49 changes: 23 additions & 26 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<T, A: Allocator> RawVec<T, A> {

Self {
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
cap: Self::capacity_from_bytes(ptr.len()),
cap: ptr.len() / mem::size_of::<T>(),
alloc,
}
}
Expand Down Expand Up @@ -346,16 +346,6 @@ impl<T, A: Allocator> RawVec<T, A> {
additional > self.capacity().wrapping_sub(len)
}

fn capacity_from_bytes(excess: usize) -> usize {
debug_assert_ne!(mem::size_of::<T>(), 0);
excess / mem::size_of::<T>()
}

fn set_ptr(&mut self, ptr: NonNull<[u8]>) {
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
self.cap = Self::capacity_from_bytes(ptr.len());
}

// This method must only be called after `needs_to_grow(len, additional)`
// succeeds. Otherwise, if `T` is zero-sized it will cause a divide by
// zero.
Expand All @@ -370,15 +360,16 @@ impl<T, A: Allocator> RawVec<T, A> {
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
// `finish_grow_amortized` is non-generic over `T`.
let elem_layout = Layout::new::<T>();
let ptr = finish_grow_amortized(
let (ptr, cap) = finish_grow_amortized(
len,
additional,
elem_layout,
self.cap,
self.current_memory(),
&mut self.alloc,
)?;
self.set_ptr(ptr);
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
self.cap = cap;
Ok(())
}

Expand All @@ -392,14 +383,15 @@ impl<T, A: Allocator> RawVec<T, A> {
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
// `finish_grow_exact` is non-generic over `T`.
let elem_layout = Layout::new::<T>();
let ptr = finish_grow_exact(
let (ptr, cap) = finish_grow_exact(
len,
additional,
elem_layout,
self.current_memory(),
&mut self.alloc,
)?;
self.set_ptr(ptr);
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
self.cap = cap;
Ok(())
}

Expand All @@ -415,7 +407,8 @@ impl<T, A: Allocator> RawVec<T, A> {
.shrink(ptr, layout, new_layout)
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
};
self.set_ptr(ptr);
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
self.cap = ptr.len() / mem::size_of::<T>();
Ok(())
}
}
Expand All @@ -432,7 +425,7 @@ fn finish_grow_amortized<A>(
current_cap: usize,
current_memory: Option<(NonNull<u8>, Layout)>,
alloc: &mut A,
) -> Result<NonNull<[u8]>, TryReserveError>
) -> Result<(NonNull<[u8]>, usize), TryReserveError>
where
A: Allocator,
{
Expand Down Expand Up @@ -467,18 +460,20 @@ where

let new_layout = unsafe { Layout::from_size_align_unchecked(array_size, elem_layout.align()) };

let memory = if let Some((ptr, old_layout)) = current_memory {
let new_ptr = if let Some((old_ptr, old_layout)) = current_memory {
debug_assert_eq!(old_layout.align(), new_layout.align());
unsafe {
// The allocator checks for alignment equality
intrinsics::assume(old_layout.align() == new_layout.align());
alloc.grow(ptr, old_layout, new_layout)
alloc.grow(old_ptr, old_layout, new_layout)
}
} else {
alloc.allocate(new_layout)
};
}
.map_err(|_| TryReserveError::from(AllocError { layout: new_layout, non_exhaustive: () }))?;

memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
let new_cap = new_ptr.len() / elem_layout.size();
Ok((new_ptr, new_cap))
}

// This function is outside `RawVec` to minimize compile times. See the comment
Expand All @@ -492,7 +487,7 @@ fn finish_grow_exact<A>(
elem_layout: Layout,
current_memory: Option<(NonNull<u8>, Layout)>,
alloc: &mut A,
) -> Result<NonNull<[u8]>, TryReserveError>
) -> Result<(NonNull<[u8]>, usize), TryReserveError>
where
A: Allocator,
{
Expand All @@ -509,18 +504,20 @@ where

let new_layout = unsafe { Layout::from_size_align_unchecked(array_size, elem_layout.align()) };

let memory = if let Some((ptr, old_layout)) = current_memory {
let new_ptr = if let Some((old_ptr, old_layout)) = current_memory {
debug_assert_eq!(old_layout.align(), new_layout.align());
unsafe {
// The allocator checks for alignment equality
intrinsics::assume(old_layout.align() == new_layout.align());
alloc.grow(ptr, old_layout, new_layout)
alloc.grow(old_ptr, old_layout, new_layout)
}
} else {
alloc.allocate(new_layout)
};
}
.map_err(|_| TryReserveError::from(AllocError { layout: new_layout, non_exhaustive: () }))?;

memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
let new_cap = new_ptr.len() / elem_layout.size();
Ok((new_ptr, new_cap))
}

unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
Expand Down