Skip to content

Commit

Permalink
Remove redundant ComponentId in Column (bevyengine#4855)
Browse files Browse the repository at this point in the history
# Objective
The `ComponentId` in `Column` is redundant as it's stored in parallel in the surrounding `SparseSet` all the time.

## Solution
Remove it. Add `SparseSet::iter(_mut)` to parallel `HashMap::iter(_mut)` to allow iterating pairs of columns and their IDs.

---

## Changelog
Added: `SparseSet::iter` and `SparseSet::iter_mut`.
  • Loading branch information
james7132 committed Jun 7, 2022
1 parent 15d5c3d commit 36049e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/storage/sparse_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.dense.iter_mut()
}

pub fn iter(&self) -> impl Iterator<Item = (&I, &V)> {
self.indices.iter().zip(self.dense.iter())
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&I, &mut V)> {
self.indices.iter().zip(self.dense.iter_mut())
}
}

pub trait SparseSetIndex: Clone + PartialEq + Eq + Hash {
Expand Down
15 changes: 6 additions & 9 deletions crates/bevy_ecs/src/storage/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl TableId {
}

pub struct Column {
pub(crate) component_id: ComponentId,
pub(crate) data: BlobVec,
pub(crate) ticks: Vec<UnsafeCell<ComponentTicks>>,
}
Expand All @@ -40,7 +39,6 @@ impl Column {
#[inline]
pub fn with_capacity(component_info: &ComponentInfo, capacity: usize) -> Self {
Column {
component_id: component_info.id(),
// SAFE: component_info.drop() is valid for the types that will be inserted.
data: unsafe { BlobVec::new(component_info.layout(), component_info.drop(), capacity) },
ticks: Vec::with_capacity(capacity),
Expand Down Expand Up @@ -253,10 +251,9 @@ impl Table {
debug_assert!(row < self.len());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for column in self.columns.values_mut() {
let component_id = column.component_id;
for (component_id, column) in self.columns.iter_mut() {
let (data, ticks) = column.swap_remove_and_forget_unchecked(row);
if let Some(new_column) = new_table.get_column_mut(component_id) {
if let Some(new_column) = new_table.get_column_mut(*component_id) {
new_column.initialize(new_row, data, ticks);
}
}
Expand Down Expand Up @@ -284,8 +281,8 @@ impl Table {
debug_assert!(row < self.len());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for column in self.columns.values_mut() {
if let Some(new_column) = new_table.get_column_mut(column.component_id) {
for (component_id, column) in self.columns.iter_mut() {
if let Some(new_column) = new_table.get_column_mut(*component_id) {
let (data, ticks) = column.swap_remove_and_forget_unchecked(row);
new_column.initialize(new_row, data, ticks);
} else {
Expand Down Expand Up @@ -316,8 +313,8 @@ impl Table {
debug_assert!(row < self.len());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for column in self.columns.values_mut() {
let new_column = new_table.get_column_mut(column.component_id).unwrap();
for (component_id, column) in self.columns.iter_mut() {
let new_column = new_table.get_column_mut(*component_id).unwrap();
let (data, ticks) = column.swap_remove_and_forget_unchecked(row);
new_column.initialize(new_row, data, ticks);
}
Expand Down

0 comments on commit 36049e9

Please sign in to comment.