Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the pointers in Fetch impls unions #5085

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5b4edee
Get started
james7132 May 18, 2022
08178ac
get it to compile
james7132 May 18, 2022
550ebfe
Use dense iteration when any fetch is dense
james7132 May 18, 2022
24c6d59
Remove internal entity_table_rows. No longer needed.
james7132 May 18, 2022
729d174
Remove unneeded entities reference
james7132 May 18, 2022
a925fc3
Fix WorldQuery derive macro
james7132 May 19, 2022
ed0c1a3
Add missing safety docs
james7132 May 19, 2022
6cfcbcc
CI fixes and debug_unwrap_unchecked
james7132 May 19, 2022
cac2927
Coallese entity and row together in Archetype
james7132 May 20, 2022
f1b4da5
Merge branch 'main' into dense-iteration
james7132 May 30, 2022
389477f
Fix build from bad merge
james7132 May 30, 2022
95f57d7
Remove transmutes
james7132 May 31, 2022
8fef6c8
Remove unnecessary lifetime
james7132 May 31, 2022
91eda72
Revert code duplication.
james7132 May 31, 2022
4ea8e34
Fix AnyOf density
james7132 May 31, 2022
3fbe9ea
Merge table_components and table_ticks together
james7132 May 31, 2022
637e981
Remove matches in set_table impls
james7132 May 31, 2022
7b44be9
Remove outdated comments
james7132 May 31, 2022
9392d09
Merge branch 'main' into dense-iteration
james7132 Jun 6, 2022
255c481
Merge branch 'main' into dense-iteration
james7132 Jun 6, 2022
d96461f
Merge branch 'main' into dense-iteration
james7132 Jun 18, 2022
c04ee45
Remove unsafe on FetchState
james7132 Jun 18, 2022
ed72c4c
Fix docs
james7132 Jun 18, 2022
4d27afc
Reference to value
james7132 Jun 18, 2022
50b52ae
Formatting
james7132 Jun 21, 2022
49ae4b6
Fix up WorldQuery derives
james7132 Jun 21, 2022
1789404
Merge branch 'main' into dense-iteration
james7132 Jun 23, 2022
714e39f
Use a union for holding pointers
james7132 Jun 23, 2022
9e6bb4f
SparseSet variants don't need to be Option
james7132 Jun 23, 2022
d9d4f95
Copy over Clone
james7132 Jun 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,34 +225,19 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
#(self.#field_idents.set_table(&_state.#field_idents, _table);)*
}

/// SAFETY: we call `table_fetch` for each member that implements `Fetch`.
#[inline]
unsafe fn table_fetch(&mut self, _table_row: usize) -> Self::Item {
Self::Item {
#(#field_idents: self.#field_idents.table_fetch(_table_row),)*
#(#ignored_field_idents: Default::default(),)*
}
}

