Skip to content

Commit

Permalink
Mark RawVec::reserve as inline and outline the resizing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Mar 21, 2021
1 parent f826641 commit f5e3710
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,20 @@ impl<T, A: Allocator> RawVec<T, A> {
/// # vector.push_all(&[1, 3, 5, 7, 9]);
/// # }
/// ```
#[inline]
pub fn reserve(&mut self, len: usize, additional: usize) {
handle_reserve(self.try_reserve(len, additional));
// Callers expect this function to be very cheap when there is already sufficient capacity.
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
// handle_reserve behind a call, while making sure that the this function is likely to be
// inlined as just a comparison and a call if the comparison fails.
#[inline(never)]
fn do_reserve_and_handle<T, A: Allocator>(slf: &mut RawVec<T,A>, len: usize, additional: usize) {
handle_reserve(slf.grow_amortized(len, additional));
}

if self.needs_to_grow(len, additional) {
do_reserve_and_handle(self, len, additional);
}
}

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

0 comments on commit f5e3710

Please sign in to comment.