Skip to content

Commit

Permalink
change ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Aug 5, 2022
1 parent a400e1b commit f5f80cd
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod entity_ref;
mod spawn_batch;
mod world_cell;

pub use crate::change_detection::Mut;
pub use crate::change_detection::{Mut, CHECK_TICK_THRESHOLD};
pub use entity_ref::*;
pub use spawn_batch::*;
pub use world_cell::*;
Expand Down Expand Up @@ -91,6 +91,7 @@ pub struct World {
main_thread_validator: MainThreadValidator,
pub(crate) change_tick: AtomicU32,
pub(crate) last_change_tick: u32,
pub(crate) last_check_tick: u32,
}

impl Default for World {
Expand All @@ -109,6 +110,7 @@ impl Default for World {
// are detected on first system runs and for direct world queries.
change_tick: AtomicU32::new(1),
last_change_tick: 0,
last_check_tick: 0,
}
}
}
Expand Down Expand Up @@ -1399,15 +1401,31 @@ impl World {
self.last_change_tick
}

/// All component change ticks are scanned for risk of age overflow once the world counter
/// has incremented at least [`CHECK_TICK_THRESHOLD`](crate::change_detection::CHECK_TICK_THRESHOLD)
/// times since the previous `check_change_ticks` scan.
///
/// During each scan, any change ticks older than [`MAX_CHANGE_AGE`](crate::change_detection::MAX_CHANGE_AGE)
/// are clamped to that age. This prevents false positives that would appear because of overflow.
// TODO: parallelize
pub fn check_change_ticks(&mut self) {
// Iterate over all component change ticks, clamping their age to max age
// PERF: parallelize
let last_check_tick = self.last_check_tick;
let change_tick = self.change_tick();
self.storages.tables.check_change_ticks(change_tick);
self.storages.sparse_sets.check_change_ticks(change_tick);
let resource_archetype = self.archetypes.resource_mut();
for column in resource_archetype.unique_components.values_mut() {
column.check_change_ticks(change_tick);
if change_tick.wrapping_sub(last_check_tick) >= CHECK_TICK_THRESHOLD {
#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!("check component ticks").entered();
self.storages.tables.check_change_ticks(change_tick);
self.storages.sparse_sets.check_change_ticks(change_tick);
let resource_archetype = self.archetypes.resource_mut();
for column in resource_archetype.unique_components.values_mut() {
column.check_change_ticks(change_tick);
}

if let Some(mut systems) = self.get_resource_mut::<crate::schedule_v3::Systems>() {
systems.check_change_ticks(change_tick, last_check_tick);
}

self.last_check_tick = change_tick;
}
}

Expand Down

0 comments on commit f5f80cd

Please sign in to comment.