Skip to content

Commit

Permalink
Auto merge of rust-lang#122976 - caibear:optimize_reserve_for_push, r…
Browse files Browse the repository at this point in the history
…=cuviper

Remove len argument from RawVec::reserve_for_push

Removes `RawVec::reserve_for_push`'s `len` argument since it's always the same as capacity.
Also makes `Vec::insert` use `RawVec::reserve_for_push`.
  • Loading branch information
bors committed Mar 29, 2024
2 parents 399fa2f + aba592d commit ca8923a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion library/alloc/src/collections/vec_deque/mod.rs
Expand Up @@ -2087,7 +2087,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
// buffer without it being full emerge
debug_assert!(self.is_full());
let old_cap = self.capacity();
self.buf.reserve_for_push(old_cap);
self.buf.grow_one();
unsafe {
self.handle_capacity_increase(old_cap);
}
Expand Down
8 changes: 4 additions & 4 deletions library/alloc/src/raw_vec.rs
Expand Up @@ -345,12 +345,12 @@ impl<T, A: Allocator> RawVec<T, A> {
}
}

/// A specialized version of `reserve()` used only by the hot and
/// oft-instantiated `Vec::push()`, which does its own capacity check.
/// A specialized version of `self.reserve(len, 1)` which requires the
/// caller to ensure `len == self.capacity()`.
#[cfg(not(no_global_oom_handling))]
#[inline(never)]
pub fn reserve_for_push(&mut self, len: usize) {
if let Err(err) = self.grow_amortized(len, 1) {
pub fn grow_one(&mut self) {
if let Err(err) = self.grow_amortized(self.cap.0, 1) {
handle_error(err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/vec/mod.rs
Expand Up @@ -1547,7 +1547,7 @@ impl<T, A: Allocator> Vec<T, A> {

// space for the new element
if len == self.buf.capacity() {
self.reserve(1);
self.buf.grow_one();
}

unsafe {
Expand Down Expand Up @@ -1967,7 +1967,7 @@ impl<T, A: Allocator> Vec<T, A> {
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if self.len == self.buf.capacity() {
self.buf.reserve_for_push(self.len);
self.buf.grow_one();
}
unsafe {
let end = self.as_mut_ptr().add(self.len);
Expand Down

0 comments on commit ca8923a

Please sign in to comment.