Skip to content

Commit f7aa741

Browse files
Avoid repeated calls to validate_world in Query::get_multiple
1 parent be22235 commit f7aa741

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

crates/bevy_ecs/src/query/state.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ where
193193
) -> Result<[<Q::ReadOnlyFetch as Fetch<'w, 's>>::Item; N], QueryEntityError> {
194194
self.update_archetypes(world);
195195

196-
self.get_multiple_read_only_manual(
197-
world,
198-
entities,
199-
world.last_change_tick(),
200-
world.read_change_tick(),
201-
)
196+
// SAFE: update_archetypes validates the `World` matches
197+
unsafe {
198+
self.get_multiple_read_only_manual(
199+
world,
200+
entities,
201+
world.last_change_tick(),
202+
world.read_change_tick(),
203+
)
204+
}
202205
}
203206

204207
/// Gets the query result for the given [`World`] and [`Entity`].
@@ -360,7 +363,12 @@ where
360363

361364
/// Gets the read-only query results for the given [`World`] and array of [`Entity`], where the last change and
362365
/// the current change tick are given.
363-
pub(crate) fn get_multiple_read_only_manual<'s, 'w, const N: usize>(
366+
///
367+
/// # Safety
368+
///
369+
/// This must be called on the same `World` that the `Query` was generated from:
370+
/// use `QueryState::validate_world` to verify this.
371+
pub(crate) unsafe fn get_multiple_read_only_manual<'s, 'w, const N: usize>(
364372
&'s self,
365373
world: &'w World,
366374
entities: [Entity; N],
@@ -370,16 +378,15 @@ where
370378
self.validate_world(world);
371379

372380
// SAFE: fetch is read-only
373-
let array_of_results = unsafe {
374-
entities.map(|entity| {
375-
self.get_unchecked_manual::<Q::ReadOnlyFetch>(
376-
world,
377-
entity,
378-
last_change_tick,
379-
change_tick,
380-
)
381-
})
382-
};
381+
// and world must be validated
382+
let array_of_results = entities.map(|entity| {
383+
self.get_unchecked_manual::<Q::ReadOnlyFetch>(
384+
world,
385+
entity,
386+
last_change_tick,
387+
change_tick,
388+
)
389+
});
383390

384391
// TODO: Replace with TryMap once https://github.com/rust-lang/rust/issues/79711 is stabilized
385392
// If any of the get calls failed, bubble up the error

crates/bevy_ecs/src/system/query.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,15 @@ where
631631
&self,
632632
entities: [Entity; N],
633633
) -> Result<[<Q::ReadOnlyFetch as Fetch<'_, 's>>::Item; N], QueryEntityError> {
634-
self.state.get_multiple_read_only_manual(
635-
self.world,
636-
entities,
637-
self.last_change_tick,
638-
self.change_tick,
639-
)
634+
// SAFE: it is the scheduler's responsibility to ensure that `Query` is never handed out on the wrong `World`.
635+
unsafe {
636+
self.state.get_multiple_read_only_manual(
637+
self.world,
638+
entities,
639+
self.last_change_tick,
640+
self.change_tick,
641+
)
642+
}
640643
}
641644

642645
/// Returns the read-only query items for the provided array of [`Entity`]

0 commit comments

Comments
 (0)