Skip to content

Commit e528b63

Browse files
committed
merge matches_archetype and matches_table (#4807)
# Objective the code in these fns are always identical so stop having two functions ## Solution make them the same function --- ## Changelog change `matches_archetype` and `matches_table` to `fn matches_component_set(&self, &SparseArray<ComponentId, usize>) -> bool` then do extremely boring updating of all `FetchState` impls ## Migration Guide - move logic of `matches_archetype` and `matches_table` into `matches_component_set` in any manual `FetchState` impls
1 parent 2f5591f commit e528b63

File tree

4 files changed

+45
-88
lines changed

4 files changed

+45
-88
lines changed

crates/bevy_ecs/macros/src/fetch.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,9 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
279279
#(self.#field_idents.update_archetype_component_access(_archetype, _access);)*
280280
}
281281

282-
fn matches_archetype(&self, _archetype: &#path::archetype::Archetype) -> bool {
283-
true #(&& self.#field_idents.matches_archetype(_archetype))*
284-
}
282+
fn matches_component_set(&self, _set_contains_id: &impl Fn(#path::component::ComponentId) -> bool) -> bool {
283+
true #(&& self.#field_idents.matches_component_set(_set_contains_id))*
285284

286-
fn matches_table(&self, _table: &#path::storage::Table) -> bool {
287-
true #(&& self.#field_idents.matches_table(_table))*
288285
}
289286
}
290287
};

crates/bevy_ecs/src/query/fetch.rs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ pub trait Fetch<'world>: Sized {
421421
///
422422
/// Implementor must ensure that [`FetchState::update_component_access`] and
423423
/// [`FetchState::update_archetype_component_access`] exactly reflects the results of
424-
/// [`FetchState::matches_archetype`], [`FetchState::matches_table`], [`Fetch::archetype_fetch`], and
424+
/// [`FetchState::matches_component_set`], [`Fetch::archetype_fetch`], and
425425
/// [`Fetch::table_fetch`].
426426
pub unsafe trait FetchState: Send + Sync + Sized {
427427
fn init(world: &mut World) -> Self;
@@ -431,8 +431,7 @@ pub unsafe trait FetchState: Send + Sync + Sized {
431431
archetype: &Archetype,
432432
access: &mut Access<ArchetypeComponentId>,
433433
);
434-
fn matches_archetype(&self, archetype: &Archetype) -> bool;
435-
fn matches_table(&self, table: &Table) -> bool;
434+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool;
436435
}
437436

438437
/// A fetch that is read only.
@@ -480,12 +479,7 @@ unsafe impl FetchState for EntityState {
480479
}
481480

482481
#[inline]
483-
fn matches_archetype(&self, _archetype: &Archetype) -> bool {
484-
true
485-
}
486-
487-
#[inline]
488-
fn matches_table(&self, _table: &Table) -> bool {
482+
fn matches_component_set(&self, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
489483
true
490484
}
491485
}
@@ -588,12 +582,8 @@ unsafe impl<T: Component> FetchState for ReadState<T> {
588582
}
589583
}
590584

591-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
592-
archetype.contains(self.component_id)
593-
}
594-
595-
fn matches_table(&self, table: &Table) -> bool {
596-
table.has_column(self.component_id)
585+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
586+
set_contains_id(self.component_id)
597587
}
598588
}
599589

@@ -825,12 +815,8 @@ unsafe impl<T: Component> FetchState for WriteState<T> {
825815
}
826816
}
827817

828-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
829-
archetype.contains(self.component_id)
830-
}
831-
832-
fn matches_table(&self, table: &Table) -> bool {
833-
table.has_column(self.component_id)
818+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
819+
set_contains_id(self.component_id)
834820
}
835821
}
836822

