Skip to content

Commit b6be8a5

Browse files
committed
Fix table reserve logic (#1698)
Fixes #1692 Alternative to #1696 This ensures that the capacity actually grows in increments of grow_amount, and also ensures that Table capacity is always <= column and entity vec capacity. Debug logs that describe the new logic (running the example in #1692) [out.txt](https://github.com/bevyengine/bevy/files/6173808/out.txt)
1 parent c78b76b commit b6be8a5

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

crates/bevy_ecs/src/storage/table.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,17 @@ impl Table {
327327
pub fn reserve(&mut self, amount: usize) {
328328
let available_space = self.capacity - self.len();
329329
if available_space < amount {
330-
let reserve_amount = (amount - available_space).max(self.grow_amount);
330+
let min_capacity = self.len() + amount;
331+
// normally we would check if min_capacity is 0 for the below calculation, but amount > available_space and
332+
// available_space > 0, so min_capacity > 1
333+
let new_capacity =
334+
((min_capacity + self.grow_amount - 1) / self.grow_amount) * self.grow_amount;
335+
let reserve_amount = new_capacity - self.len();
331336
for column in self.columns.values_mut() {
332337
column.reserve(reserve_amount);
333338
}
334339
self.entities.reserve(reserve_amount);
335-
self.capacity += reserve_amount;
340+
self.capacity = new_capacity;
336341
}
337342
}
338343

0 commit comments

Comments
 (0)