Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ impl<T, const N: usize> SmallVec<T, N> {
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,
Expand Down Expand Up @@ -1231,16 +1231,22 @@ impl<T, const N: usize> SmallVec<T, N> {
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() {
Expand All @@ -1259,15 +1265,21 @@ impl<T, const N: usize> SmallVec<T, N> {
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() {
Expand Down Expand Up @@ -1441,8 +1453,11 @@ impl<T, const N: usize> SmallVec<T, N> {
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) };
Expand Down