From dd7a21703f31cda228edd3928003f8c46688ade5 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Tue, 28 Feb 2023 17:59:41 +0000 Subject: [PATCH] Move safe operations out of `unsafe` blocks in `Query` (#7851) # Objective Several `Query` methods unnecessarily place the call to `Query::update_archetypes` inside of unsafe blocks. ## Solution Move the method calls out of the unsafe blocks. --- crates/bevy_ecs/src/query/state.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 8c8fd43edf..f4ae1a6837 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -502,9 +502,9 @@ impl QueryState { &'s mut self, world: &'w World, ) -> QueryIter<'w, 's, Q::ReadOnly, F::ReadOnly> { + self.update_archetypes(world); // SAFETY: query is read only unsafe { - self.update_archetypes(world); self.as_readonly().iter_unchecked_manual( world, world.last_change_tick(), @@ -517,11 +517,9 @@ impl QueryState { #[inline] pub fn iter_mut<'w, 's>(&'s mut self, world: &'w mut World) -> QueryIter<'w, 's, Q, F> { let change_tick = world.change_tick(); + self.update_archetypes(world); // SAFETY: query has unique world access - unsafe { - self.update_archetypes(world); - self.iter_unchecked_manual(world, world.last_change_tick(), change_tick) - } + unsafe { self.iter_unchecked_manual(world, world.last_change_tick(), change_tick) } } /// Returns an [`Iterator`] over the query results for the given [`World`] without updating the query's archetypes. @@ -570,9 +568,9 @@ impl QueryState { &'s mut self, world: &'w World, ) -> QueryCombinationIter<'w, 's, Q::ReadOnly, F::ReadOnly, K> { + self.update_archetypes(world); // SAFETY: query is read only unsafe { - self.update_archetypes(world); self.as_readonly().iter_combinations_unchecked_manual( world, world.last_change_tick(), @@ -604,9 +602,9 @@ impl QueryState { world: &'w mut World, ) -> QueryCombinationIter<'w, 's, Q, F, K> { let change_tick = world.change_tick(); + self.update_archetypes(world); // SAFETY: query has unique world access unsafe { - self.update_archetypes(world); self.iter_combinations_unchecked_manual(world, world.last_change_tick(), change_tick) } } @@ -766,9 +764,9 @@ impl QueryState { /// This can only be called for read-only queries, see [`Self::for_each_mut`] for write-queries. #[inline] pub fn for_each<'w, FN: FnMut(ROQueryItem<'w, Q>)>(&mut self, world: &'w World, func: FN) { + self.update_archetypes(world); // SAFETY: query is read only unsafe { - self.update_archetypes(world); self.as_readonly().for_each_unchecked_manual( world, func, @@ -783,9 +781,9 @@ impl QueryState { #[inline] pub fn for_each_mut<'w, FN: FnMut(Q::Item<'w>)>(&mut self, world: &'w mut World, func: FN) { let change_tick = world.change_tick(); + self.update_archetypes(world); // SAFETY: query has unique world access unsafe { - self.update_archetypes(world); self.for_each_unchecked_manual(world, func, world.last_change_tick(), change_tick); } }