Skip to content

Commit bd43b55

Browse files
committed
Auto merge of rust-lang#122976 - caibear:optimize_reserve_for_push, r=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`.
2 parents 0421b4a + aee40df commit bd43b55

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

alloc/src/collections/vec_deque/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
20872087
// buffer without it being full emerge
20882088
debug_assert!(self.is_full());
20892089
let old_cap = self.capacity();
2090-
self.buf.reserve_for_push(old_cap);
2090+
self.buf.grow_one();
20912091
unsafe {
20922092
self.handle_capacity_increase(old_cap);
20932093
}

alloc/src/raw_vec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ impl<T, A: Allocator> RawVec<T, A> {
345345
}
346346
}
347347

348-
/// A specialized version of `reserve()` used only by the hot and
349-
/// oft-instantiated `Vec::push()`, which does its own capacity check.
348+
/// A specialized version of `self.reserve(len, 1)` which requires the
349+
/// caller to ensure `len == self.capacity()`.
350350
#[cfg(not(no_global_oom_handling))]
351351
#[inline(never)]
352-
pub fn reserve_for_push(&mut self, len: usize) {
353-
if let Err(err) = self.grow_amortized(len, 1) {
352+
pub fn grow_one(&mut self) {
353+
if let Err(err) = self.grow_amortized(self.cap.0, 1) {
354354
handle_error(err);
355355
}
356356
}

alloc/src/vec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ impl<T, A: Allocator> Vec<T, A> {
15471547

15481548
// space for the new element
15491549
if len == self.buf.capacity() {
1550-
self.reserve(1);
1550+
self.buf.grow_one();
15511551
}
15521552

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

0 commit comments

Comments
 (0)