Make a note about the performance of Query::is_empty (#12466)

# Objective
`Query::is_empty` does not mention the potential performance footgun of
using it with non-archetypal filters.

## Solution
Document it.
This commit is contained in:
James Liu 2024-03-13 18:36:03 -07:00 committed by GitHub
parent ee0fa7d1c2
commit 4b64d1d1d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -188,9 +188,17 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// Checks if the query is empty for the given [`World`], where the last change and current tick are given. /// Checks if the query is empty for the given [`World`], where the last change and current tick are given.
/// ///
/// This is equivalent to `self.iter().next().is_none()`, and thus the worst case runtime will be `O(n)`
/// where `n` is the number of *potential* matches. This can be notably expensive for queries that rely
/// on non-archetypal filters such as [`Added`] or [`Changed`] which must individually check each query
/// result for a match.
///
/// # Panics /// # Panics
/// ///
/// If `world` does not match the one used to call `QueryState::new` for this instance. /// If `world` does not match the one used to call `QueryState::new` for this instance.
///
/// [`Added`]: crate::query::Added
/// [`Changed`]: crate::query::Changed
#[inline] #[inline]
pub fn is_empty(&self, world: &World, last_run: Tick, this_run: Tick) -> bool { pub fn is_empty(&self, world: &World, last_run: Tick, this_run: Tick) -> bool {
self.validate_world(world.id()); self.validate_world(world.id());

View file

@ -1208,6 +1208,11 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
/// Returns `true` if there are no query items. /// Returns `true` if there are no query items.
/// ///
/// This is equivalent to `self.iter().next().is_none()`, and thus the worst case runtime will be `O(n)`
/// where `n` is the number of *potential* matches. This can be notably expensive for queries that rely
/// on non-archetypal filters such as [`Added`] or [`Changed`] which must individually check each query
/// result for a match.
///
/// # Example /// # Example
/// ///
/// Here, the score is increased only if an entity with a `Player` component is present in the world: /// Here, the score is increased only if an entity with a `Player` component is present in the world:
@ -1226,6 +1231,9 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {
/// } /// }
/// # bevy_ecs::system::assert_is_system(update_score_system); /// # bevy_ecs::system::assert_is_system(update_score_system);
/// ``` /// ```
///
/// [`Added`]: crate::query::Added
/// [`Changed`]: crate::query::Changed
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
// SAFETY: // SAFETY: