Skip to content

Commit f5f80cd

Browse files
committed
change ticks
1 parent a400e1b commit f5f80cd

File tree

1 file changed

+26
-8
lines changed
  • crates/bevy_ecs/src/world

1 file changed

+26
-8
lines changed

crates/bevy_ecs/src/world/mod.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod entity_ref;
22
mod spawn_batch;
33
mod world_cell;
44

5-
pub use crate::change_detection::Mut;
5+
pub use crate::change_detection::{Mut, CHECK_TICK_THRESHOLD};
66
pub use entity_ref::*;
77
pub use spawn_batch::*;
88
pub use world_cell::*;
@@ -91,6 +91,7 @@ pub struct World {
9191
main_thread_validator: MainThreadValidator,
9292
pub(crate) change_tick: AtomicU32,
9393
pub(crate) last_change_tick: u32,
94+
pub(crate) last_check_tick: u32,
9495
}
9596

9697
impl Default for World {
@@ -109,6 +110,7 @@ impl Default for World {
109110
// are detected on first system runs and for direct world queries.
110111
change_tick: AtomicU32::new(1),
111112
last_change_tick: 0,
113+
last_check_tick: 0,
112114
}
113115
}
114116
}
@@ -1399,15 +1401,31 @@ impl World {
13991401
self.last_change_tick
14001402
}
14011403

1404+
/// All component change ticks are scanned for risk of age overflow once the world counter
1405+
/// has incremented at least [`CHECK_TICK_THRESHOLD`](crate::change_detection::CHECK_TICK_THRESHOLD)
1406+
/// times since the previous `check_change_ticks` scan.
1407+
///
1408+
/// During each scan, any change ticks older than [`MAX_CHANGE_AGE`](crate::change_detection::MAX_CHANGE_AGE)
1409+
/// are clamped to that age. This prevents false positives that would appear because of overflow.
1410+
// TODO: parallelize
14021411
pub fn check_change_ticks(&mut self) {
1403-
// Iterate over all component change ticks, clamping their age to max age
1404-
// PERF: parallelize
1412+
let last_check_tick = self.last_check_tick;
14051413
let change_tick = self.change_tick();
1406-
self.storages.tables.check_change_ticks(change_tick);
1407-
self.storages.sparse_sets.check_change_ticks(change_tick);
1408-
let resource_archetype = self.archetypes.resource_mut();
1409-
for column in resource_archetype.unique_components.values_mut() {
1410-
column.check_change_ticks(change_tick);
1414+
if change_tick.wrapping_sub(last_check_tick) >= CHECK_TICK_THRESHOLD {
1415+
#[cfg(feature = "trace")]
1416+
let _span = bevy_utils::tracing::info_span!("check component ticks").entered();
1417+
self.storages.tables.check_change_ticks(change_tick);
1418+
self.storages.sparse_sets.check_change_ticks(change_tick);
1419+
let resource_archetype = self.archetypes.resource_mut();
1420+
for column in resource_archetype.unique_components.values_mut() {
1421+
column.check_change_ticks(change_tick);
1422+
}
1423+
1424+
if let Some(mut systems) = self.get_resource_mut::<crate::schedule_v3::Systems>() {
1425+
systems.check_change_ticks(change_tick, last_check_tick);
1426+
}
1427+
1428+
self.last_check_tick = change_tick;
14111429
}
14121430
}
14131431

0 commit comments

Comments
 (0)