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

[Merged by Bors] - Clean up Fetch code #4800

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
51 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
4ca8881
Merge branch 'main' into dense-iteration
james7132 Jun 28, 2022
255e306
Documenting `BufferVec`. (#4673)
Jun 28, 2022
696a187
Fix Events example link (#5126)
64kramsystem Jun 28, 2022
d1e8773
Update `ExactSizeIterator` impl to support archetypal filters (With, …
harudagondi Jun 29, 2022
ba28b0d
Wider ECS Benchmarks (#5123)
james7132 Jun 29, 2022
cce14dd
fix resource not found error message (#5128)
hymm Jun 29, 2022
da59bbd
Move texture sample out of branch in prepare_normal (#5129)
DGriffin91 Jun 29, 2022
5bc5adb
Fix typos in bevy_reflect readme (#5134)
grace125 Jun 29, 2022
aa04589
Merge branch 'main' into dense-iteration
james7132 Jul 2, 2022
b32681c
Eagerly fetch table for set_archetype
james7132 Jul 2, 2022
38f5ea4
Fix WorldQuery macro
james7132 Jul 2, 2022
815072f
Set table inside set_archetype only if dense
james7132 Jul 2, 2022
0dbf1c9
Reduce Option mapping when getting ticks
james7132 Jul 2, 2022
44c8f86
Merge branch 'main' into dense-iteration
james7132 Oct 24, 2022
eb2c6a4
Fix build
james7132 Oct 24, 2022
7f794ed
Fix CI
james7132 Oct 24, 2022
2c31e05
Replace a few remaining unwraps
james7132 Oct 24, 2022
24b0b99
Remove EntityFetch
james7132 Oct 25, 2022
2832c04
Fix up the doc comment
james7132 Oct 25, 2022
659f644
Fix rebase error
james7132 Oct 25, 2022
92cc6e1
Fix up the doc comment
james7132 Oct 25, 2022
8d1beee
Merge branch 'main' into dense-iteration
james7132 Oct 28, 2022
47ad0d1
Fix CI
james7132 Oct 28, 2022
8f9f568
Address review comments
james7132 Oct 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 14 additions & 27 deletions crates/bevy_ecs/macros/src/fetch.rs
Expand Up @@ -249,9 +249,9 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch,
_state: &Self::State,
_archetype: &'__w #path::archetype::Archetype,
_tables: &'__w #path::storage::Tables
_table: &'__w #path::storage::Table
) {
#(<#field_types>::set_archetype(&mut _fetch.#field_idents, &_state.#field_idents, _archetype, _tables);)*
#(<#field_types>::set_archetype(&mut _fetch.#field_idents, &_state.#field_idents, _archetype, _table);)*
}

/// SAFETY: we call `set_table` for each member that implements `Fetch`
Expand All @@ -264,40 +264,27 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
#(<#field_types>::set_table(&mut _fetch.#field_idents, &_state.#field_idents, _table);)*
}

/// SAFETY: we call `table_fetch` for each member that implements `Fetch`.
#[inline]
unsafe fn table_fetch<'__w>(
/// SAFETY: we call `fetch` for each member that implements `Fetch`.
#[inline(always)]
unsafe fn fetch<'__w>(
_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch,
_entity: Entity,
_table_row: usize
) -> <Self as #path::query::WorldQueryGats<'__w>>::Item {
Self::Item {
#(#field_idents: <#field_types>::table_fetch(&mut _fetch.#field_idents, _table_row),)*
#(#field_idents: <#field_types>::fetch(&mut _fetch.#field_idents, _entity, _table_row),)*
#(#ignored_field_idents: Default::default(),)*
}
}

/// SAFETY: we call `archetype_fetch` for each member that implements `Fetch`.
#[inline]
unsafe fn archetype_fetch<'__w>(
_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch,
_archetype_index: usize
) -> <Self as #path::query::WorldQueryGats<'__w>>::Item {
Self::Item {
#(#field_idents: <#field_types>::archetype_fetch(&mut _fetch.#field_idents, _archetype_index),)*
#(#ignored_field_idents: Default::default(),)*
}
}

#[allow(unused_variables)]
#[inline]
unsafe fn table_filter_fetch<'__w>(_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch, _table_row: usize) -> bool {
true #(&& <#field_types>::table_filter_fetch(&mut _fetch.#field_idents, _table_row))*
}

#[allow(unused_variables)]
#[inline]
unsafe fn archetype_filter_fetch<'__w>(_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch, _archetype_index: usize) -> bool {
true #(&& <#field_types>::archetype_filter_fetch(&mut _fetch.#field_idents, _archetype_index))*
#[inline(always)]
unsafe fn filter_fetch<'__w>(
_fetch: &mut <Self as #path::query::WorldQueryGats<'__w>>::Fetch,
_entity: Entity,
_table_row: usize
) -> bool {
true #(&& <#field_types>::filter_fetch(&mut _fetch.#field_idents, _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
Expand Up @@ -121,9 +121,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 @@ -138,9 +148,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]>,
components: SparseSet<ComponentId, ArchetypeComponentInfo>,
Expand Down Expand Up @@ -183,14 +193,11 @@ 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,
entities: Default::default(),
edges: Default::default(),
}
}
Expand All @@ -202,19 +209,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 @@ -242,20 +244,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 @@ -265,21 +266,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 @@ -317,7 +317,6 @@ impl Archetype {

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

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/lib.rs
Expand Up @@ -540,8 +540,8 @@ mod tests {
let f = world
.spawn((TableStored("def"), A(456), SparseStored(1)))
.id();
// // this should be skipped
// SparseStored(1).spawn("abc");
james7132 marked this conversation as resolved.
Show resolved Hide resolved
// this should be skipped
// SparseStored(1).spawn().insert("abc");
let ents = world
.query::<(Entity, Option<&SparseStored>, &A)>()
.iter(&world)
Expand Down