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] - merge matches_archetype and matches_table #4807

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 2 additions & 5 deletions crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,9 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
#(self.#field_idents.update_archetype_component_access(_archetype, _access);)*
}

fn matches_archetype(&self, _archetype: &#path::archetype::Archetype) -> bool {
true #(&& self.#field_idents.matches_archetype(_archetype))*
}
fn matches_component_set(&self, _component_set: &#path::storage::SparseArray<#path::component::ComponentId, usize>) -> bool {
true #(&& self.#field_idents.matches_component_set(_component_set))*

fn matches_table(&self, _table: &#path::storage::Table) -> bool {
true #(&& self.#field_idents.matches_table(_table))*
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ impl Archetype {
self.components.indices()
}

#[inline]
pub fn component_ids(&self) -> &SparseArray<ComponentId, usize> {
BoxyUwU marked this conversation as resolved.
Show resolved Hide resolved
self.components.sparse_array()
}

#[inline]
pub fn edges(&self) -> &Edges {
&self.edges
Expand Down
75 changes: 21 additions & 54 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
component::{Component, ComponentId, ComponentStorage, ComponentTicks, StorageType},
entity::Entity,
query::{debug_checked_unreachable, Access, FilteredAccess},
storage::{ComponentSparseSet, Table, Tables},
storage::{ComponentSparseSet, SparseArray, Table, Tables},
world::{Mut, World},
};
use bevy_ecs_macros::all_tuples;
Expand Down Expand Up @@ -421,7 +421,7 @@ pub trait Fetch<'world>: Sized {
///
/// Implementor must ensure that [`FetchState::update_component_access`] and
/// [`FetchState::update_archetype_component_access`] exactly reflects the results of
/// [`FetchState::matches_archetype`], [`FetchState::matches_table`], [`Fetch::archetype_fetch`], and
/// [`FetchState::matches_component_set`], [`Fetch::archetype_fetch`], and
/// [`Fetch::table_fetch`].
pub unsafe trait FetchState: Send + Sync + Sized {
fn init(world: &mut World) -> Self;
Expand All @@ -431,8 +431,7 @@ pub unsafe trait FetchState: Send + Sync + Sized {
archetype: &Archetype,
access: &mut Access<ArchetypeComponentId>,
);
fn matches_archetype(&self, archetype: &Archetype) -> bool;
fn matches_table(&self, table: &Table) -> bool;
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool;
}

/// A fetch that is read only.
Expand Down Expand Up @@ -480,12 +479,7 @@ unsafe impl FetchState for EntityState {
}

#[inline]
fn matches_archetype(&self, _archetype: &Archetype) -> bool {
true
}

#[inline]
fn matches_table(&self, _table: &Table) -> bool {
fn matches_component_set(&self, _component_set: &SparseArray<ComponentId, usize>) -> bool {
true
}
}
Expand Down Expand Up @@ -588,12 +582,8 @@ unsafe impl<T: Component> FetchState for ReadState<T> {
}
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
component_set.contains(self.component_id)
}
}

Expand Down Expand Up @@ -825,12 +815,8 @@ unsafe impl<T: Component> FetchState for WriteState<T> {
}
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
component_set.contains(self.component_id)
}
}

Expand Down Expand Up @@ -1105,17 +1091,13 @@ unsafe impl<T: FetchState> FetchState for OptionState<T> {
archetype: &Archetype,
access: &mut Access<ArchetypeComponentId>,
) {
if self.state.matches_archetype(archetype) {
if self.state.matches_component_set(archetype.component_ids()) {
self.state
.update_archetype_component_access(archetype, access);
}
}

fn matches_archetype(&self, _archetype: &Archetype) -> bool {
true
}

fn matches_table(&self, _table: &Table) -> bool {
fn matches_component_set(&self, _component_set: &SparseArray<ComponentId, usize>) -> bool {
true
}
}
Expand Down Expand Up @@ -1153,15 +1135,15 @@ impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch<T> {
archetype: &'w Archetype,
tables: &'w Tables,
) {
self.matches = state.state.matches_archetype(archetype);
self.matches = state.state.matches_component_set(archetype.component_ids());
if self.matches {
self.fetch.set_archetype(&state.state, archetype, tables);
}
}

#[inline]
unsafe fn set_table(&mut self, state: &Self::State, table: &'w Table) {
self.matches = state.state.matches_table(table);
self.matches = state.state.matches_component_set(table.component_ids());
if self.matches {
self.fetch.set_table(&state.state, table);
}
Expand Down Expand Up @@ -1297,12 +1279,8 @@ unsafe impl<T: Component> FetchState for ChangeTrackersState<T> {
}
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
component_set.contains(self.component_id)
}
}

Expand Down Expand Up @@ -1551,14 +1529,9 @@ macro_rules! impl_tuple_fetch {
$($name.update_archetype_component_access(_archetype, _access);)*
}

fn matches_archetype(&self, _archetype: &Archetype) -> bool {
let ($($name,)*) = self;
true $(&& $name.matches_archetype(_archetype))*
}

fn matches_table(&self, _table: &Table) -> bool {
fn matches_component_set(&self, _component_set: &SparseArray<ComponentId, usize>) -> bool {
let ($($name,)*) = self;
true $(&& $name.matches_table(_table))*
true $(&& $name.matches_component_set(_component_set))*
}
}

