Skip to content

Commit fca1c86

Browse files
committed
Make change lifespan deterministic and update docs (#3956)
## Objective - ~~Make absurdly long-lived changes stay detectable for even longer (without leveling up to `u64`).~~ - Give all changes a consistent maximum lifespan. - Improve code clarity. ## Solution - ~~Increase the frequency of `check_tick` scans to increase the oldest reliably-detectable change.~~ (Deferred until we can benchmark the cost of a scan.) - Ignore changes older than the maximum reliably-detectable age. - General refactoring—name the constants, use them everywhere, and update the docs. - Update test cases to check for the specified behavior. ## Related This PR addresses (at least partially) the concerns raised in: - #3071 - #3082 (and associated PR #3084) ## Background - #1471 Given the minimum interval between `check_ticks` scans, `N`, the oldest reliably-detectable change is `u32::MAX - (2 * N - 1)` (or `MAX_CHANGE_AGE`). Reducing `N` from ~530 million (current value) to something like ~2 million would extend the lifetime of changes by a billion. | minimum `check_ticks` interval | oldest reliably-detectable change | usable % of `u32::MAX` | | --- | --- | --- | | `u32::MAX / 8` (536,870,911) | `(u32::MAX / 4) * 3` | 75.0% | | `2_000_000` | `u32::MAX - 3_999_999` | 99.9% | Similarly, changes are still allowed to be between `MAX_CHANGE_AGE`-old and `u32::MAX`-old in the interim between `check_tick` scans. While we prevent their age from overflowing, the test to detect changes still compares raw values. This makes failure ultimately unreliable, since when ancient changes stop being detected varies depending on when the next scan occurs. ## Open Question Currently, systems and system states are incorrectly initialized with their `last_change_tick` set to `0`, which doesn't handle wraparound correctly. For consistent behavior, they should either be initialized to the world's `last_change_tick` (and detect no changes) or to `MAX_CHANGE_AGE` behind the world's current `change_tick` (and detect everything as a change). I've currently gone with the latter since that was closer to the existing behavior. ## Follow-up Work (Edited: entire section) We haven't actually profiled how long a `check_ticks` scan takes on a "large" `World` , so we don't know if it's safe to increase their frequency. However, we are currently relying on play sessions not lasting long enough to trigger a scan and apps not having enough entities/archetypes for it to be "expensive" (our assumption). That isn't a real solution. (Either scanning never costs enough to impact frame times or we provide an option to use `u64` change ticks. Nobody will accept random hiccups.) To further extend the lifetime of changes, we actually only need to increment the world tick if a system has `Fetch: !ReadOnlySystemParamFetch`. The behavior will be identical because all writes are sequenced, but I'm not sure how to implement that in a way that the compiler can optimize the branch out. Also, since having no false positives depends on a `check_ticks` scan running at least every `2 * N - 1` ticks, a `last_check_tick` should also be stored in the `World` so that any lull in system execution (like a command flush) could trigger a scan if needed. To be completely robust, all the systems initialized on the world should be scanned, not just those in the current stage.
1 parent 7f73666 commit fca1c86

File tree

8 files changed

+207
-147
lines changed

8 files changed

+207
-147
lines changed

crates/bevy_ecs/src/change_detection.rs

+124-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ use crate::{component::ComponentTicks, system::Resource};
55
use bevy_reflect::Reflect;
66
use std::ops::{Deref, DerefMut};
77

8+
/// The (arbitrarily chosen) minimum number of world tick increments between `check_tick` scans.
9+
///
10+
/// Change ticks can only be scanned when systems aren't running. Thus, if the threshold is `N`,
11+
/// the maximum is `2 * N - 1` (i.e. the world ticks `N - 1` times, then `N` times).
12+
///
13+
/// If no change is older than `u32::MAX - (2 * N - 1)` following a scan, none of their ages can
14+
/// overflow and cause false positives.
15+
// (518,400,000 = 1000 ticks per frame * 144 frames per second * 3600 seconds per hour)
16+
pub const CHECK_TICK_THRESHOLD: u32 = 518_400_000;
17+
18+
/// The maximum change tick difference that won't overflow before the next `check_tick` scan.
19+
///
20+
/// Changes stop being detected once they become this old.
21+
pub const MAX_CHANGE_AGE: u32 = u32::MAX - (2 * CHECK_TICK_THRESHOLD - 1);
22+
823
/// Types that implement reliable change detection.
924
///
1025
/// ## Example
@@ -28,19 +43,18 @@ use std::ops::{Deref, DerefMut};
2843
/// ```
2944
///
3045
pub trait DetectChanges {
31-
/// Returns true if (and only if) this value been added since the last execution of this
32-
/// system.
46+
/// Returns `true` if this value was added after the system last ran.
3347
fn is_added(&self) -> bool;
3448

35-
/// Returns true if (and only if) this value been changed since the last execution of this
36-
/// system.
49+
/// Returns `true` if this value was added or mutably dereferenced after the system last ran.
3750
fn is_changed(&self) -> bool;
3851

39-
/// Manually flags this value as having been changed. This normally isn't
40-
/// required because accessing this pointer mutably automatically flags this
41-
/// value as "changed".
52+
/// Flags this value as having been changed.
4253
///
43-
/// **Note**: This operation is irreversible.
54+
/// Mutably accessing this smart pointer will automatically flag this value as having been changed.
55+
/// However, mutation through interior mutability requires manual reporting.
56+
///
57+
/// **Note**: This operation cannot be undone.
4458
fn set_changed(&mut self);
4559

4660
/// Returns the change tick recording the previous time this component (or resource) was changed.
@@ -213,3 +227,105 @@ pub struct ReflectMut<'a> {
213227
change_detection_impl!(ReflectMut<'a>, dyn Reflect,);
214228
#[cfg(feature = "bevy_reflect")]
215229
impl_into_inner!(ReflectMut<'a>, dyn Reflect,);
230+
231+
#[cfg(test)]
232+
mod tests {
233+
use crate::{
234+
self as bevy_ecs,
235+
change_detection::{CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE},
236+
component::Component,
237+
query::ChangeTrackers,
238+
system::{IntoSystem, Query, System},
239+
world::World,
240+
};
241+
242+
#[derive(Component)]
243+
struct C;
244+
245+
#[test]
246+
fn change_expiration() {
247+
fn change_detected(query: Query<ChangeTrackers<C>>) -> bool {
248+
query.single().is_changed()
249+
}
250+
251+
fn change_expired(query: Query<ChangeTrackers<C>>) -> bool {
252+
query.single().is_changed()
253+
}
254+
255+
let mut world = World::new();
256+
257+
// component added: 1, changed: 1
258+
world.spawn().insert(C);
259+
260+
let mut change_detected_system = IntoSystem::into_system(change_detected);
261+
let mut change_expired_system = IntoSystem::into_system(change_expired);
262+
change_detected_system.initialize(&mut world);
263+
change_expired_system.initialize(&mut world);
264+
265+
// world: 1, system last ran: 0, component changed: 1
266+
// The spawn will be detected since it happened after the system "last ran".
267+
assert!(change_detected_system.run((), &mut world));
268+
269+
// world: 1 + MAX_CHANGE_AGE
270+
let change_tick = world.change_tick.get_mut();
271+
*change_tick = change_tick.wrapping_add(MAX_CHANGE_AGE);
272+
273+
// Both the system and component appeared `MAX_CHANGE_AGE` ticks ago.
274+
// Since we clamp things to `MAX_CHANGE_AGE` for determinism,
275+
// `ComponentTicks::is_changed` will now see `MAX_CHANGE_AGE > MAX_CHANGE_AGE`
276+
// and return `false`.
277+
assert!(!change_expired_system.run((), &mut world));
278+
}
279+
280+
#[test]
281+
fn change_tick_wraparound() {
282+
fn change_detected(query: Query<ChangeTrackers<C>>) -> bool {
283+
query.single().is_changed()
284+
}
285+
286+
let mut world = World::new();
287+
world.last_change_tick = u32::MAX;
288+
*world.change_tick.get_mut() = 0;
289+
290+
// component added: 0, changed: 0
291+
world.spawn().insert(C);
292+
293+
// system last ran: u32::MAX
294+
let mut change_detected_system = IntoSystem::into_system(change_detected);
295+
change_detected_system.initialize(&mut world);
296+
297+
// Since the world is always ahead, as long as changes can't get older than `u32::MAX` (which we ensure),
298+
// the wrapping difference will always be positive, so wraparound doesn't matter.
299+
assert!(change_detected_system.run((), &mut world));
300+
}
301+
302+
#[test]
303+
fn change_tick_scan() {
304+
let mut world = World::new();
305+
306+
// component added: 1, changed: 1
307+
world.spawn().insert(C);
308+
309+
// a bunch of stuff happens, the component is now older than `MAX_CHANGE_AGE`
310+
*world.change_tick.get_mut() += MAX_CHANGE_AGE + CHECK_TICK_THRESHOLD;
311+
let change_tick = world.change_tick();
312+
313+
let mut query = world.query::<ChangeTrackers<C>>();
314+
for tracker in query.iter(&world) {
315+
let ticks_since_insert = change_tick.wrapping_sub(tracker.component_ticks.added);
316+
let ticks_since_change = change_tick.wrapping_sub(tracker.component_ticks.changed);
317+
assert!(ticks_since_insert > MAX_CHANGE_AGE);
318+
assert!(ticks_since_change > MAX_CHANGE_AGE);
319+
}
320+
321+
// scan change ticks and clamp those at risk of overflow
322+
world.check_change_ticks();
323+
324+
for tracker in query.iter(&world) {
325+
let ticks_since_insert = change_tick.wrapping_sub(tracker.component_ticks.added);
326+
let ticks_since_change = change_tick.wrapping_sub(tracker.component_ticks.changed);
327+
assert!(ticks_since_insert == MAX_CHANGE_AGE);
328+
assert!(ticks_since_change == MAX_CHANGE_AGE);
329+
}
330+
}
331+
}

crates/bevy_ecs/src/component.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Types for declaring and storing [`Component`]s.
22
33
use crate::{
4+
change_detection::MAX_CHANGE_AGE,
45
storage::{SparseSetIndex, Storages},
56
system::Resource,
67
};
@@ -345,6 +346,7 @@ impl Components {
345346
}
346347
}
347348

349+
/// Records when a component was added and when it was last mutably dereferenced (or added).
348350
#[derive(Copy, Clone, Debug)]
349351
pub struct ComponentTicks {
350352
pub(crate) added: u32,
@@ -353,22 +355,35 @@ pub struct ComponentTicks {
353355

354356
impl ComponentTicks {
355357
#[inline]
358+
/// Returns `true` if the component was added after the system last ran.
356359
pub fn is_added(&self, last_change_tick: u32, change_tick: u32) -> bool {
357-
// The comparison is relative to `change_tick` so that we can detect changes over the whole
358-
// `u32` range. Comparing directly the ticks would limit to half that due to overflow
359-
// handling.
360-
let component_delta = change_tick.wrapping_sub(self.added);
361-
let system_delta = change_tick.wrapping_sub(last_change_tick);
360+
// This works even with wraparound because the world tick (`change_tick`) is always "newer" than
361+
// `last_change_tick` and `self.added`, and we scan periodically to clamp `ComponentTicks` values
362+
// so they never get older than `u32::MAX` (the difference would overflow).
363+
//
364+
// The clamp here ensures determinism (since scans could differ between app runs).
365+
let ticks_since_insert = change_tick.wrapping_sub(self.added).min(MAX_CHANGE_AGE);
366+
let ticks_since_system = change_tick
367+
.wrapping_sub(last_change_tick)
368+
.min(MAX_CHANGE_AGE);
362369

363-
component_delta < system_delta
370+
ticks_since_system > ticks_since_insert
364371
}
365372

366373
#[inline]
374+
/// Returns `true` if the component was added or mutably dereferenced after the system last ran.
367375
pub fn is_changed(&self, last_change_tick: u32, change_tick: u32) -> bool {
368-
let component_delta = change_tick.wrapping_sub(self.changed);
369-
let system_delta = change_tick.wrapping_sub(last_change_tick);
376+
// This works even with wraparound because the world tick (`change_tick`) is always "newer" than
377+
// `last_change_tick` and `self.changed`, and we scan periodically to clamp `ComponentTicks` values
378+
// so they never get older than `u32::MAX` (the difference would overflow).
379+
//
380+
// The clamp here ensures determinism (since scans could differ between app runs).
381+
let ticks_since_change = change_tick.wrapping_sub(self.changed).min(MAX_CHANGE_AGE);
382+
let ticks_since_system = change_tick
383+
.wrapping_sub(last_change_tick)
384+
.min(MAX_CHANGE_AGE);
370385

371-
component_delta < system_delta
386+
ticks_since_system > ticks_since_change
372387
}
373388

374389
pub(crate) fn new(change_tick: u32) -> Self {
@@ -384,8 +399,10 @@ impl ComponentTicks {
384399
}
385400

386401
/// Manually sets the change tick.
387-
/// Usually, this is done automatically via the [`DerefMut`](std::ops::DerefMut) implementation
388-
/// on [`Mut`](crate::world::Mut) or [`ResMut`](crate::system::ResMut) etc.
402+
///
403+
/// This is normally done automatically via the [`DerefMut`](std::ops::DerefMut) implementation
404+
/// on [`Mut<T>`](crate::change_detection::Mut), [`ResMut<T>`](crate::change_detection::ResMut), etc.
405+
/// However, components and resources that make use of interior mutability might require manual updates.
389406
///
390407
/// # Example
391408
/// ```rust,no_run
@@ -402,10 +419,10 @@ impl ComponentTicks {
402419
}
403420

404421
fn check_tick(last_change_tick: &mut u32, change_tick: u32) {
405-
let tick_delta = change_tick.wrapping_sub(*last_change_tick);
406-
const MAX_DELTA: u32 = (u32::MAX / 4) * 3;
407-
// Clamp to max delta
408-
if tick_delta > MAX_DELTA {
409-
*last_change_tick = change_tick.wrapping_sub(MAX_DELTA);
422+
let age = change_tick.wrapping_sub(*last_change_tick);
423+
// This comparison assumes that `age` has not overflowed `u32::MAX` before, which will be true
424+
// so long as this check always runs before that can happen.
425+
if age > MAX_CHANGE_AGE {
426+
*last_change_tick = change_tick.wrapping_sub(MAX_CHANGE_AGE);
410427
}
411428
}

crates/bevy_ecs/src/query/filter.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -650,17 +650,12 @@ macro_rules! impl_tick_filter {
650650
}
651651

652652
impl_tick_filter!(
653-
/// Filter that retrieves components of type `T` that have been added since the last execution
654-
/// of this system.
653+
/// A filter on a component that only retains results added after the system last ran.
655654
///
656-
/// This filter is useful to do one-time post-processing on components.
655+
/// A common use for this filter is one-time initialization.
657656
///
658-
/// Because the ordering of systems can change and this filter is only effective on changes
659-
/// before the query executes you need to use explicit dependency ordering or ordered stages to
660-
/// avoid frame delays.
661-
///
662-
/// If instead behavior is meant to change on whether the component changed or not
663-
/// [`ChangeTrackers`](crate::query::ChangeTrackers) may be used.
657+
/// To retain all results without filtering but still check whether they were added after the
658+
/// system last ran, use [`ChangeTrackers<T>`](crate::query::ChangeTrackers).
664659
///
665660
/// # Examples
666661
///
@@ -690,18 +685,15 @@ impl_tick_filter!(
690685
);
691686

692687
impl_tick_filter!(
693-
/// Filter that retrieves components of type `T` that have been changed since the last
694-
/// execution of this system.
695-
///
696-
/// This filter is useful for synchronizing components, and as a performance optimization as it
697-
/// means that the query contains fewer items for a system to iterate over.
688+
/// A filter on a component that only retains results added or mutably dereferenced after the system last ran.
689+
///
690+
/// A common use for this filter is avoiding redundant work when values have not changed.
698691
///
699-
/// Because the ordering of systems can change and this filter is only effective on changes
700-
/// before the query executes you need to use explicit dependency ordering or ordered
701-
/// stages to avoid frame delays.
692+
/// **Note** that simply *mutably dereferencing* a component is considered a change ([`DerefMut`](std::ops::DerefMut)).
693+
/// Bevy does not compare components to their previous values.
702694
///
703-
/// If instead behavior is meant to change on whether the component changed or not
704-
/// [`ChangeTrackers`](crate::query::ChangeTrackers) may be used.
695+
/// To retain all results without filtering but still check whether they were changed after the
696+
/// system last ran, use [`ChangeTrackers<T>`](crate::query::ChangeTrackers).
705697
///
706698
/// # Examples
707699
///

0 commit comments

Comments
 (0)