diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index ed81ce71ddfac..73cdf8d5e44f3 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -297,10 +297,14 @@ impl RawVec { /// # } /// ``` pub fn reserve(&mut self, len: usize, additional: usize) { - match self.try_reserve(len, additional) { - Err(CapacityOverflow) => capacity_overflow(), - Err(AllocError { layout, .. }) => handle_alloc_error(layout), - Ok(()) => { /* yay */ } + // This function is marginally shorter if it calls `try_reserve`, but + // that results in more LLVM IR being generated. + if self.needs_to_grow(len, additional) { + match self.grow_amortized(len, additional) { + Ok(()) => { /* yay */ } + Err(CapacityOverflow) => capacity_overflow(), + Err(AllocError { layout, .. }) => handle_alloc_error(layout), + } } } @@ -331,10 +335,14 @@ impl RawVec { /// /// Aborts on OOM. pub fn reserve_exact(&mut self, len: usize, additional: usize) { - match self.try_reserve_exact(len, additional) { - Err(CapacityOverflow) => capacity_overflow(), - Err(AllocError { layout, .. }) => handle_alloc_error(layout), - Ok(()) => { /* yay */ } + // This function is marginally shorter if it calls `try_reserve_exact`, + // but that results in more LLVM IR being generated. + if self.needs_to_grow(len, additional) { + match self.grow_exact(len, additional) { + Ok(()) => { /* yay */ } + Err(CapacityOverflow) => capacity_overflow(), + Err(AllocError { layout, .. }) => handle_alloc_error(layout), + } } }