Skip to content

Commit f5e3710

Browse files
committed
Mark RawVec::reserve as inline and outline the resizing logic
1 parent f826641 commit f5e3710

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

library/alloc/src/raw_vec.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,20 @@ impl<T, A: Allocator> RawVec<T, A> {
315315
/// # vector.push_all(&[1, 3, 5, 7, 9]);
316316
/// # }
317317
/// ```
318+
#[inline]
318319
pub fn reserve(&mut self, len: usize, additional: usize) {
319-
handle_reserve(self.try_reserve(len, additional));
320+
// Callers expect this function to be very cheap when there is already sufficient capacity.
321+
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
322+
// handle_reserve behind a call, while making sure that the this function is likely to be
323+
// inlined as just a comparison and a call if the comparison fails.
324+
#[inline(never)]
325+
fn do_reserve_and_handle<T, A: Allocator>(slf: &mut RawVec<T,A>, len: usize, additional: usize) {
326+
handle_reserve(slf.grow_amortized(len, additional));
327+
}
328+
329+
if self.needs_to_grow(len, additional) {
330+
do_reserve_and_handle(self, len, additional);
331+
}
320332
}
321333

322334
/// The same as `reserve`, but returns on errors instead of panicking or aborting.

0 commit comments

Comments
 (0)