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

Avoid unnecessary change detection resulting from component lens #91

Closed
bonsairobo opened this issue Jun 30, 2023 · 3 comments · Fixed by #124
Closed

Avoid unnecessary change detection resulting from component lens #91

bonsairobo opened this issue Jun 30, 2023 · 3 comments · Fixed by #124
Labels
enhancement New feature or request

Comments

@bonsairobo
Copy link

bonsairobo commented Jun 30, 2023

Right now the Lens trait's lerp method takes &mut T for T: Component, but it should only need Mut<T>.

The system invokes DerefMut so we have no chance to avoid change detection in a custom lens:

&mut target,

Specifically, I have a custom lens that acts like a discrete switch that changes from one value to another at a specific ratio. This would only require a single DerefMut, but in fact it happens every frame that the Animator is running.

@bonsairobo
Copy link
Author

bonsairobo commented Jun 30, 2023

A workaround is to create a proxy component that acts continuously (by syncing with the ratio) and then have a separate system to do the discrete switch.

@djeedai djeedai added the enhancement New feature or request label Oct 28, 2023
djeedai added a commit that referenced this issue Apr 26, 2024
Add the ability for all `Lens`es to avoid triggering change detection if
they don't make any actual change to their target (component or asset).

This change modifies the signature of `Lerp::lerp()` to take `&mut dyn
Targetable<T>` instead of `&mut T`. The `Targetable<T>` acts as a
`Mut<T>`, marking the target as changed only if actually dereferenced
mutably.

Note that we cannot use `Mut<T>` directly because we can't obtain a
`Mut<A: Asset>` without marking the asset as mutable; see
bevyengine/bevy#13104.

Fixes #91
@djeedai
Copy link
Owner

djeedai commented Apr 26, 2024

@bonsairobo better late than never ;) this should work now, by mean of Targetable<T> which is really the same as Mut<T>, being passed to Lens::lerp().

@bonsairobo
Copy link
Author

@djeedai Thanks! I'll give it a try when I jump back into my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants