Skip to content
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

Track source location in change detection #14034

Merged
merged 26 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1c1af8f
Track callers for change detection
aevyrie Jun 26, 2024
aebbc27
use Location instead of String
aevyrie Jun 26, 2024
a5cbbd4
feat: create `change_detection_source` feature
BD103 Jul 27, 2024
7219b9c
refactor: use imports instead of full qualified path
BD103 Jul 27, 2024
60497cb
feat: store `Location` behind a `&'static`
BD103 Jul 27, 2024
c0b21e1
feat: feature flag change detection tracking
BD103 Jul 27, 2024
34e5601
fix: replaced components not being dropped
BD103 Jul 27, 2024
0dba2c7
chore: add `track_change_detection` feature to `bevy_internal` and `b…
BD103 Jul 27, 2024
2751878
fix: change detection example
BD103 Jul 27, 2024
3965c7c
Merge pull request #2 from BD103/changed-by
aevyrie Jul 27, 2024
f244435
Add change detection feature to documentation
aevyrie Jul 27, 2024
6093da0
Formatting
aevyrie Jul 27, 2024
e0a4ce1
Fix locations not updating in component mutation.
aevyrie Jul 27, 2024
9ef68fd
rename `caller` to `changed_by`
aevyrie Jul 28, 2024
ff2cf5f
Update change detection example to teach new features.
aevyrie Jul 28, 2024
6fff6e6
Merge branch 'main' into changed-by
aevyrie Jul 28, 2024
a5ef39b
Fix spelling
aevyrie Jul 28, 2024
c2bcdfd
Fix upstream compile error
aevyrie Jul 28, 2024
64cc652
Remove unused.
aevyrie Jul 28, 2024
83a96f6
Fix API gaps
aevyrie Jul 28, 2024
a397fb3
Expand example to include resources, and rename.
aevyrie Jul 28, 2024
7214af8
Revert image fix
aevyrie Jul 28, 2024
861425a
Update example readme
aevyrie Jul 28, 2024
88666cd
Update examples/ecs/change_detection.rs
aevyrie Jul 28, 2024
eafb34b
Merge branch 'main' into changed-by
aevyrie Jul 28, 2024
898f973
Update examples/ecs/change_detection.rs
aevyrie Jul 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ wasm = false
name = "component_change_detection"
path = "examples/ecs/component_change_detection.rs"
doc-scrape-examples = true
required-features = ["track_change_detection"]

[package.metadata.example.component_change_detection]
name = "Component Change Detection"
Expand Down
78 changes: 39 additions & 39 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ macro_rules! change_detection_impl {
#[inline]
#[cfg(feature = "track_change_detection")]
fn changed_by(&self) -> &'static Location<'static> {
self.caller
self.changed_by
}
}

Expand Down Expand Up @@ -326,7 +326,7 @@ macro_rules! change_detection_mut_impl {
*self.ticks.changed = self.ticks.this_run;
#[cfg(feature = "track_change_detection")]
{
*self.caller = Location::caller();
*self.changed_by = Location::caller();
}
}

Expand All @@ -336,7 +336,7 @@ macro_rules! change_detection_mut_impl {
*self.ticks.changed = last_changed;
#[cfg(feature = "track_change_detection")]
{
*self.caller = Location::caller();
*self.changed_by = Location::caller();
}
}

Expand All @@ -353,7 +353,7 @@ macro_rules! change_detection_mut_impl {
self.set_changed();
#[cfg(feature = "track_change_detection")]
{
*self.caller = Location::caller();
*self.changed_by = Location::caller();
}
self.value
}
Expand Down Expand Up @@ -393,7 +393,7 @@ macro_rules! impl_methods {
this_run: self.ticks.this_run,
},
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}

Expand Down Expand Up @@ -424,7 +424,7 @@ macro_rules! impl_methods {
value: f(self.value),
ticks: self.ticks,
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}

Expand Down Expand Up @@ -536,7 +536,7 @@ pub struct Res<'w, T: ?Sized + Resource> {
pub(crate) value: &'w T,
pub(crate) ticks: Ticks<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'static Location<'static>,
pub(crate) changed_by: &'static Location<'static>,
}

impl<'w, T: Resource> Res<'w, T> {
Expand All @@ -550,7 +550,7 @@ impl<'w, T: Resource> Res<'w, T> {
value: this.value,
ticks: this.ticks.clone(),
#[cfg(feature = "track_change_detection")]
caller: this.caller,
changed_by: this.changed_by,
}
}

Expand All @@ -568,7 +568,7 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Res<'w, T> {
value: res.value,
ticks: res.ticks.into(),
#[cfg(feature = "track_change_detection")]
caller: res.caller,
changed_by: res.changed_by,
}
}
}
Expand Down Expand Up @@ -602,7 +602,7 @@ pub struct ResMut<'w, T: ?Sized + Resource> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'w mut &'static Location<'static>,
pub(crate) changed_by: &'w mut &'static Location<'static>,
}

impl<'w, 'a, T: Resource> IntoIterator for &'a ResMut<'w, T>
Expand Down Expand Up @@ -643,7 +643,7 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Mut<'w, T> {
value: other.value,
ticks: other.ticks,
#[cfg(feature = "track_change_detection")]
caller: other.caller,
changed_by: other.changed_by,
}
}
}
Expand All @@ -664,7 +664,7 @@ pub struct NonSendMut<'w, T: ?Sized + 'static> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'w mut &'static Location<'static>,
pub(crate) changed_by: &'w mut &'static Location<'static>,
}

change_detection_impl!(NonSendMut<'w, T>, T,);
Expand All @@ -680,7 +680,7 @@ impl<'w, T: 'static> From<NonSendMut<'w, T>> for Mut<'w, T> {
value: other.value,
ticks: other.ticks,
#[cfg(feature = "track_change_detection")]
caller: other.caller,
changed_by: other.changed_by,
}
}
}
Expand Down Expand Up @@ -713,7 +713,7 @@ pub struct Ref<'w, T: ?Sized> {
pub(crate) value: &'w T,
pub(crate) ticks: Ticks<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'static Location<'static>,
pub(crate) changed_by: &'static Location<'static>,
}

impl<'w, T: ?Sized> Ref<'w, T> {
Expand All @@ -731,7 +731,7 @@ impl<'w, T: ?Sized> Ref<'w, T> {
value: f(self.value),
ticks: self.ticks,
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}

Expand Down Expand Up @@ -763,7 +763,7 @@ impl<'w, T: ?Sized> Ref<'w, T> {
this_run,
},
#[cfg(feature = "track_change_detection")]
caller,
changed_by: caller,
}
}
}
Expand Down Expand Up @@ -846,7 +846,7 @@ pub struct Mut<'w, T: ?Sized> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'w mut &'static Location<'static>,
pub(crate) changed_by: &'w mut &'static Location<'static>,
}

impl<'w, T: ?Sized> Mut<'w, T> {
Expand Down Expand Up @@ -882,7 +882,7 @@ impl<'w, T: ?Sized> Mut<'w, T> {
this_run,
},
#[cfg(feature = "track_change_detection")]
caller,
changed_by: caller,
}
}
}
Expand All @@ -893,7 +893,7 @@ impl<'w, T: ?Sized> From<Mut<'w, T>> for Ref<'w, T> {
value: mut_ref.value,
ticks: mut_ref.ticks.into(),
#[cfg(feature = "track_change_detection")]
caller: mut_ref.caller,
changed_by: mut_ref.changed_by,
}
}
}
Expand Down Expand Up @@ -940,7 +940,7 @@ pub struct MutUntyped<'w> {
pub(crate) value: PtrMut<'w>,
pub(crate) ticks: TicksMut<'w>,
#[cfg(feature = "track_change_detection")]
pub(crate) caller: &'w mut &'static core::panic::Location<'static>,
pub(crate) changed_by: &'w mut &'static core::panic::Location<'static>,
}

impl<'w> MutUntyped<'w> {
Expand All @@ -966,7 +966,7 @@ impl<'w> MutUntyped<'w> {
this_run: self.ticks.this_run,
},
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}

Expand Down Expand Up @@ -1018,7 +1018,7 @@ impl<'w> MutUntyped<'w> {
value: f(self.value),
ticks: self.ticks,
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}

Expand All @@ -1033,7 +1033,7 @@ impl<'w> MutUntyped<'w> {
ticks: self.ticks,
// SAFETY: `caller` is `Aligned`.
#[cfg(feature = "track_change_detection")]
caller: self.caller,
changed_by: self.changed_by,
}
}
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ impl<'w> DetectChanges for MutUntyped<'w> {
#[inline]
#[cfg(feature = "track_change_detection")]
fn changed_by(&self) -> &'static Location<'static> {
self.caller
self.changed_by
}
}

Expand All @@ -1074,7 +1074,7 @@ impl<'w> DetectChangesMut for MutUntyped<'w> {
*self.ticks.changed = self.ticks.this_run;
#[cfg(feature = "track_change_detection")]
{
*self.caller = Location::caller();
*self.changed_by = Location::caller();
}
}

Expand All @@ -1084,7 +1084,7 @@ impl<'w> DetectChangesMut for MutUntyped<'w> {
*self.ticks.changed = last_changed;
#[cfg(feature = "track_change_detection")]
{
*self.caller = Location::caller();
*self.changed_by = Location::caller();
}
}

Expand All @@ -1109,7 +1109,7 @@ impl<'w, T> From<Mut<'w, T>> for MutUntyped<'w> {
value: value.value.into(),
ticks: value.ticks,
#[cfg(feature = "track_change_detection")]
caller: value.caller,
changed_by: value.changed_by,
}
}
}
Expand Down Expand Up @@ -1304,13 +1304,13 @@ mod tests {
};
let mut res = R {};
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let res_mut = ResMut {
value: &mut res,
ticks,
#[cfg(feature = "track_change_detection")]
caller,
changed_by: &mut caller,
};

let into_mut: Mut<R> = res_mut.into();
Expand All @@ -1328,7 +1328,7 @@ mod tests {
};
let mut res = R {};
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let val = Mut::new(
&mut res,
Expand All @@ -1337,7 +1337,7 @@ mod tests {
Tick::new(2), // last_run
Tick::new(4), // this_run
#[cfg(feature = "track_change_detection")]
caller,
&mut caller,
);

assert!(!val.is_added());
Expand All @@ -1358,13 +1358,13 @@ mod tests {
};
let mut res = R {};
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let non_send_mut = NonSendMut {
value: &mut res,
ticks,
#[cfg(feature = "track_change_detection")]
caller,
changed_by: &mut caller,
};

let into_mut: Mut<R> = non_send_mut.into();
Expand Down Expand Up @@ -1394,13 +1394,13 @@ mod tests {

let mut outer = Outer(0);
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let ptr = Mut {
value: &mut outer,
ticks,
#[cfg(feature = "track_change_detection")]
caller,
changed_by: &mut caller,
};
assert!(!ptr.is_changed());

Expand Down Expand Up @@ -1484,13 +1484,13 @@ mod tests {

let mut value: i32 = 5;
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let value = MutUntyped {
value: PtrMut::from(&mut value),
ticks,
#[cfg(feature = "track_change_detection")]
caller,
changed_by: &mut caller,
};

let reflect_from_ptr = <ReflectFromPtr as FromType<i32>>::from_type();
Expand Down Expand Up @@ -1522,13 +1522,13 @@ mod tests {
};
let mut c = C {};
#[cfg(feature = "track_change_detection")]
let caller = Location::caller();
let mut caller = Location::caller();

let mut_typed = Mut {
value: &mut c,
ticks,
#[cfg(feature = "track_change_detection")]
caller,
changed_by: &mut caller,
};

let into_mut: MutUntyped = mut_typed.into();
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
last_run: fetch.last_run,
},
#[cfg(feature = "track_change_detection")]
caller: caller.deref(),
changed_by: caller.deref(),
}
}
StorageType::SparseSet => {
Expand All @@ -1172,7 +1172,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
value: component.deref(),
ticks: Ticks::from_tick_cells(ticks, fetch.last_run, fetch.this_run),
#[cfg(feature = "track_change_detection")]
caller: _caller.deref(),
changed_by: _caller.deref(),
}
}
}
Expand Down Expand Up @@ -1349,7 +1349,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
last_run: fetch.last_run,
},
#[cfg(feature = "track_change_detection")]
caller: caller.deref_mut(),
changed_by: caller.deref_mut(),
}
}
StorageType::SparseSet => {
Expand All @@ -1367,7 +1367,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
value: component.assert_unique().deref_mut(),
ticks: TicksMut::from_tick_cells(ticks, fetch.last_run, fetch.this_run),
#[cfg(feature = "track_change_detection")]
caller: _caller.deref_mut(),
changed_by: _caller.deref_mut(),
}
}
}
Expand Down
Loading