-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[Merged by Bors] - Split Component Ticks #6547
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
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e2eb78a
Split ComponentTicks internally
james7132 3560c20
Introduce split ticks into Column
james7132 e21916a
Split ticks in SparseSet
james7132 4f1e425
Bubble up splitting
james7132 b356548
Remove Column::ticks
james7132 1c8d242
Fix CI
james7132 62baac2
Only fetch relevant ticks for filters
james7132 e380e0d
Slim down initialize
james7132 51d10f0
Formatting
james7132 956f226
Fix CI
james7132 ea6065d
is_changed -> is_older_than
james7132 0484924
Split ChangeTrackers table ticks
james7132 79056da
Tuples to TickCells
james7132 e82ba14
Add and use Ticks::from_tick_cells
james7132 7ec3d5e
Add TickCells::read
james7132 8ec9a1a
Document Ticks
james7132 e63ca26
Cleanup filters
james7132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ use crate::{ | |
system::Resource, | ||
}; | ||
pub use bevy_ecs_macros::Component; | ||
use bevy_ptr::OwningPtr; | ||
use bevy_ptr::{OwningPtr, UnsafeCellDeref}; | ||
use std::cell::UnsafeCell; | ||
use std::{ | ||
alloc::Layout, | ||
any::{Any, TypeId}, | ||
|
@@ -517,58 +518,108 @@ impl Components { | |
} | ||
} | ||
|
||
/// Records when a component was added and when it was last mutably dereferenced (or added). | ||
/// Used to track changes in state between system runs, e.g. components being added or accessed mutably. | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct ComponentTicks { | ||
pub(crate) added: u32, | ||
pub(crate) changed: u32, | ||
pub struct Tick { | ||
pub(crate) tick: u32, | ||
} | ||
|
||
impl ComponentTicks { | ||
impl Tick { | ||
pub const fn new(tick: u32) -> Self { | ||
Self { tick } | ||
} | ||
|
||
#[inline] | ||
/// Returns `true` if the component was added after the system last ran. | ||
pub fn is_added(&self, last_change_tick: u32, change_tick: u32) -> bool { | ||
/// Returns `true` if the tick is older than the system last's run. | ||
pub fn is_older_than(&self, last_change_tick: u32, change_tick: u32) -> bool { | ||
// This works even with wraparound because the world tick (`change_tick`) is always "newer" than | ||
// `last_change_tick` and `self.added`, and we scan periodically to clamp `ComponentTicks` values | ||
// `last_change_tick` and `self.tick`, and we scan periodically to clamp `ComponentTicks` values | ||
// so they never get older than `u32::MAX` (the difference would overflow). | ||
// | ||
// The clamp here ensures determinism (since scans could differ between app runs). | ||
let ticks_since_insert = change_tick.wrapping_sub(self.added).min(MAX_CHANGE_AGE); | ||
let ticks_since_insert = change_tick.wrapping_sub(self.tick).min(MAX_CHANGE_AGE); | ||
let ticks_since_system = change_tick | ||
.wrapping_sub(last_change_tick) | ||
.min(MAX_CHANGE_AGE); | ||
|
||
ticks_since_system > ticks_since_insert | ||
} | ||
|
||
pub(crate) fn check_tick(&mut self, change_tick: u32) { | ||
let age = change_tick.wrapping_sub(self.tick); | ||
// This comparison assumes that `age` has not overflowed `u32::MAX` before, which will be true | ||
// so long as this check always runs before that can happen. | ||
if age > MAX_CHANGE_AGE { | ||
self.tick = change_tick.wrapping_sub(MAX_CHANGE_AGE); | ||
} | ||
} | ||
|
||
/// Manually sets the change tick. | ||
/// | ||
/// This is normally done automatically via the [`DerefMut`](std::ops::DerefMut) implementation | ||
/// on [`Mut<T>`](crate::change_detection::Mut), [`ResMut<T>`](crate::change_detection::ResMut), etc. | ||
/// However, components and resources that make use of interior mutability might require manual updates. | ||
/// | ||
/// # Example | ||
/// ```rust,no_run | ||
/// # use bevy_ecs::{world::World, component::ComponentTicks}; | ||
/// let world: World = unimplemented!(); | ||
/// let component_ticks: ComponentTicks = unimplemented!(); | ||
/// | ||
/// component_ticks.set_changed(world.read_change_tick()); | ||
/// ``` | ||
#[inline] | ||
pub fn set_changed(&mut self, change_tick: u32) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed, what if it is an I would prefer direct access of the field. |
||
self.tick = change_tick; | ||
} | ||
} | ||
|
||
/// Wrapper around [`Tick`]s for a single component | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct TickCells<'a> { | ||
pub added: &'a UnsafeCell<Tick>, | ||
pub changed: &'a UnsafeCell<Tick>, | ||
} | ||
|
||
impl<'a> TickCells<'a> { | ||
/// # Safety | ||
/// All cells contained within must uphold the safety invariants of [`UnsafeCellDeref::read`]. | ||
#[inline] | ||
pub(crate) unsafe fn read(&self) -> ComponentTicks { | ||
ComponentTicks { | ||
added: self.added.read(), | ||
changed: self.changed.read(), | ||
} | ||
} | ||
} | ||
|
||
/// Records when a component was added and when it was last mutably dereferenced (or added). | ||
#[derive(Copy, Clone, Debug)] | ||
pub struct ComponentTicks { | ||
pub(crate) added: Tick, | ||
pub(crate) changed: Tick, | ||
} | ||
|
||
impl ComponentTicks { | ||
#[inline] | ||
/// Returns `true` if the component was added after the system last ran. | ||
pub fn is_added(&self, last_change_tick: u32, change_tick: u32) -> bool { | ||
self.added.is_older_than(last_change_tick, change_tick) | ||
} | ||
|
||
#[inline] | ||
/// Returns `true` if the component was added or mutably dereferenced after the system last ran. | ||
pub fn is_changed(&self, last_change_tick: u32, change_tick: u32) -> bool { | ||
// This works even with wraparound because the world tick (`change_tick`) is always "newer" than | ||
// `last_change_tick` and `self.changed`, and we scan periodically to clamp `ComponentTicks` values | ||
// so they never get older than `u32::MAX` (the difference would overflow). | ||
// | ||
// The clamp here ensures determinism (since scans could differ between app runs). | ||
let ticks_since_change = change_tick.wrapping_sub(self.changed).min(MAX_CHANGE_AGE); | ||
let ticks_since_system = change_tick | ||
.wrapping_sub(last_change_tick) | ||
.min(MAX_CHANGE_AGE); | ||
|
||
ticks_since_system > ticks_since_change | ||
self.changed.is_older_than(last_change_tick, change_tick) | ||
} | ||
|
||
pub(crate) fn new(change_tick: u32) -> Self { | ||
Self { | ||
added: change_tick, | ||
changed: change_tick, | ||
added: Tick::new(change_tick), | ||
changed: Tick::new(change_tick), | ||
} | ||
} | ||
|
||
pub(crate) fn check_ticks(&mut self, change_tick: u32) { | ||
check_tick(&mut self.added, change_tick); | ||
check_tick(&mut self.changed, change_tick); | ||
} | ||
|
||
/// Manually sets the change tick. | ||
/// | ||
/// This is normally done automatically via the [`DerefMut`](std::ops::DerefMut) implementation | ||
|
@@ -585,15 +636,6 @@ impl ComponentTicks { | |
/// ``` | ||
#[inline] | ||
pub fn set_changed(&mut self, change_tick: u32) { | ||
self.changed = change_tick; | ||
} | ||
} | ||
|
||
fn check_tick(last_change_tick: &mut u32, change_tick: u32) { | ||
let age = change_tick.wrapping_sub(*last_change_tick); | ||
// This comparison assumes that `age` has not overflowed `u32::MAX` before, which will be true | ||
// so long as this check always runs before that can happen. | ||
if age > MAX_CHANGE_AGE { | ||
*last_change_tick = change_tick.wrapping_sub(MAX_CHANGE_AGE); | ||
self.changed.set_changed(change_tick); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.