@@ -1105,17 +1091,16 @@ unsafe impl<T: FetchState> FetchState for OptionState<T> {
11051091
archetype: &Archetype,
11061092
access: &mut Access<ArchetypeComponentId>,
11071093
) {
1108-
if self.state.matches_archetype(archetype) {
1094+
if self
1095+
.state
1096+
.matches_component_set(&|id| archetype.contains(id))
1097+
{
11091098
self.state
11101099
.update_archetype_component_access(archetype, access);
11111100
}
11121101
}
11131102

1114-
fn matches_archetype(&self, _archetype: &Archetype) -> bool {
1115-
true
1116-
}
1117-
1118-
fn matches_table(&self, _table: &Table) -> bool {
1103+
fn matches_component_set(&self, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
11191104
true
11201105
}
11211106
}
@@ -1153,15 +1138,19 @@ impl<'w, T: Fetch<'w>> Fetch<'w> for OptionFetch<T> {
11531138
archetype: &'w Archetype,
11541139
tables: &'w Tables,
11551140
) {
1156-
self.matches = state.state.matches_archetype(archetype);
1141+
self.matches = state
1142+
.state
1143+
.matches_component_set(&|id| archetype.contains(id));
11571144
if self.matches {
11581145
self.fetch.set_archetype(&state.state, archetype, tables);
11591146
}
11601147
}
11611148

11621149
#[inline]
11631150
unsafe fn set_table(&mut self, state: &Self::State, table: &'w Table) {
1164-
self.matches = state.state.matches_table(table);
1151+
self.matches = state
1152+
.state
1153+
.matches_component_set(&|id| table.has_column(id));
11651154
if self.matches {
11661155
self.fetch.set_table(&state.state, table);
11671156
}
@@ -1297,12 +1286,8 @@ unsafe impl<T: Component> FetchState for ChangeTrackersState<T> {
12971286
}
12981287
}
12991288

1300-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
1301-
archetype.contains(self.component_id)
1302-
}
1303-
1304-
fn matches_table(&self, table: &Table) -> bool {
1305-
table.has_column(self.component_id)
1289+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
1290+
set_contains_id(self.component_id)
13061291
}
13071292
}
13081293

@@ -1551,14 +1536,9 @@ macro_rules! impl_tuple_fetch {
15511536
$($name.update_archetype_component_access(_archetype, _access);)*
15521537
}
15531538

1554-
fn matches_archetype(&self, _archetype: &Archetype) -> bool {
1555-
let ($($name,)*) = self;
1556-
true $(&& $name.matches_archetype(_archetype))*
1557-
}
1558-
1559-
fn matches_table(&self, _table: &Table) -> bool {
1539+
fn matches_component_set(&self, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
15601540
let ($($name,)*) = self;
1561-
true $(&& $name.matches_table(_table))*
1541+
true $(&& $name.matches_component_set(_set_contains_id))*
15621542
}
15631543
}
15641544

