@@ -2,7 +2,7 @@ mod entity_ref;
2
2
mod spawn_batch;
3
3
mod world_cell;
4
4
5
- pub use crate :: change_detection:: Mut ;
5
+ pub use crate :: change_detection:: { Mut , CHECK_TICK_THRESHOLD } ;
6
6
pub use entity_ref:: * ;
7
7
pub use spawn_batch:: * ;
8
8
pub use world_cell:: * ;
@@ -91,6 +91,7 @@ pub struct World {
91
91
main_thread_validator : MainThreadValidator ,
92
92
pub ( crate ) change_tick : AtomicU32 ,
93
93
pub ( crate ) last_change_tick : u32 ,
94
+ pub ( crate ) last_check_tick : u32 ,
94
95
}
95
96
96
97
impl Default for World {
@@ -109,6 +110,7 @@ impl Default for World {
109
110
// are detected on first system runs and for direct world queries.
110
111
change_tick : AtomicU32 :: new ( 1 ) ,
111
112
last_change_tick : 0 ,
113
+ last_check_tick : 0 ,
112
114
}
113
115
}
114
116
}
@@ -1399,15 +1401,31 @@ impl World {
1399
1401
self . last_change_tick
1400
1402
}
1401
1403
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
1402
1411
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 ;
1405
1413
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;
1411
1429
}
1412
1430
}
1413
1431
0 commit comments