Expand Down Expand Up @@ -1618,7 +1591,7 @@ macro_rules! impl_anytuple_fetch {
let ($($name,)*) = &mut self.0;
let ($($state,)*) = &_state.0;
$(
$name.1 = $state.matches_archetype(_archetype);
$name.1 = $state.matches_component_set(_archetype.component_ids());
if $name.1 {
$name.0.set_archetype($state, _archetype, _tables);
}
Expand All @@ -1630,7 +1603,7 @@ macro_rules! impl_anytuple_fetch {
let ($($name,)*) = &mut self.0;
let ($($state,)*) = &_state.0;
$(
$name.1 = $state.matches_table(_table);
$name.1 = $state.matches_component_set(_table.component_ids());
if $name.1 {
$name.0.set_table($state, _table);
}
Expand Down Expand Up @@ -1699,20 +1672,14 @@ macro_rules! impl_anytuple_fetch {
fn update_archetype_component_access(&self, _archetype: &Archetype, _access: &mut Access<ArchetypeComponentId>) {
let ($($name,)*) = &self.0;
$(
if $name.matches_archetype(_archetype) {
if $name.matches_component_set(_archetype.component_ids()) {
$name.update_archetype_component_access(_archetype, _access);
}
)*
}

fn matches_archetype(&self, _archetype: &Archetype) -> bool {
let ($($name,)*) = &self.0;
false $(|| $name.matches_archetype(_archetype))*
}

fn matches_table(&self, _table: &Table) -> bool {
fn matches_component_set(&self, _component_set: &SparseArray<ComponentId, usize>) -> bool {
let ($($name,)*) = &self.0;
false $(|| $name.matches_table(_table))*
false $(|| $name.matches_component_set(_component_set))*
}
}

Expand Down
40 changes: 11 additions & 29 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
debug_checked_unreachable, Access, Fetch, FetchState, FilteredAccess, QueryFetch,
ROQueryFetch, WorldQuery, WorldQueryGats,
},
storage::{ComponentSparseSet, Table, Tables},
storage::{ComponentSparseSet, SparseArray, Table, Tables},
world::World,
};
use bevy_ecs_macros::all_tuples;
Expand Down Expand Up @@ -90,12 +90,8 @@ unsafe impl<T: Component> FetchState for WithState<T> {
) {
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
component_set.contains(self.component_id)
}
}

Expand Down Expand Up @@ -232,13 +228,8 @@ unsafe impl<T: Component> FetchState for WithoutState<T> {
_access: &mut Access<ArchetypeComponentId>,
) {
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
!archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
!table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
!component_set.contains(self.component_id)
}
}

Expand Down Expand Up @@ -390,7 +381,7 @@ macro_rules! impl_query_filter_tuple {
let ($($filter,)*) = &mut self.0;
let ($($state,)*) = &state.0;
$(
$filter.matches = $state.matches_table(table);
$filter.matches = $state.matches_component_set(table.component_ids());
if $filter.matches {
$filter.fetch.set_table($state, table);
}
Expand All @@ -402,7 +393,7 @@ macro_rules! impl_query_filter_tuple {
let ($($filter,)*) = &mut self.0;
let ($($state,)*) = &state.0;
$(
$filter.matches = $state.matches_archetype(archetype);
$filter.matches = $state.matches_component_set(archetype.component_ids());
if $filter.matches {
$filter.fetch.set_archetype($state, archetype, tables);
}
Expand Down Expand Up @@ -477,14 +468,9 @@ macro_rules! impl_query_filter_tuple {
$($filter.update_archetype_component_access(archetype, access);)*
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
fn matches_component_set(&self, _component_set: &SparseArray<ComponentId, usize>) -> bool {
let ($($filter,)*) = &self.0;
false $(|| $filter.matches_archetype(archetype))*
}

fn matches_table(&self, table: &Table) -> bool {
let ($($filter,)*) = &self.0;
false $(|| $filter.matches_table(table))*
false $(|| $filter.matches_component_set(_component_set))*
}
}

Expand Down Expand Up @@ -564,12 +550,8 @@ macro_rules! impl_tick_filter {
}
}

fn matches_archetype(&self, archetype: &Archetype) -> bool {
archetype.contains(self.component_id)
}

fn matches_table(&self, table: &Table) -> bool {
table.has_column(self.component_id)
fn matches_component_set(&self, component_set: &SparseArray<ComponentId, usize>) -> bool {
component_set.contains(self.component_id)
}
}

Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F> {

/// Creates a new [`Archetype`].
pub fn new_archetype(&mut self, archetype: &Archetype) {
if self.fetch_state.matches_archetype(archetype)
&& self.filter_state.matches_archetype(archetype)
if self
.fetch_state
.matches_component_set(archetype.component_ids())
&& self
.filter_state
.matches_component_set(archetype.component_ids())
{
self.fetch_state
.update_archetype_component_access(archetype, &mut self.archetype_component_access);
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_ecs/src/storage/sparse_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl<I: SparseSetIndex, V> SparseSet<I, V> {
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.dense.iter_mut()
}

pub fn sparse_array(&self) -> &SparseArray<I, usize> {
BoxyUwU marked this conversation as resolved.
Show resolved Hide resolved
&self.sparse
}
}

pub trait SparseSetIndex: Clone + PartialEq + Eq + Hash {
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_ecs/src/storage/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ impl Table {
column.clear();
}
}

pub fn component_ids(&self) -> &super::SparseArray<ComponentId, usize> {
BoxyUwU marked this conversation as resolved.
Show resolved Hide resolved
self.columns.sparse_array()
}
}

/// A collection of [`Table`] storages, indexed by [`TableId`]
Expand Down