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
Update docs of set_if_neq
and replace_if_neq
#12919
Conversation
In one of my personal projects I stumbled across a case where I wanted to be cautious with the change detection while also not sacraficing any performance. My use case: I basically have a horizontal surface which is saved as a collection of `Vec<Vec2>` in a component. The component also includes a height field. Now when the height is updated there only have two (three) options: - The collection of points can be cloned so the existing `set_if_neq` is usable - The `set_if_neq` code can be inlined and adjusted for this special case - (The component can be split up into two more granular components) I have to acknowledge that the third point here is probably the way to go in most cases, but I was curious: - if it would be possible to have a more flexible API and how that would look like - if you are interested in this change in general No hard feelings if we just end up rejecting this PR. I'm also totally fine with this. But maybe it's useful to anyone and I just wanted to hear your feedback first before tossing this back into the void and forgetting about it.
Note that there's another alternative which is also very similar to the approach proposed in the PR. You can use In fact I'm pretty sure (haven't actually tried though) that you can implement |
Thanks for the hint, I didn't know that! Then maybe that should be the way to go and there's no real need for |
set_if_neq
set_if_neq
and replace_if_neq
@SkiFire13 can I get your review here? |
Thank you both: this is a helpful note. |
Objective
This PR adds more flexible versions ofset_if_neq
andreplace_if_neq
to only compare and update certain fields of a components which is not just a newtypeset_if_neq
andreplace_if_neq
#12919 (comment) gave a good solution to the original problem, so let's update the docs so that this is easier to findSolution
Addset_if_neq_with
andreplace_if_neq_with
which take an accessor closure to access the relevant fieldIn 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:set_if_neq
method.set_if_neq
code specifically for this scenario.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
orQuat
with 3 or 4f32
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:
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.