From f5e37100d9c115ab327bafd42d0c4dad539d318f Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 21 Mar 2021 17:47:57 -0400 Subject: [PATCH 1/2] Mark RawVec::reserve as inline and outline the resizing logic --- library/alloc/src/raw_vec.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 56f4ebe57f8af..ff7bff7584af9 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -315,8 +315,20 @@ impl RawVec { /// # 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(slf: &mut RawVec, 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. From 73d773482afcceb8475dd773c4e2e70ed4835242 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 21 Mar 2021 19:17:07 -0400 Subject: [PATCH 2/2] fmt, change to cold --- library/alloc/src/raw_vec.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index ff7bff7584af9..dc02c9c883ea0 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -321,8 +321,12 @@ impl RawVec { // 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(slf: &mut RawVec, len: usize, additional: usize) { + #[cold] + fn do_reserve_and_handle( + slf: &mut RawVec, + len: usize, + additional: usize, + ) { handle_reserve(slf.grow_amortized(len, additional)); }