`use_memo` does not properly diff inputs and drops updates
Problem
Currently, when using use_memo, some updates are dropped, and some updates are identical to the previous one.
Steps To Reproduce
See https://github.com/DioxusLabs/dioxus/pull/2415 for reproduction test case and expected result.
I suspect this could be related to #2415 and #2370.
Questionnaire
- [x] I'm interested in fixing this myself but don't know where to start
- [ ] I would like to fix and I have a solution
- [ ] I don't have time to fix this right now, but maybe later
Here is a smaller reproduction of the issue:
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
launch(app)
}
fn app() -> Element {
let mut count = use_signal(|| 0);
let memorized = use_memo(move || count());
let mut effect_run_count = use_hook(|| CopyValue::new(0));
// This effect should only run once, because the memo only ever reads as 1
use_effect(move || {
// trigger a sync update on memo. This will mark the effect as dirty
if *count.peek() == 0 {
count += 1
}
println!("{memorized}");
effect_run_count += 1;
assert!(effect_run_count() <= 1, "This effect should only run once");
});
rsx! { "hello world" }
}
Memo<T> is recomputing the value when you read the memo inside the effect. Recomputing the value sets the signal with the memorized value which causes all reactive closures that read the memo to rerun, including the effect that it recomputed the value inside.
We can fix this issue by unsubscribing to all signals before rerunning reactive scopes (use_memo, use_effect, use_resource, components). With that fix, the effect should not be subscribed to the memo until the effect is finished running which avoids the duplicate run (https://github.com/DioxusLabs/dioxus/issues/2158)
Memo<T>is recomputing the value when you read the memo inside the effect.
Why is recomputing the value necessary at that point?
Memo<T>is recomputing the value when you read the memo inside the effect.Why is recomputing the value necessary at that point?
Memos use a push/pull based system to try to maintain consistent. They don't recompute until one of these two conditions is met:
- The value is read
- All other rendering is done
let mut count = use_signal(|| 0);
// memorized will run once to get it's initial value
let memorized = use_memo(move || count() * 2);
// memorized is marked as dirty, but doesn't rerun - Lazily reruning lets us skip calculating the memo value when count = 1
count += 1;
// memorized is marked as dirty, but doesn't rerun
count += 1;
// memorized is read so it is rerun - this means we can never observe an out of date value
println!("{memorized}");
// memorized is marked as dirty, but doesn't rerun
count += 1;
// Once the component (and any parent components are done rendering, memorized is recomputed to see if any reactive closures/components need to be rerun)
Interesting, thank you for elaborating.
What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?
Interesting, thank you for elaborating.
What happens if multiple observers read from the memo? I assume the value is recalculated on the first read, and the dirty flag is removed. The second read then gets the value without recalculating?
Yes, exactly. It will rerun at most once for every write to a signal that is subscribes to