Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make a note about the performance of Query::is_empty
  • Loading branch information
james7132 committed Mar 14, 2024
commit ace24e910f4347804e27dd7fc41761bd9394f351
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 @@ -1207,6 +1207,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
///
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
Loading