Skip to content

Commit

Permalink
Auto merge of rust-lang#122396 - kornelski:vec-err-debloat, r=<try>
Browse files Browse the repository at this point in the history
Less generic code for Vec allocations

Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
  • Loading branch information
bors committed Mar 12, 2024
2 parents 7de1a1f + 870d332 commit 5af3381
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
41 changes: 28 additions & 13 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ impl<T> RawVec<T, Global> {
#[must_use]
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global))
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global) {
Ok(res) => res,
Err(err) => handle_reserve_err(err),
}
}

/// Like `with_capacity`, but guarantees the buffer is zeroed.
Expand Down Expand Up @@ -152,7 +155,10 @@ impl<T, A: Allocator> RawVec<T, A> {
#[cfg(not(no_global_oom_handling))]
#[inline]
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc))
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) {
Ok(res) => res,
Err(err) => handle_reserve_err(err),
}
}

/// Like `try_with_capacity`, but parameterized over the choice of
Expand All @@ -167,7 +173,10 @@ impl<T, A: Allocator> RawVec<T, A> {
#[cfg(not(no_global_oom_handling))]
#[inline]
pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc))
match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) {
Ok(res) => res,
Err(err) => handle_reserve_err(err),
}
}

/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
Expand Down Expand Up @@ -326,7 +335,9 @@ impl<T, A: Allocator> RawVec<T, A> {
len: usize,
additional: usize,
) {
handle_reserve(slf.grow_amortized(len, additional));
if let Err(err) = slf.grow_amortized(len, additional) {
handle_reserve_err(err);
}
}

if self.needs_to_grow(len, additional) {
Expand All @@ -339,7 +350,9 @@ impl<T, A: Allocator> RawVec<T, A> {
#[cfg(not(no_global_oom_handling))]
#[inline(never)]
pub fn reserve_for_push(&mut self, len: usize) {
handle_reserve(self.grow_amortized(len, 1));
if let Err(err) = self.grow_amortized(len, 1) {
handle_reserve_err(err);
}
}

/// The same as `reserve`, but returns on errors instead of panicking or aborting.
Expand Down Expand Up @@ -373,7 +386,9 @@ impl<T, A: Allocator> RawVec<T, A> {
/// Aborts on OOM.
#[cfg(not(no_global_oom_handling))]
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
handle_reserve(self.try_reserve_exact(len, additional));
if let Err(err) = self.try_reserve_exact(len, additional) {
handle_reserve_err(err);
}
}

/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
Expand Down Expand Up @@ -404,7 +419,9 @@ impl<T, A: Allocator> RawVec<T, A> {
/// Aborts on OOM.
#[cfg(not(no_global_oom_handling))]
pub fn shrink_to_fit(&mut self, cap: usize) {
handle_reserve(self.shrink(cap));
if let Err(err) = self.shrink(cap) {
handle_reserve_err(err);
}
}
}

Expand Down Expand Up @@ -559,12 +576,10 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {

// Central function for reserve error handling.
#[cfg(not(no_global_oom_handling))]
#[inline]
fn handle_reserve<T>(result: Result<T, TryReserveError>) -> T {
match result.map_err(|e| e.kind()) {
Ok(res) => res,
Err(CapacityOverflow) => capacity_overflow(),
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
fn handle_reserve_err(e: TryReserveError) -> ! {
match e.kind() {
CapacityOverflow => capacity_overflow(),
AllocError { layout, .. } => handle_alloc_error(layout),
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ impl<T> Vec<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
Vec { buf: RawVec::with_capacity_in(capacity, Global), len: 0 }
}

/// Constructs a new, empty `Vec<T>` with at least the specified capacity.
Expand Down

0 comments on commit 5af3381

Please sign in to comment.