Skip to content

Commit 07f3055

Browse files
committed
Don't grow if the capacity is sufficient
1 parent 8c11af7 commit 07f3055

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,11 @@ impl<T, const N: usize, #[cfg(feature = "allocator_api")] A: Allocator> alloc_pa
620620
// way in which data is stored.
621621
let (old_ptr, old_capacity) = unsafe { self.inner.heap };
622622

623+
// Nothing needs to be done if the capacity is already sufficient.
624+
if old_capacity >= new_capacity {
625+
return Ok(true);
626+
}
627+
623628
// Ensure capacity growth is exponential.
624629
let new_capacity = new_capacity.max(2 * old_capacity);
625630

@@ -676,6 +681,11 @@ impl<T, const N: usize, #[cfg(feature = "allocator_api")] A: Allocator> alloc_pa
676681
// way in which data is stored.
677682
let (old_ptr, old_capacity) = unsafe { self.inner.heap };
678683

684+
// Nothing needs to be done if the capacity is already sufficient.
685+
if old_capacity >= new_capacity {
686+
return Ok(true);
687+
}
688+
679689
// SAFETY: The stored capacity corresponds always to a valid layout.
680690
let old_layout = unsafe { array_layout_unchecked::<T>(old_capacity) };
681691

0 commit comments

Comments
 (0)