From a0c722ff4ca3f95b44eddee5098b3bdb86e63812 Mon Sep 17 00:00:00 2001 From: Christian Hughes <9044780+ItsDoot@users.noreply.github.com> Date: Fri, 27 Sep 2024 07:06:40 -0700 Subject: [PATCH] Reduce memory usage in component fetches and change detection filters (#15283) ## Objective - Adopted #6396 ## Solution Same as #6396, we use a compile-time checked `StorageSwitch` union type to select the fetch data based on the component's storage type, saving >= 8 bytes per component fetch in a given query. Note: We forego the Query iteration change as it exists in a slightly different form now on main. ## Testing - All current tests pass locally. --------- Co-authored-by: james7132 Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com> --- crates/bevy_ecs/src/query/fetch.rs | 313 +++++++++++++++++----------- crates/bevy_ecs/src/query/filter.rs | 114 ++++++---- 2 files changed, 268 insertions(+), 159 deletions(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index e2140a36912ff..18a2ecda08155 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1058,19 +1058,22 @@ unsafe impl QueryData for &Archetype { unsafe impl ReadOnlyQueryData for &Archetype {} #[doc(hidden)] -pub struct ReadFetch<'w, T> { - // T::STORAGE_TYPE = StorageType::Table - table_components: Option>>, - // T::STORAGE_TYPE = StorageType::SparseSet - sparse_set: Option<&'w ComponentSparseSet>, +pub struct ReadFetch<'w, T: Component> { + components: StorageSwitch< + T, + // T::STORAGE_TYPE = StorageType::Table + Option>>, + // T::STORAGE_TYPE = StorageType::SparseSet + &'w ComponentSparseSet, + >, } -impl Clone for ReadFetch<'_, T> { +impl Clone for ReadFetch<'_, T> { fn clone(&self) -> Self { *self } } -impl Copy for ReadFetch<'_, T> {} +impl Copy for ReadFetch<'_, T> {} /// SAFETY: /// `fetch` accesses a single component in a readonly way. @@ -1098,20 +1101,22 @@ unsafe impl WorldQuery for &T { _this_run: Tick, ) -> ReadFetch<'w, T> { ReadFetch { - table_components: None, - sparse_set: (T::STORAGE_TYPE == StorageType::SparseSet).then(|| { - // SAFETY: The underlying type associated with `component_id` is `T`, - // which we are allowed to access since we registered it in `update_archetype_component_access`. - // Note that we do not actually access any components in this function, we just get a shared - // reference to the sparse set, which is used to access the components in `Self::fetch`. - unsafe { - world - .storages() - .sparse_sets - .get(component_id) - .debug_checked_unwrap() - } - }), + components: StorageSwitch::new( + || None, + || { + // SAFETY: The underlying type associated with `component_id` is `T`, + // which we are allowed to access since we registered it in `update_archetype_component_access`. + // Note that we do not actually access any components in this function, we just get a shared + // reference to the sparse set, which is used to access the components in `Self::fetch`. + unsafe { + world + .storages() + .sparse_sets + .get(component_id) + .debug_checked_unwrap() + } + }, + ), } } @@ -1143,12 +1148,14 @@ unsafe impl WorldQuery for &T { &component_id: &ComponentId, table: &'w Table, ) { - fetch.table_components = Some( + let table_data = Some( table .get_data_slice_for(component_id) .debug_checked_unwrap() .into(), ); + // SAFETY: set_table is only called when T::STORAGE_TYPE = StorageType::Table + unsafe { fetch.components.set_table(table_data) }; } #[inline(always)] @@ -1157,22 +1164,20 @@ unsafe impl WorldQuery for &T { entity: Entity, table_row: TableRow, ) -> Self::Item<'w> { - match T::STORAGE_TYPE { - StorageType::Table => { - // SAFETY: STORAGE_TYPE = Table - let table = unsafe { fetch.table_components.debug_checked_unwrap() }; + fetch.components.extract( + |table| { + // SAFETY: set_table was previously called + let table = unsafe { table.debug_checked_unwrap() }; // SAFETY: Caller ensures `table_row` is in range. let item = unsafe { table.get(table_row.as_usize()) }; item.deref() - } - StorageType::SparseSet => { - // SAFETY: STORAGE_TYPE = SparseSet - let sparse_set = unsafe { fetch.sparse_set.debug_checked_unwrap() }; + }, + |sparse_set| { // SAFETY: Caller ensures `entity` is in range. let item = unsafe { sparse_set.get(entity).debug_checked_unwrap() }; item.deref() - } - } + }, + ) } fn update_component_access( @@ -1212,27 +1217,29 @@ unsafe impl QueryData for &T { unsafe impl ReadOnlyQueryData for &T {} #[doc(hidden)] -pub struct RefFetch<'w, T> { - // T::STORAGE_TYPE = StorageType::Table - table_data: Option<( - ThinSlicePtr<'w, UnsafeCell>, - ThinSlicePtr<'w, UnsafeCell>, - ThinSlicePtr<'w, UnsafeCell>, - MaybeThinSlicePtrLocation<'w>, - )>, - // T::STORAGE_TYPE = StorageType::SparseSet - sparse_set: Option<&'w ComponentSparseSet>, - +pub struct RefFetch<'w, T: Component> { + components: StorageSwitch< + T, + // T::STORAGE_TYPE = StorageType::Table + Option<( + ThinSlicePtr<'w, UnsafeCell>, + ThinSlicePtr<'w, UnsafeCell>, + ThinSlicePtr<'w, UnsafeCell>, + MaybeThinSlicePtrLocation<'w>, + )>, + // T::STORAGE_TYPE = StorageType::SparseSet + &'w ComponentSparseSet, + >, last_run: Tick, this_run: Tick, } -impl Clone for RefFetch<'_, T> { +impl Clone for RefFetch<'_, T> { fn clone(&self) -> Self { *self } } -impl Copy for RefFetch<'_, T> {} +impl Copy for RefFetch<'_, T> {} /// SAFETY: /// `fetch` accesses a single component in a readonly way. @@ -1260,20 +1267,22 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { this_run: Tick, ) -> RefFetch<'w, T> { RefFetch { - table_data: None, - sparse_set: (T::STORAGE_TYPE == StorageType::SparseSet).then(|| { - // SAFETY: The underlying type associated with `component_id` is `T`, - // which we are allowed to access since we registered it in `update_archetype_component_access`. - // Note that we do not actually access any components in this function, we just get a shared - // reference to the sparse set, which is used to access the components in `Self::fetch`. - unsafe { - world - .storages() - .sparse_sets - .get(component_id) - .debug_checked_unwrap() - } - }), + components: StorageSwitch::new( + || None, + || { + // SAFETY: The underlying type associated with `component_id` is `T`, + // which we are allowed to access since we registered it in `update_archetype_component_access`. + // Note that we do not actually access any components in this function, we just get a shared + // reference to the sparse set, which is used to access the components in `Self::fetch`. + unsafe { + world + .storages() + .sparse_sets + .get(component_id) + .debug_checked_unwrap() + } + }, + ), last_run, this_run, } @@ -1308,7 +1317,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { table: &'w Table, ) { let column = table.get_column(component_id).debug_checked_unwrap(); - fetch.table_data = Some(( + let table_data = Some(( column.get_data_slice(table.entity_count()).into(), column.get_added_ticks_slice(table.entity_count()).into(), column.get_changed_ticks_slice(table.entity_count()).into(), @@ -1317,6 +1326,8 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { #[cfg(not(feature = "track_change_detection"))] (), )); + // SAFETY: set_table is only called when T::STORAGE_TYPE = StorageType::Table + unsafe { fetch.components.set_table(table_data) }; } #[inline(always)] @@ -1325,11 +1336,11 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { entity: Entity, table_row: TableRow, ) -> Self::Item<'w> { - match T::STORAGE_TYPE { - StorageType::Table => { - // SAFETY: STORAGE_TYPE = Table + fetch.components.extract( + |table| { + // SAFETY: set_table was previously called let (table_components, added_ticks, changed_ticks, _callers) = - unsafe { fetch.table_data.debug_checked_unwrap() }; + unsafe { table.debug_checked_unwrap() }; // SAFETY: The caller ensures `table_row` is in range. let component = unsafe { table_components.get(table_row.as_usize()) }; @@ -1352,17 +1363,11 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { #[cfg(feature = "track_change_detection")] changed_by: caller.deref(), } - } - StorageType::SparseSet => { - // SAFETY: STORAGE_TYPE = SparseSet - let component_sparse_set = unsafe { fetch.sparse_set.debug_checked_unwrap() }; - + }, + |sparse_set| { // SAFETY: The caller ensures `entity` is in range. - let (component, ticks, _caller) = unsafe { - component_sparse_set - .get_with_ticks(entity) - .debug_checked_unwrap() - }; + let (component, ticks, _caller) = + unsafe { sparse_set.get_with_ticks(entity).debug_checked_unwrap() }; Ref { value: component.deref(), @@ -1370,8 +1375,8 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> { #[cfg(feature = "track_change_detection")] changed_by: _caller.deref(), } - } - } + }, + ) } fn update_component_access( @@ -1411,27 +1416,29 @@ unsafe impl<'__w, T: Component> QueryData for Ref<'__w, T> { unsafe impl<'__w, T: Component> ReadOnlyQueryData for Ref<'__w, T> {} #[doc(hidden)] -pub struct WriteFetch<'w, T> { - // T::STORAGE_TYPE = StorageType::Table - table_data: Option<( - ThinSlicePtr<'w, UnsafeCell>, - ThinSlicePtr<'w, UnsafeCell>, - ThinSlicePtr<'w, UnsafeCell>, - MaybeThinSlicePtrLocation<'w>, - )>, - // T::STORAGE_TYPE = StorageType::SparseSet - sparse_set: Option<&'w ComponentSparseSet>, - +pub struct WriteFetch<'w, T: Component> { + components: StorageSwitch< + T, + // T::STORAGE_TYPE = StorageType::Table + Option<( + ThinSlicePtr<'w, UnsafeCell>, + ThinSlicePtr<'w, UnsafeCell>, + ThinSlicePtr<'w, UnsafeCell>, + MaybeThinSlicePtrLocation<'w>, + )>, + // T::STORAGE_TYPE = StorageType::SparseSet + &'w ComponentSparseSet, + >, last_run: Tick, this_run: Tick, } -impl Clone for WriteFetch<'_, T> { +impl Clone for WriteFetch<'_, T> { fn clone(&self) -> Self { *self } } -impl Copy for WriteFetch<'_, T> {} +impl Copy for WriteFetch<'_, T> {} /// SAFETY: /// `fetch` accesses a single component mutably. @@ -1459,20 +1466,22 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { this_run: Tick, ) -> WriteFetch<'w, T> { WriteFetch { - table_data: None, - sparse_set: (T::STORAGE_TYPE == StorageType::SparseSet).then(|| { - // SAFETY: The underlying type associated with `component_id` is `T`, - // which we are allowed to access since we registered it in `update_archetype_component_access`. - // Note that we do not actually access any components in this function, we just get a shared - // reference to the sparse set, which is used to access the components in `Self::fetch`. - unsafe { - world - .storages() - .sparse_sets - .get(component_id) - .debug_checked_unwrap() - } - }), + components: StorageSwitch::new( + || None, + || { + // SAFETY: The underlying type associated with `component_id` is `T`, + // which we are allowed to access since we registered it in `update_archetype_component_access`. + // Note that we do not actually access any components in this function, we just get a shared + // reference to the sparse set, which is used to access the components in `Self::fetch`. + unsafe { + world + .storages() + .sparse_sets + .get(component_id) + .debug_checked_unwrap() + } + }, + ), last_run, this_run, } @@ -1507,7 +1516,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { table: &'w Table, ) { let column = table.get_column(component_id).debug_checked_unwrap(); - fetch.table_data = Some(( + let table_data = Some(( column.get_data_slice(table.entity_count()).into(), column.get_added_ticks_slice(table.entity_count()).into(), column.get_changed_ticks_slice(table.entity_count()).into(), @@ -1516,6 +1525,8 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[cfg(not(feature = "track_change_detection"))] (), )); + // SAFETY: set_table is only called when T::STORAGE_TYPE = StorageType::Table + unsafe { fetch.components.set_table(table_data) }; } #[inline(always)] @@ -1524,11 +1535,11 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { entity: Entity, table_row: TableRow, ) -> Self::Item<'w> { - match T::STORAGE_TYPE { - StorageType::Table => { - // SAFETY: STORAGE_TYPE = Table + fetch.components.extract( + |table| { + // SAFETY: set_table was previously called let (table_components, added_ticks, changed_ticks, _callers) = - unsafe { fetch.table_data.debug_checked_unwrap() }; + unsafe { table.debug_checked_unwrap() }; // SAFETY: The caller ensures `table_row` is in range. let component = unsafe { table_components.get(table_row.as_usize()) }; @@ -1551,17 +1562,11 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[cfg(feature = "track_change_detection")] changed_by: caller.deref_mut(), } - } - StorageType::SparseSet => { - // SAFETY: STORAGE_TYPE = SparseSet - let component_sparse_set = unsafe { fetch.sparse_set.debug_checked_unwrap() }; - + }, + |sparse_set| { // SAFETY: The caller ensures `entity` is in range. - let (component, ticks, _caller) = unsafe { - component_sparse_set - .get_with_ticks(entity) - .debug_checked_unwrap() - }; + let (component, ticks, _caller) = + unsafe { sparse_set.get_with_ticks(entity).debug_checked_unwrap() }; Mut { value: component.assert_unique().deref_mut(), @@ -1569,8 +1574,8 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T { #[cfg(feature = "track_change_detection")] changed_by: _caller.deref_mut(), } - } - } + }, + ) } fn update_component_access( @@ -2312,6 +2317,74 @@ unsafe impl QueryData for PhantomData { /// SAFETY: `PhantomData` never accesses any world data. unsafe impl ReadOnlyQueryData for PhantomData {} +/// A compile-time checked union of two different types that differs based on the +/// [`StorageType`] of a given component. +pub(super) union StorageSwitch { + /// The table variant. Requires the component to be a table component. + table: T, + /// The sparse set variant. Requires the component to be a sparse set component. + sparse_set: S, + _marker: PhantomData, +} + +impl StorageSwitch { + /// Creates a new [`StorageSwitch`] using the given closures to initialize + /// the variant corresponding to the component's [`StorageType`]. + pub fn new(table: impl FnOnce() -> T, sparse_set: impl FnOnce() -> S) -> Self { + match C::STORAGE_TYPE { + StorageType::Table => Self { table: table() }, + StorageType::SparseSet => Self { + sparse_set: sparse_set(), + }, + } + } + + /// Creates a new [`StorageSwitch`] using a table variant. + /// + /// # Panics + /// + /// This will panic on debug builds if `C` is not a table component. + /// + /// # Safety + /// + /// `C` must be a table component. + #[inline] + pub unsafe fn set_table(&mut self, table: T) { + match C::STORAGE_TYPE { + StorageType::Table => self.table = table, + _ => { + #[cfg(debug_assertions)] + unreachable!(); + #[cfg(not(debug_assertions))] + std::hint::unreachable_unchecked() + } + } + } + + /// Fetches the internal value from the variant that corresponds to the + /// component's [`StorageType`]. + pub fn extract(&self, table: impl FnOnce(T) -> R, sparse_set: impl FnOnce(S) -> R) -> R { + match C::STORAGE_TYPE { + StorageType::Table => table( + // SAFETY: C::STORAGE_TYPE == StorageType::Table + unsafe { self.table }, + ), + StorageType::SparseSet => sparse_set( + // SAFETY: C::STORAGE_TYPE == StorageType::SparseSet + unsafe { self.sparse_set }, + ), + } + } +} + +impl Clone for StorageSwitch { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for StorageSwitch {} + #[cfg(test)] mod tests { use bevy_ecs_macros::QueryData; diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index 8395d548989ef..e75f506039a29 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -2,7 +2,7 @@ use crate::{ archetype::Archetype, component::{Component, ComponentId, Components, StorageType, Tick}, entity::Entity, - query::{DebugCheckedUnwrap, FilteredAccess, WorldQuery}, + query::{DebugCheckedUnwrap, FilteredAccess, StorageSwitch, WorldQuery}, storage::{ComponentSparseSet, Table, TableRow}, world::{unsafe_world_cell::UnsafeWorldCell, World}, }; @@ -615,14 +615,28 @@ all_tuples!(impl_or_query_filter, 0, 15, F, S); pub struct Added(PhantomData); #[doc(hidden)] -#[derive(Clone)] -pub struct AddedFetch<'w> { - table_ticks: Option>>, - sparse_set: Option<&'w ComponentSparseSet>, +pub struct AddedFetch<'w, T: Component> { + ticks: StorageSwitch< + T, + // T::STORAGE_TYPE = StorageType::Table + Option>>, + // T::STORAGE_TYPE = StorageType::SparseSet + &'w ComponentSparseSet, + >, last_run: Tick, this_run: Tick, } +impl Clone for AddedFetch<'_, T> { + fn clone(&self) -> Self { + Self { + ticks: self.ticks, + last_run: self.last_run, + this_run: self.this_run, + } + } +} + /// SAFETY: /// `fetch` accesses a single component in a readonly way. /// This is sound because `update_component_access` adds read access for that component and panics when appropriate. @@ -630,7 +644,7 @@ pub struct AddedFetch<'w> { /// This is sound because `matches_component_set` returns whether the set contains that component. unsafe impl WorldQuery for Added { type Item<'w> = bool; - type Fetch<'w> = AddedFetch<'w>; + type Fetch<'w> = AddedFetch<'w, T>; type State = ComponentId; fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { @@ -649,9 +663,16 @@ unsafe impl WorldQuery for Added { this_run: Tick, ) -> Self::Fetch<'w> { Self::Fetch::<'w> { - table_ticks: None, - sparse_set: (T::STORAGE_TYPE == StorageType::SparseSet) - .then(|| world.storages().sparse_sets.get(id).debug_checked_unwrap()), + ticks: StorageSwitch::new( + || None, + || { + // SAFETY: The underlying type associated with `component_id` is `T`, + // which we are allowed to access since we registered it in `update_archetype_component_access`. + // Note that we do not actually access any components' ticks in this function, we just get a shared + // reference to the sparse set, which is used to access the components' ticks in `Self::fetch`. + unsafe { world.storages().sparse_sets.get(id).debug_checked_unwrap() } + }, + ), last_run, this_run, } @@ -685,12 +706,14 @@ unsafe impl WorldQuery for Added { &component_id: &ComponentId, table: &'w Table, ) { - fetch.table_ticks = Some( + let table_ticks = Some( table .get_added_ticks_slice_for(component_id) .debug_checked_unwrap() .into(), ); + // SAFETY: set_table is only called when T::STORAGE_TYPE = StorageType::Table + unsafe { fetch.ticks.set_table(table_ticks) }; } #[inline(always)] @@ -699,26 +722,24 @@ unsafe impl WorldQuery for Added { entity: Entity, table_row: TableRow, ) -> Self::Item<'w> { - match T::STORAGE_TYPE { - StorageType::Table => { - // SAFETY: STORAGE_TYPE = Table - let table = unsafe { fetch.table_ticks.debug_checked_unwrap() }; + fetch.ticks.extract( + |table| { + // SAFETY: set_table was previously called + let table = unsafe { table.debug_checked_unwrap() }; // SAFETY: The caller ensures `table_row` is in range. let tick = unsafe { table.get(table_row.as_usize()) }; tick.deref().is_newer_than(fetch.last_run, fetch.this_run) - } - StorageType::SparseSet => { - // SAFETY: STORAGE_TYPE = SparseSet - let sparse_set = unsafe { &fetch.sparse_set.debug_checked_unwrap() }; + }, + |sparse_set| { // SAFETY: The caller ensures `entity` is in range. let tick = unsafe { ComponentSparseSet::get_added_tick(sparse_set, entity).debug_checked_unwrap() }; tick.deref().is_newer_than(fetch.last_run, fetch.this_run) - } - } + }, + ) } #[inline] @@ -833,14 +854,22 @@ unsafe impl QueryFilter for Added { pub struct Changed(PhantomData); #[doc(hidden)] -#[derive(Clone)] -pub struct ChangedFetch<'w> { - table_ticks: Option>>, - sparse_set: Option<&'w ComponentSparseSet>, +pub struct ChangedFetch<'w, T: Component> { + ticks: StorageSwitch>>, &'w ComponentSparseSet>, last_run: Tick, this_run: Tick, } +impl Clone for ChangedFetch<'_, T> { + fn clone(&self) -> Self { + Self { + ticks: self.ticks, + last_run: self.last_run, + this_run: self.this_run, + } + } +} + /// SAFETY: /// `fetch` accesses a single component in a readonly way. /// This is sound because `update_component_access` add read access for that component and panics when appropriate. @@ -848,7 +877,7 @@ pub struct ChangedFetch<'w> { /// This is sound because `matches_component_set` returns whether the set contains that component. unsafe impl WorldQuery for Changed { type Item<'w> = bool; - type Fetch<'w> = ChangedFetch<'w>; + type Fetch<'w> = ChangedFetch<'w, T>; type State = ComponentId; fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> { @@ -867,9 +896,16 @@ unsafe impl WorldQuery for Changed { this_run: Tick, ) -> Self::Fetch<'w> { Self::Fetch::<'w> { - table_ticks: None, - sparse_set: (T::STORAGE_TYPE == StorageType::SparseSet) - .then(|| world.storages().sparse_sets.get(id).debug_checked_unwrap()), + ticks: StorageSwitch::new( + || None, + || { + // SAFETY: The underlying type associated with `component_id` is `T`, + // which we are allowed to access since we registered it in `update_archetype_component_access`. + // Note that we do not actually access any components' ticks in this function, we just get a shared + // reference to the sparse set, which is used to access the components' ticks in `Self::fetch`. + unsafe { world.storages().sparse_sets.get(id).debug_checked_unwrap() } + }, + ), last_run, this_run, } @@ -903,12 +939,14 @@ unsafe impl WorldQuery for Changed { &component_id: &ComponentId, table: &'w Table, ) { - fetch.table_ticks = Some( + let table_ticks = Some( table .get_changed_ticks_slice_for(component_id) .debug_checked_unwrap() .into(), ); + // SAFETY: set_table is only called when T::STORAGE_TYPE = StorageType::Table + unsafe { fetch.ticks.set_table(table_ticks) }; } #[inline(always)] @@ -917,26 +955,24 @@ unsafe impl WorldQuery for Changed { entity: Entity, table_row: TableRow, ) -> Self::Item<'w> { - match T::STORAGE_TYPE { - StorageType::Table => { - // SAFETY: STORAGE_TYPE = Table - let table = unsafe { fetch.table_ticks.debug_checked_unwrap() }; + fetch.ticks.extract( + |table| { + // SAFETY: set_table was previously called + let table = unsafe { table.debug_checked_unwrap() }; // SAFETY: The caller ensures `table_row` is in range. let tick = unsafe { table.get(table_row.as_usize()) }; tick.deref().is_newer_than(fetch.last_run, fetch.this_run) - } - StorageType::SparseSet => { - // SAFETY: STORAGE_TYPE = SparseSet - let sparse_set = unsafe { &fetch.sparse_set.debug_checked_unwrap() }; + }, + |sparse_set| { // SAFETY: The caller ensures `entity` is in range. let tick = unsafe { ComponentSparseSet::get_changed_tick(sparse_set, entity).debug_checked_unwrap() }; tick.deref().is_newer_than(fetch.last_run, fetch.this_run) - } - } + }, + ) } #[inline]