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

Use dense fetches in Query::get and Query::iter_many #12501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
88 changes: 56 additions & 32 deletions crates/bevy_ecs/src/query/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,46 +332,70 @@ where
continue;
};

if !self
.query_state
.matched_archetypes
.contains(location.archetype_id.index())
{
continue;
if D::IS_DENSE && F::IS_DENSE {
if !self
.query_state
.matched_tables
.contains(location.table_id.as_usize())
{
continue;
}
} else {
if !self
.query_state
.matched_archetypes
.contains(location.archetype_id.index())
{
continue;
}
}

let archetype = self
.archetypes
.get(location.archetype_id)
.debug_checked_unwrap();
let table = self.tables.get(location.table_id).debug_checked_unwrap();

// SAFETY: `archetype` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
D::set_archetype(
&mut self.fetch,
&self.query_state.fetch_state,
archetype,
table,
);
}
// SAFETY: `table` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
F::set_archetype(
&mut self.filter,
&self.query_state.filter_state,
archetype,
table,
);
if D::IS_DENSE && F::IS_DENSE {
// SAFETY: `archetype` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
D::set_table(&mut self.fetch, &self.query_state.fetch_state, table);
}
// SAFETY: `table` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
F::set_table(&mut self.filter, &self.query_state.filter_state, table);
}
} else {
let archetype = self
.archetypes
.get(location.archetype_id)
.debug_checked_unwrap();
// SAFETY: `archetype` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
D::set_archetype(
&mut self.fetch,
&self.query_state.fetch_state,
archetype,
table,
);
}
// SAFETY: `table` is from the world that `fetch/filter` were created for,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
unsafe {
F::set_archetype(
&mut self.filter,
&self.query_state.filter_state,
archetype,
table,
);
}
}

// SAFETY: set_archetype was called prior.
// `location.archetype_row` is an archetype index row in range of the current archetype, because if it was not, the match above would have `continue`d
// SAFETY: set_archetype or set_table was correctly called prior based D and F
// `location.table_row` is an table row in range of the current table, because if it was not, the match above would have `continue`d
if unsafe { F::filter_fetch(&mut self.filter, entity, location.table_row) } {
// SAFETY:
// - set_archetype was called prior, `location.archetype_row` is an archetype index in range of the current archetype
// - set_archetype or set_table was correctly called prior based on D and F
// - `location.table_row` is an table index in range of the current table
// - fetch is only called once for each entity.
return Some(unsafe { D::fetch(&mut self.fetch, entity, location.table_row) });
}
Expand Down
35 changes: 24 additions & 11 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,16 +730,19 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
.entities()
.get(entity)
.ok_or(QueryEntityError::NoSuchEntity(entity))?;
if !self
.matched_archetypes
.contains(location.archetype_id.index())
{
return Err(QueryEntityError::QueryDoesNotMatch(entity));
if D::IS_DENSE && F::IS_DENSE {
if !self.matched_tables.contains(location.table_id.as_usize()) {
return Err(QueryEntityError::QueryDoesNotMatch(entity));
}
} else {
if !self
.matched_archetypes
.contains(location.archetype_id.index())
{
return Err(QueryEntityError::QueryDoesNotMatch(entity));
}
}
let archetype = world
.archetypes()
.get(location.archetype_id)
.debug_checked_unwrap();

let mut fetch = D::init_fetch(world, &self.fetch_state, last_run, this_run);
let mut filter = F::init_fetch(world, &self.filter_state, last_run, this_run);

Expand All @@ -748,8 +751,18 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
.tables
.get(location.table_id)
.debug_checked_unwrap();
D::set_archetype(&mut fetch, &self.fetch_state, archetype, table);
F::set_archetype(&mut filter, &self.filter_state, archetype, table);

if D::IS_DENSE && F::IS_DENSE {
D::set_table(&mut fetch, &self.fetch_state, table);
F::set_table(&mut filter, &self.filter_state, table);
} else {
let archetype = world
.archetypes()
.get(location.archetype_id)
.debug_checked_unwrap();
D::set_archetype(&mut fetch, &self.fetch_state, archetype, table);
F::set_archetype(&mut filter, &self.filter_state, archetype, table);
}

if F::filter_fetch(&mut filter, entity, location.table_row) {
Ok(D::fetch(&mut fetch, entity, location.table_row))
Expand Down
Loading