Skip to content

Commit 877d36b

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 a3cfa03 + 4500c83 commit 877d36b

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Diff for: library/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
}

Diff for: library/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
}

Diff for: library/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);

Diff for: tests/codegen/vec_pop_push_noop.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#[no_mangle]
66
// CHECK-LABEL: @noop(
77
pub fn noop(v: &mut Vec<u8>) {
8-
// CHECK-NOT: reserve_for_push
8+
// CHECK-NOT: grow_one
99
// CHECK-NOT: call
1010
// CHECK: tail call void @llvm.assume
11-
// CHECK-NOT: reserve_for_push
11+
// CHECK-NOT: grow_one
1212
// CHECK-NOT: call
1313
// CHECK: ret
1414
if let Some(x) = v.pop() {
@@ -19,6 +19,6 @@ pub fn noop(v: &mut Vec<u8>) {
1919
#[no_mangle]
2020
// CHECK-LABEL: @push_byte(
2121
pub fn push_byte(v: &mut Vec<u8>) {
22-
// CHECK: call {{.*}}reserve_for_push
22+
// CHECK: call {{.*}}grow_one
2323
v.push(3);
2424
}

0 commit comments

Comments
 (0)