diff --git a/crates/bevy_ecs/src/storage/blob_vec.rs b/crates/bevy_ecs/src/storage/blob_vec.rs index ef98e0d3a16f52..50ca7d39208a38 100644 --- a/crates/bevy_ecs/src/storage/blob_vec.rs +++ b/crates/bevy_ecs/src/storage/blob_vec.rs @@ -122,21 +122,20 @@ impl BlobVec { /// Reserves the minimum capacity for at least `additional` more elements to be inserted in the given `BlobVec`. #[inline] pub fn reserve(&mut self, additional: usize) { - if self.capacity - self.len < additional { - self.do_reserve(additional); + /// Similar to `reserve_exact`. This method ensures that the capacity will grow at least `self.capacity()` if there is no + /// enough space to hold `additional` more elements. + #[cold] + fn do_reserve(slf: &mut BlobVec, additional: usize) { + if slf.item_layout.size() > 0 { + let increment = slf.capacity.max(additional - (slf.capacity - slf.len)); + let increment = NonZeroUsize::new(increment).unwrap(); + // SAFETY: not called for ZSTs + unsafe { slf.grow_exact(increment) }; + } } - } - /// Similar to `reserve_exact`. This method ensures that the capacity will grow at least `self.capacity()` if there is no - /// enough space to hold `additional` more elements. - #[cold] - fn do_reserve(&mut self, additional: usize) { - let available_space = self.capacity - self.len; - if available_space < additional && self.item_layout.size() > 0 { - let increment = self.capacity.max(additional - available_space); - let increment = NonZeroUsize::new(increment).unwrap(); - // SAFETY: not called for ZSTs - unsafe { self.grow_exact(increment) }; + if self.capacity - self.len < additional { + do_reserve(self, additional); } }