Skip to content

Commit

Permalink
Merge "reserve" and "do_reserve"
Browse files Browse the repository at this point in the history
  • Loading branch information
garychia committed Jan 6, 2024
1 parent 458879e commit bc07407
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions crates/bevy_ecs/src/storage/blob_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit bc07407

Please sign in to comment.