Skip to content

Commit

Permalink
Make a note about the performance of Query::is_empty (bevyengine#12466)
Browse files Browse the repository at this point in the history
# Objective
`Query::is_empty` does not mention the potential performance footgun of
using it with non-archetypal filters.

## Solution
Document it.
  • Loading branch information
james7132 authored Mar 14, 2024
1 parent ee0fa7d commit 4b64d1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// 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
///
/// If `world` does not match the one used to call `QueryState::new` for this instance.
///
/// [`Added`]: crate::query::Added
/// [`Changed`]: crate::query::Changed
#[inline]
pub fn is_empty(&self, world: &World, last_run: Tick, this_run: Tick) -> bool {
self.validate_world(world.id());
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,11 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> {

/// 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
///
/// Here, the score is increased only if an entity with a `Player` component is present in the world:
Expand All @@ -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);
/// ```
///
/// [`Added`]: crate::query::Added
/// [`Changed`]: crate::query::Changed
#[inline]
pub fn is_empty(&self) -> bool {
// SAFETY:
Expand Down

0 comments on commit 4b64d1d

Please sign in to comment.