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 11 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.
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 @@ -219,34 +219,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))*
}
}
}
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 @@ -100,9 +100,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 @@ -117,9 +127,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 @@ -163,15 +173,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 @@ -183,19 +190,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 @@ -233,20 +235,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 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 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 @@ -256,21 +257,20 @@ impl Archetype {

pub 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 @@ -308,7 +308,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