Repository navigation
Conversation
seb-odoo
left a comment
There was a problem hiding this comment.
Some notes on the choices made in this PR, to answer the questions before they come.
Disclaimer: these notes are AI-assisted, I read each one before sending it. As usual, feel free to challenge any of them.
| markDownstream(ctx); | ||
| // An immediate effect would pull this computed in this write anyway: recompute it before | ||
| // marking its observers, so that an equal value notifies no one. | ||
| if (ctx.immediateObserverCount && readsOnlySignals(ctx)) { |
There was a problem hiding this comment.
Not needed for correctness, since the immediate effects wait for the end of the loop. It avoids running the getter twice when the computed reads the downstream of another computed recomputed later in the same loop.
There was a problem hiding this comment.
[Performance, Impact: 20%] Branch switches bypass the signal-only guard.
readsOnlySignals sees the last run's sources, so a computed whose branch only now reads such a downstream still runs its getter twice.
Suggestion: The result stays correct, so maybe the description should say "read only signals in its last run" rather than "never".
There was a problem hiding this comment.
Right, done in 26ecd44: the description now says "read only signals in its last run", and that a computed switching to a branch reading such a computed runs its getter twice in that write and ends with the right value. I also added a test for the case the guard exists for (a computed reading below another changing computed runs its getter once per write), which is red without readsOnlySignals.
| eagerDepth++; | ||
| for (const computation of eagerComputations) { | ||
| try { | ||
| updateComputation(computation); |
There was a problem hiding this comment.
This catch can't be reached today: the computed catches its getter and equals errors, and the nested write no longer runs the immediate effects during this loop. I kept it so that a throw can never leave eagerDepth above 0, which would stop every immediate effect for good.
| } | ||
| // A computed recomputed above notifies from inside its compute, before it holds its new value: | ||
| // the immediate effects queued during this loop run in the outermost write, after the loop. | ||
| if (immediateObservers.length && !eagerDepth) { |
There was a problem hiding this comment.
One change of behaviour, only when a computed getter writes a signal (not supported anyway): if an immediate effect throws during that write, the error now comes out of the outer write and the computed keeps its value, where before the computed held the error.
| }); | ||
| const labelAtom = (label as any)[atomSymbol] as ComputationAtom; | ||
| s.set(2); | ||
| expect(labelAtom.flags & ComputationFlags.STATE_MASK).toBe(ComputationFlags.EXECUTED); |
There was a problem hiding this comment.
Before this commit the effect behind label was not re-run either, only checked, so nothing outside shows the difference: the test reads the state of label instead, as computed.test.ts does for sources.
| expect(labels).toEqual(["positive", "negative"]); | ||
| }); | ||
|
|
||
| test("a computed is lazy again once no immediate effect reads it", () => { |
There was a problem hiding this comment.
This one passes before this commit too: it guards the counter (a double read counts once, a disposed effect is forgotten), as a wrong count would keep the computed eager for nothing.
| if (ctx.immediateObserverCount && readsOnlySignals(ctx)) { | ||
| (eagerComputations ??= []).push(ctx); | ||
| } else { | ||
| markDownstream(ctx); | ||
| } |
There was a problem hiding this comment.
[Performance, Impact: 40%] Discarded downstream computeds stay subscribed.
Skipping markDownstream also skips its pendingDisposals sweep, so a computed read only outside effects is kept until this value changes.
Suggestion: Maybe the eager path should still queue unobserved derived observers in pendingDisposals.
There was a problem hiding this comment.
Right, fixed in 0b36450: the next effect flush now walks the computeds below the recomputed computed once, so the unread ones are disposed as before, also deeper in the chain.
2358e45 to
0b36450
Compare
|
Changes since the last push:
|
| let hasError = false; | ||
| let error: unknown; | ||
| if (immediateObservers.length) { | ||
| if (eagerComputations) { |
There was a problem hiding this comment.
[Performance, Impact: 30%] Recomputing before queued immediate effects adds re-runs.
Readers of draft() !== saved() now re-run on draft.set() when an immediate effect syncs saved, though it stays false.
Suggestion: Maybe the computed could take its readers' place in the immediate queue, recomputed when the flush reaches it.
There was a problem hiding this comment.
I'd rather keep it. Before this PR the number of runs depended on the subscription order: reader before sync, reader ran twice on draft.set(), with dirty true then false, a state that never existed after the write; sync before reader, it did not run at all. With this PR it runs once, with false, in both orders. Recomputing at the readers' place in the queue gives back the parent behaviour, 0 or 2 runs with the glitch depending on the order. The extra run here always sees the final value, which I think is the better trade.
There was a problem hiding this comment.
The same holds for a plain effect reading dirty, such as a render: one run with false in the sync first order, where the parent had none.
0b36450 to
26ecd44
Compare
|
Do we still need this change after odoo/odoo#298795? |
|
No, mail does not need it anymore. Since odoo/odoo#298795, Before odoo/odoo#298795 and odoo/odoo#295572, an insert of 100 chats in the mail store ran about 29.7M immediate effect runs, and that is why I made this PR. Today the same insert, on odoo master (b8e72ee3) with this PR ported into its owl.js (same alpha.50 base), gives these owl counters:
So for mail the gain is now small, 2 to 10% of the counted work. On the pattern the PR targets, the gain is still large. 1000 immediate effects read
The cost on master comes from re-checking every immediate effect at each write. The cost of the PR is about 80 lines in the core of the reactivity (computations.ts), plus the edge cases found in review: a computed that nobody reads anymore is disposed at the next flush, and a getter can change its sources between runs. @ged-odoo I would keep it and merge it as a general protection for this pattern, rather than close it. Do you agree it is worth its cost? |
Before this commit, a write to a signal marks every observer of a computed that reads it, and each immediate effect among them checks that computed again during the write. The problem is that each check happens even when the computed keeps its value: a signal written N times under a computed read by M immediate effects costs N * M checks. In the mail store of Odoo, a depth counter written at each nested update is read through `depth() > 0` by about 1000 immediate effects. This commit recomputes such a computed right after the write, before its observers are marked, so that they are only notified when its value changes. The recompute is done when at least one immediate effect reads the computed, as that effect would usually pull the computed in the same write anyway, and when the computed read only signals in its last run, so that it does not read a computed this write did not mark yet. A computed that switches to a branch reading such a computed runs its getter twice in that write, and ends with the right value. A computed that changes notifies from inside this recompute, before it holds its new value: the immediate effects queued during the recompute run at the end of the outermost write. Each computed counts the immediate effects that read it, so the other computeds keep the lazy path. Note that the marking of the observers of a computed also queues the disposal of its readers that nothing observes anymore. This commit keeps that disposal for a recomputed computed: the next effect flush walks the computeds below that computed once, instead of at each write. With 1000 immediate effects reading `depth() > 0`, 4000 writes of depth that keep it above 0 take 122 ms before this commit, and 3 ms after.
26ecd44 to
86da380
Compare
Before this commit, a write to a signal marks every observer of a computed that reads it, and each immediate effect among them checks that computed again during the write. The problem is that each check happens even when the computed keeps its value: a signal written N times under a computed read by M immediate effects costs N * M checks. In the mail store of Odoo, a depth counter written at each nested update is read through
depth() > 0by about 1000 immediate effects.This commit recomputes such a computed right after the write, before its observers are marked, so that they are only notified when its value changes. The recompute is done when at least one immediate effect reads the computed, as that effect would usually pull the computed in the same write anyway, and when the computed read only signals in its last run, so that it does not read a computed this write did not mark yet. A computed that switches to a branch reading such a computed runs its getter twice in that write, and ends with the right value. A computed that changes notifies from inside this recompute, before it holds its new value: the immediate effects queued during the recompute run at the end of the outermost write. Each computed counts the immediate effects that read it, so the other computeds keep the lazy path.
Note that the marking of the observers of a computed also queues the disposal of its readers that nothing observes anymore. This commit keeps that disposal for a recomputed computed: the next effect flush walks the computeds below that computed once, instead of at each write.
With 1000 immediate effects reading
depth() > 0, 4000 writes of depth that keep it above 0 take 122 ms before this commit, and 3 ms after.