From 6fd1a56434eac65c317a0e7e1ba79447561a6a45 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 4 Aug 2020 10:48:36 +1000 Subject: [PATCH] Tweak `RawVec::reserve{,_exact}`. --- library/alloc/src/raw_vec.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) 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), + } } }