/// SAFETY: we call `archetype_fetch` for each member that implements `Fetch`.
#[inline]
unsafe fn archetype_fetch(&mut self, _archetype_index: usize) -> Self::Item {
/// SAFETY: we call `fetch` for each member that implements `Fetch`.
#[inline(always)]
unsafe fn fetch(&mut self, _entity: Entity, _table_row: usize) -> Self::Item {
Self::Item {
#(#field_idents: self.#field_idents.archetype_fetch(_archetype_index),)*
#(#field_idents: self.#field_idents.fetch(_entity, _table_row),)*
#(#ignored_field_idents: Default::default(),)*
}
}

#[allow(unused_variables)]
#[inline]
unsafe fn table_filter_fetch(&mut self, _table_row: usize) -> bool {
true #(&& self.#field_idents.table_filter_fetch(_table_row))*
}

#[allow(unused_variables)]
#[inline]
unsafe fn archetype_filter_fetch(&mut self, _archetype_index: usize) -> bool {
true #(&& self.#field_idents.archetype_filter_fetch(_archetype_index))*
#[inline(always)]
unsafe fn filter_fetch(&mut self, _entity: Entity, _table_row: usize) -> bool {
true #(&& self.#field_idents.filter_fetch(_entity, _table_row))*
}

fn update_component_access(state: &Self::State, _access: &mut #path::query::FilteredAccess<#path::component::ComponentId>) {
Expand Down
51 changes: 25 additions & 26 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,19 @@ impl Edges {
}
}

struct TableInfo {
id: TableId,
entity_rows: Vec<usize>,
pub struct ArchetypeEntity {
pub(crate) entity: Entity,
pub(crate) table_row: usize,
}

impl ArchetypeEntity {
pub fn entity(&self) -> Entity {
self.entity
}

pub fn table_row(&self) -> usize {
self.table_row
}
}

pub(crate) struct ArchetypeSwapRemoveResult {
Expand All @@ -135,9 +145,9 @@ pub(crate) struct ArchetypeComponentInfo {

pub struct Archetype {
id: ArchetypeId,
entities: Vec<Entity>,
table_id: TableId,
edges: Edges,
table_info: TableInfo,
entities: Vec<ArchetypeEntity>,
table_components: Box<[ComponentId]>,
sparse_set_components: Box<[ComponentId]>,
pub(crate) unique_components: SparseSet<ComponentId, Column>,
Expand Down Expand Up @@ -181,15 +191,12 @@ impl Archetype {
}
Self {
id,
table_info: TableInfo {
id: table_id,
entity_rows: Default::default(),
},
table_id,
entities: Vec::new(),
components,
table_components,
sparse_set_components,
unique_components: SparseSet::new(),
entities: Default::default(),
edges: Default::default(),
}
}
Expand All @@ -201,19 +208,14 @@ impl Archetype {

#[inline]
pub fn table_id(&self) -> TableId {
self.table_info.id
self.table_id
}

#[inline]
pub fn entities(&self) -> &[Entity] {
pub fn entities(&self) -> &[ArchetypeEntity] {
&self.entities
}

#[inline]
pub fn entity_table_rows(&self) -> &[usize] {
&self.table_info.entity_rows
}

#[inline]
pub fn table_components(&self) -> &[ComponentId] {
&self.table_components
Expand Down Expand Up @@ -251,20 +253,19 @@ impl Archetype {

#[inline]
pub fn entity_table_row(&self, index: usize) -> usize {
self.table_info.entity_rows[index]
self.entities[index].table_row
}

#[inline]
pub(crate) fn set_entity_table_row(&mut self, index: usize, table_row: usize) {
self.table_info.entity_rows[index] = table_row;
self.entities[index].table_row = table_row;
}

/// # Safety
/// valid component values must be immediately written to the relevant storages
/// `table_row` must be valid
pub(crate) unsafe fn allocate(&mut self, entity: Entity, table_row: usize) -> EntityLocation {
self.entities.push(entity);
self.table_info.entity_rows.push(table_row);
self.entities.push(ArchetypeEntity { entity, table_row });

EntityLocation {
archetype_id: self.id,
Expand All @@ -274,21 +275,20 @@ impl Archetype {

pub(crate) fn reserve(&mut self, additional: usize) {
self.entities.reserve(additional);
self.table_info.entity_rows.reserve(additional);
}

/// Removes the entity at `index` by swapping it out. Returns the table row the entity is stored
/// in.
pub(crate) fn swap_remove(&mut self, index: usize) -> ArchetypeSwapRemoveResult {
let is_last = index == self.entities.len() - 1;
self.entities.swap_remove(index);
let entity = self.entities.swap_remove(index);
ArchetypeSwapRemoveResult {
swapped_entity: if is_last {
None
} else {
Some(self.entities[index])
Some(self.entities[index].entity)
},
table_row: self.table_info.entity_rows.swap_remove(index),
table_row: entity.table_row,
}
}

Expand Down Expand Up @@ -326,7 +326,6 @@ impl Archetype {

pub(crate) fn clear_entities(&mut self) {
self.entities.clear();
self.table_info.entity_rows.clear();
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ mod tests {
.spawn()
.insert_bundle((TableStored("def"), A(456), SparseStored(1)))
.id();
// // this should be skipped
// this should be skipped
// SparseStored(1).spawn().insert("abc");
let ents = world
.query::<(Entity, Option<&SparseStored>, &A)>()
Expand Down
Loading