Skip to content

Commit e7ab656

Browse files
authored
Update docs of set_if_neq and replace_if_neq (#12919)
# Objective - ~~This PR adds more flexible versions of `set_if_neq` and `replace_if_neq` to only compare and update certain fields of a components which is not just a newtype~~ - #12919 (comment) gave a good solution to the original problem, so let's update the docs so that this is easier to find ## Solution - ~~Add `set_if_neq_with` and `replace_if_neq_with` which take an accessor closure to access the relevant field~~ --- In a recent project, a scenario emerged that required careful consideration regarding change detection without compromising performance. The context involves a component that maintains a collection of `Vec<Vec2>` representing a horizontal surface, alongside a height field. When the height is updated, there are a few approaches to consider: 1. Clone the collection of points to utilize the existing `set_if_neq` method. 2. Inline and adjust the `set_if_neq` code specifically for this scenario. 3. (Consider splitting the component into more granular components.) It's worth noting that the third option might be the most suitable in most cases. A similar situation arises with the Bevy internal Transform component, which includes fields for translation, rotation, and scale. These fields are relatively small (`Vec3` or `Quat` with 3 or 4 `f32` values), but the creation of a single pointer (`usize`) might be more efficient than copying the data of the other fields. This is speculative, and insights from others could be valuable. Questions remain: - Is it feasible to develop a more flexible API, and what might that entail? - Is there general interest in this change? There's no hard feelings if this idea or the PR is ultimately rejected. I just wanted to put this idea out there and hope that this might be beneficial to others and that feedback could be valuable before abandoning the idea.
1 parent 409e409 commit e7ab656

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

crates/bevy_ecs/src/change_detection.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ pub trait DetectChangesMut: DetectChanges {
131131
/// This is useful to ensure change detection is only triggered when the underlying value
132132
/// changes, instead of every time it is mutably accessed.
133133
///
134+
/// If you're dealing with non-trivial structs which have multiple fields of non-trivial size,
135+
/// then consider applying a `map_unchanged` beforehand to allow changing only the relevant
136+
/// field and prevent unnecessary copying and cloning.
137+
/// See the docs of [`Mut::map_unchanged`], [`MutUntyped::map_unchanged`],
138+
/// [`ResMut::map_unchanged`] or [`NonSendMut::map_unchanged`] for an example
139+
///
134140
/// If you need the previous value, use [`replace_if_neq`](DetectChangesMut::replace_if_neq).
135141
///
136142
/// # Examples
@@ -181,6 +187,12 @@ pub trait DetectChangesMut: DetectChanges {
181187
/// This is useful to ensure change detection is only triggered when the underlying value
182188
/// changes, instead of every time it is mutably accessed.
183189
///
190+
/// If you're dealing with non-trivial structs which have multiple fields of non-trivial size,
191+
/// then consider applying a [`map_unchanged`](Mut::map_unchanged) beforehand to allow
192+
/// changing only the relevant field and prevent unnecessary copying and cloning.
193+
/// See the docs of [`Mut::map_unchanged`], [`MutUntyped::map_unchanged`],
194+
/// [`ResMut::map_unchanged`] or [`NonSendMut::map_unchanged`] for an example
195+
///
184196
/// If you don't need the previous value, use [`set_if_neq`](DetectChangesMut::set_if_neq).
185197
///
186198
/// # Examples

0 commit comments

Comments
 (0)