-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Optimization in use_reducer #3945
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
base: master
Are you sure you want to change the base?
Conversation
|
Visit the preview URL for this PR (updated for commit 5e3dadf): https://yew-rs-api--pr3945-use-reducer-optimiza-u4xs77n3.web.app (expires Tue, 25 Nov 2025 22:37:31 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Benchmark - coreYew MasterPull Request |
Benchmark - SSRYew MasterDetails
Pull RequestDetails
|
Size ComparisonDetails
✅ None of the examples has changed their size significantly. |
| // instead of producing a new one. | ||
| let rc_was_reused = Rc::ptr_eq(&val, &next_val); | ||
|
|
||
| let should_render = !rc_was_reused && should_render_fn(&next_val, &val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use_reducer currently makes the explicit promise that "This hook will always trigger a re-render upon receiving an action". Please change the docs, as that's a breaking change. If you are reading this from the future and relying on this behaviour, use_force_update provides a hook to trigger this rerender manually.
Since use_reducer_base is internal, could you push the changes to the callers into their respective should_render_fn for clarity in use_reducer and use_reducer_eq below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, I updated the documentation accordingly.
As for moving the check into should_render_fn, is the current version along the lines of what you meant? Now the base fn is completely unchanged from what it was before, the diff is only in the should_render_fns
Description
Yew currently provides two versions of the reducer hook:
use_reducer: This hook always causes a re-render when an action is dispatched.use_reducer_eq: This hook avoids the re-render if the new state is the same as the old state. However, the state now has to implementPartialEq.I feel like there is room for a middle ground: The reducer takes an
Rcand returns anRc. Even if we don't have aPartialEqimplementation on the state, we can still know that the state must be unchanged if the reducer returns the exact sameRcback to us. This check is almost free (just a pointer comparison).This PR adds a simple check if the two
Rcs are the same, and in that case skip the re-render.Note that even for
use_reducer_eqthis check is beneficial, since thePartialEqimplementation might be more expensive than just a pointer comparison.This change might be a breaking change if someone was relying on the exact number of re-renders, but I'd assume that this is not covered in Yew's semver guarantees, right?
Checklist