From e0170e4b638c871c979ab5af915d627502b97ff0 Mon Sep 17 00:00:00 2001 From: Khashayar Fereidani Date: Wed, 2 Sep 2026 20:04:17 +0330 Subject: [PATCH] perf: outline and make reserve as cold --- src/lib.rs | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 22bddd3..9407f12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1111,7 +1111,7 @@ impl SmallVec { pub fn push_mut(&mut self, value: T) -> &mut T { let len = self.len(); if len == self.capacity() { - self.reserve(1); + self.reserve_cold(1); } // SAFETY: `len < capacity` after the reserve, @@ -1231,16 +1231,22 @@ impl SmallVec { pub fn reserve(&mut self, additional: usize) { // can't overflow since len <= capacity if additional > self.capacity() - self.len() { - let new_capacity = infallible( - self.len() - .checked_add(additional) - .and_then(usize::checked_next_power_of_two) - .ok_or(CollectionAllocErr::CapacityOverflow) - ); - self.grow(new_capacity); + self.reserve_cold(additional); } } + #[cold] + #[inline(never)] + fn reserve_cold(&mut self, additional: usize) { + let new_capacity = infallible( + self.len() + .checked_add(additional) + .and_then(usize::checked_next_power_of_two) + .ok_or(CollectionAllocErr::CapacityOverflow) + ); + self.grow(new_capacity); + } + #[inline] pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { if additional > self.capacity() - self.len() { @@ -1259,15 +1265,21 @@ impl SmallVec { pub fn reserve_exact(&mut self, additional: usize) { // can't overflow since len <= capacity if additional > self.capacity() - self.len() { - let new_capacity = infallible( - self.len() - .checked_add(additional) - .ok_or(CollectionAllocErr::CapacityOverflow) - ); - self.grow(new_capacity); + self.reserve_exact_cold(additional); } } + #[cold] + #[inline(never)] + fn reserve_exact_cold(&mut self, additional: usize) { + let new_capacity = infallible( + self.len() + .checked_add(additional) + .ok_or(CollectionAllocErr::CapacityOverflow) + ); + self.grow(new_capacity); + } + #[inline] pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { if additional > self.capacity() - self.len() { @@ -1441,8 +1453,11 @@ impl SmallVec { if index > len { assert_failed(index, len); } - self.reserve(1); + // reserve one if there is no capacity left + if len == self.capacity() { + self.reserve_cold(1); + } // SAFETY: `index <= len <= capacity`, // so the offset stays in bounds of the allocation. let ptr = unsafe { self.as_mut_ptr().add(index) };