Skip to content

Commit 5c598c2

Browse files
committed
Remove table and sparse set component IDs from Archetype
1 parent 9976ecb commit 5c598c2

File tree

3 files changed

+40
-51
lines changed

3 files changed

+40
-51
lines changed

crates/bevy_ecs/src/archetype.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ pub struct Archetype {
120120
entities: Vec<Entity>,
121121
edges: Edges,
122122
table_info: TableInfo,
123-
table_components: Box<[ComponentId]>,
124-
sparse_set_components: Box<[ComponentId]>,
125123
pub(crate) unique_components: SparseSet<ComponentId, Column>,
126124
pub(crate) components: SparseSet<ComponentId, ArchetypeComponentInfo>,
127125
}
@@ -130,31 +128,25 @@ impl Archetype {
130128
pub fn new(
131129
id: ArchetypeId,
132130
table_id: TableId,
133-
table_components: Box<[ComponentId]>,
134-
sparse_set_components: Box<[ComponentId]>,
135-
table_archetype_components: Vec<ArchetypeComponentId>,
136-
sparse_set_archetype_components: Vec<ArchetypeComponentId>,
131+
table_components: impl Iterator<Item = (ComponentId, ArchetypeComponentId)>,
132+
sparse_set_components: impl Iterator<Item = (ComponentId, ArchetypeComponentId)>,
137133
) -> Self {
138-
let mut components =
139-
SparseSet::with_capacity(table_components.len() + sparse_set_components.len());
140-
for (component_id, archetype_component_id) in
141-
table_components.iter().zip(table_archetype_components)
142-
{
134+
let (min_table, _) = table_components.size_hint();
135+
let (min_sparse, _) = table_components.size_hint();
136+
let mut components = SparseSet::with_capacity(min_table + min_sparse);
137+
for (component_id, archetype_component_id) in table_components {
143138
components.insert(
144-
*component_id,
139+
component_id,
145140
ArchetypeComponentInfo {
146141
storage_type: StorageType::Table,
147142
archetype_component_id,
148143
},
149144
);
150145
}
151146

152-
for (component_id, archetype_component_id) in sparse_set_components
153-
.iter()
154-
.zip(sparse_set_archetype_components)
155-
{
147+
for (component_id, archetype_component_id) in sparse_set_components {
156148
components.insert(
157-
*component_id,
149+
component_id,
158150
ArchetypeComponentInfo {
159151
storage_type: StorageType::SparseSet,
160152
archetype_component_id,
@@ -168,8 +160,6 @@ impl Archetype {
168160
entity_rows: Default::default(),
169161
},
170162
components,
171-
table_components,
172-
sparse_set_components,
173163
unique_components: SparseSet::new(),
174164
entities: Default::default(),
175165
edges: Default::default(),
@@ -197,13 +187,19 @@ impl Archetype {
197187
}
198188

199189
#[inline]
200-
pub fn table_components(&self) -> &[ComponentId] {
201-
&self.table_components
190+
pub fn table_components(&self) -> impl Iterator<Item = ComponentId> + '_ {
191+
self.components
192+
.iter()
193+
.filter(|(_, component)| component.storage_type == StorageType::Table)
194+
.map(|(id, _)| *id)
202195
}
203196

204197
#[inline]
205-
pub fn sparse_set_components(&self) -> &[ComponentId] {
206-
&self.sparse_set_components
198+
pub fn sparse_set_components(&self) -> impl Iterator<Item = ComponentId> + '_ {
199+
self.components
200+
.iter()
201+
.filter(|(_, component)| component.storage_type == StorageType::SparseSet)
202+
.map(|(id, _)| *id)
207203
}
208204

209205
#[inline]
@@ -380,10 +376,8 @@ impl Default for Archetypes {
380376
archetypes.archetypes.push(Archetype::new(
381377
ArchetypeId::RESOURCE,
382378
TableId::empty(),
383-
Box::new([]),
384-
Box::new([]),
385-
Vec::new(),
386-
Vec::new(),
379+
std::iter::empty(),
380+
std::iter::empty(),
387381
));
388382
archetypes
389383
}
@@ -476,38 +470,33 @@ impl Archetypes {
476470
table_components: Vec<ComponentId>,
477471
sparse_set_components: Vec<ComponentId>,
478472
) -> ArchetypeId {
479-
let table_components = table_components.into_boxed_slice();
480-
let sparse_set_components = sparse_set_components.into_boxed_slice();
481473
let archetype_identity = ArchetypeIdentity {
482-
sparse_set_components: sparse_set_components.clone(),
483-
table_components: table_components.clone(),
474+
sparse_set_components: sparse_set_components.clone().into_boxed_slice(),
475+
table_components: table_components.clone().into_boxed_slice(),
484476
};
485477

486478
let archetypes = &mut self.archetypes;
487479
let archetype_component_count = &mut self.archetype_component_count;
488-
let mut next_archetype_component_id = move || {
489-
let id = ArchetypeComponentId(*archetype_component_count);
490-
*archetype_component_count += 1;
491-
id
492-
};
493480
*self
494481
.archetype_ids
495482
.entry(archetype_identity)
496483
.or_insert_with(move || {
497484
let id = ArchetypeId(archetypes.len());
498-
let table_archetype_components = (0..table_components.len())
499-
.map(|_| next_archetype_component_id())
500-
.collect();
501-
let sparse_set_archetype_components = (0..sparse_set_components.len())
502-
.map(|_| next_archetype_component_id())
503-
.collect();
485+
let table_start = *archetype_component_count;
486+
*archetype_component_count += table_components.len();
487+
let table_archetype_components =
488+
(table_start..*archetype_component_count).map(ArchetypeComponentId);
489+
let sparse_start = *archetype_component_count;
490+
*archetype_component_count += sparse_set_components.len();
491+
let sparse_set_archetype_components =
492+
(sparse_start..*archetype_component_count).map(ArchetypeComponentId);
504493
archetypes.push(Archetype::new(
505494
id,
506495
table_id,
507-
table_components,
508-
sparse_set_components,
509-
table_archetype_components,
510-
sparse_set_archetype_components,
496+
table_components.into_iter().zip(table_archetype_components),
497+
sparse_set_components
498+
.into_iter()
499+
.zip(sparse_set_archetype_components),
511500
));
512501
id
513502
})

crates/bevy_ecs/src/bundle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl BundleInfo {
349349
table_components = if new_table_components.is_empty() {
350350
// if there are no new table components, we can keep using this table
351351
table_id = current_archetype.table_id();
352-
current_archetype.table_components().to_vec()
352+
current_archetype.table_components().collect()
353353
} else {
354354
new_table_components.extend(current_archetype.table_components());
355355
// sort to ignore order while hashing
@@ -365,7 +365,7 @@ impl BundleInfo {
365365
};
366366

367367
sparse_set_components = if new_sparse_set_components.is_empty() {
368-
current_archetype.sparse_set_components().to_vec()
368+
current_archetype.sparse_set_components().collect()
369369
} else {
370370
new_sparse_set_components.extend(current_archetype.sparse_set_components());
371371
// sort to ignore order while hashing

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'w> EntityMut<'w> {
467467
table_row = remove_result.table_row;
468468

469469
for component_id in archetype.sparse_set_components() {
470-
let sparse_set = world.storages.sparse_sets.get_mut(*component_id).unwrap();
470+
let sparse_set = world.storages.sparse_sets.get_mut(component_id).unwrap();
471471
sparse_set.remove(self.entity);
472472
}
473473
// SAFE: table rows stored in archetypes always exist
@@ -789,8 +789,8 @@ unsafe fn remove_bundle_from_archetype(
789789
// components are already sorted
790790
removed_table_components.sort();
791791
removed_sparse_set_components.sort();
792-
next_table_components = current_archetype.table_components().to_vec();
793-
next_sparse_set_components = current_archetype.sparse_set_components().to_vec();
792+
next_table_components = current_archetype.table_components().collect();
793+
next_sparse_set_components = current_archetype.sparse_set_components().collect();
794794
sorted_remove(&mut next_table_components, &removed_table_components);
795795
sorted_remove(
796796
&mut next_sparse_set_components,

0 commit comments

Comments
 (0)