@@ -1618,7 +1598,7 @@ macro_rules! impl_anytuple_fetch {
16181598
let ($($name,)*) = &mut self.0;
16191599
let ($($state,)*) = &_state.0;
16201600
$(
1621-
$name.1 = $state.matches_archetype(_archetype);
1601+
$name.1 = $state.matches_component_set(&|id| _archetype.contains(id));
16221602
if $name.1 {
16231603
$name.0.set_archetype($state, _archetype, _tables);
16241604
}
@@ -1630,7 +1610,7 @@ macro_rules! impl_anytuple_fetch {
16301610
let ($($name,)*) = &mut self.0;
16311611
let ($($state,)*) = &_state.0;
16321612
$(
1633-
$name.1 = $state.matches_table(_table);
1613+
$name.1 = $state.matches_component_set(&|id| _table.has_column(id));
16341614
if $name.1 {
16351615
$name.0.set_table($state, _table);
16361616
}
@@ -1699,20 +1679,14 @@ macro_rules! impl_anytuple_fetch {
16991679
fn update_archetype_component_access(&self, _archetype: &Archetype, _access: &mut Access<ArchetypeComponentId>) {
17001680
let ($($name,)*) = &self.0;
17011681
$(
1702-
if $name.matches_archetype(_archetype) {
1682+
if $name.matches_component_set(&|id| _archetype.contains(id)) {
17031683
$name.update_archetype_component_access(_archetype, _access);
17041684
}
17051685
)*
17061686
}
1707-
1708-
fn matches_archetype(&self, _archetype: &Archetype) -> bool {
1709-
let ($($name,)*) = &self.0;
1710-
false $(|| $name.matches_archetype(_archetype))*
1711-
}
1712-
1713-
fn matches_table(&self, _table: &Table) -> bool {
1687+
fn matches_component_set(&self, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
17141688
let ($($name,)*) = &self.0;
1715-
false $(|| $name.matches_table(_table))*
1689+
false $(|| $name.matches_component_set(_set_contains_id))*
17161690
}
17171691
}
17181692

crates/bevy_ecs/src/query/filter.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ unsafe impl<T: Component> FetchState for WithState<T> {
9090
) {
9191
}
9292

93-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
94-
archetype.contains(self.component_id)
95-
}
96-
97-
fn matches_table(&self, table: &Table) -> bool {
98-
table.has_column(self.component_id)
93+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
94+
set_contains_id(self.component_id)
9995
}
10096
}
10197

@@ -232,13 +228,8 @@ unsafe impl<T: Component> FetchState for WithoutState<T> {
232228
_access: &mut Access<ArchetypeComponentId>,
233229
) {
234230
}
235-
236-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
237-
!archetype.contains(self.component_id)
238-
}
239-
240-
fn matches_table(&self, table: &Table) -> bool {
241-
!table.has_column(self.component_id)
231+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
232+
!set_contains_id(self.component_id)
242233
}
243234
}
244235

@@ -390,7 +381,7 @@ macro_rules! impl_query_filter_tuple {
390381
let ($($filter,)*) = &mut self.0;
391382
let ($($state,)*) = &state.0;
392383
$(
393-
$filter.matches = $state.matches_table(table);
384+
$filter.matches = $state.matches_component_set(&|id| table.has_column(id));
394385
if $filter.matches {
395386
$filter.fetch.set_table($state, table);
396387
}
@@ -402,7 +393,7 @@ macro_rules! impl_query_filter_tuple {
402393
let ($($filter,)*) = &mut self.0;
403394
let ($($state,)*) = &state.0;
404395
$(
405-
$filter.matches = $state.matches_archetype(archetype);
396+
$filter.matches = $state.matches_component_set(&|id| archetype.contains(id));
406397
if $filter.matches {
407398
$filter.fetch.set_archetype($state, archetype, tables);
408399
}
@@ -477,14 +468,9 @@ macro_rules! impl_query_filter_tuple {
477468
$($filter.update_archetype_component_access(archetype, access);)*
478469
}
479470

480-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
471+
fn matches_component_set(&self, _set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
481472
let ($($filter,)*) = &self.0;
482-
false $(|| $filter.matches_archetype(archetype))*
483-
}
484-
485-
fn matches_table(&self, table: &Table) -> bool {
486-
let ($($filter,)*) = &self.0;
487-
false $(|| $filter.matches_table(table))*
473+
false $(|| $filter.matches_component_set(_set_contains_id))*
488474
}
489475
}
490476

@@ -564,12 +550,8 @@ macro_rules! impl_tick_filter {
564550
}
565551
}
566552

567-
fn matches_archetype(&self, archetype: &Archetype) -> bool {
568-
archetype.contains(self.component_id)
569-
}
570-
571-
fn matches_table(&self, table: &Table) -> bool {
572-
table.has_column(self.component_id)
553+
fn matches_component_set(&self, set_contains_id: &impl Fn(ComponentId) -> bool) -> bool {
554+
set_contains_id(self.component_id)
573555
}
574556
}
575557

crates/bevy_ecs/src/query/state.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ impl<Q: WorldQuery, F: WorldQuery> QueryState<Q, F> {
115115

116116
/// Creates a new [`Archetype`].
117117
pub fn new_archetype(&mut self, archetype: &Archetype) {
118-
if self.fetch_state.matches_archetype(archetype)
119-
&& self.filter_state.matches_archetype(archetype)
118+
if self
119+
.fetch_state
120+
.matches_component_set(&|id| archetype.contains(id))
121+
&& self
122+
.filter_state
123+
.matches_component_set(&|id| archetype.contains(id))
120124
{
121125
self.fetch_state
122126
.update_archetype_component_access(archetype, &mut self.archetype_component_access);

0 commit comments

Comments
 (0)