From 6767186c9dc7dbcc12362d0cc222e1fee20ead04 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 9 Oct 2023 15:05:32 +0100 Subject: [PATCH] Hint optimizer about reserved capacity --- library/alloc/src/raw_vec.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 01b03de6acb5e..fd29e4b0d55c2 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -291,6 +291,11 @@ impl RawVec { if self.needs_to_grow(len, additional) { do_reserve_and_handle(self, len, additional); + if self.needs_to_grow(len, additional) { + unsafe { + core::hint::unreachable_unchecked(); + } + } } } @@ -305,10 +310,14 @@ impl RawVec { /// The same as `reserve`, but returns on errors instead of panicking or aborting. pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { if self.needs_to_grow(len, additional) { - self.grow_amortized(len, additional) - } else { - Ok(()) + self.grow_amortized(len, additional)?; + if self.needs_to_grow(len, additional) { + unsafe { + core::hint::unreachable_unchecked(); + } + } } + Ok(()) } /// Ensures that the buffer contains at least enough space to hold `len + @@ -339,7 +348,15 @@ impl RawVec { len: usize, additional: usize, ) -> Result<(), TryReserveError> { - if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) } + if self.needs_to_grow(len, additional) { + self.grow_exact(len, additional)?; + if self.needs_to_grow(len, additional) { + unsafe { + core::hint::unreachable_unchecked(); + } + } + } + Ok(()) } /// Shrinks the buffer down to the specified capacity. If the given amount