Skip to content

[PERF] owl-core: skip immediate effects behind an unchanged computed - #2054

Open
seb-odoo wants to merge 1 commit into
odoo:masterfrom
seb-odoo:master-immediate-unchanged-computed--seb
Open

seb-odoo wants to merge 1 commit into
odoo:masterfrom
seb-odoo:master-immediate-unchanged-computed--seb

Conversation

@seb-odoo

@seb-odoo seb-odoo commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

@seb-odoo seb-odoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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", () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +143 to +147
if (ctx.immediateObserverCount && readsOnlySignals(ctx)) {
(eagerComputations ??= []).push(ctx);
} else {
markDownstream(ctx);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@seb-odoo
seb-odoo force-pushed the master-immediate-unchanged-computed--seb branch from 2358e45 to 0b36450 Compare November 3, 2026 14:09
@seb-odoo

seb-odoo commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Changes since the last push:

  • A computed that nothing reads anymore, behind an unchanged computed read by an immediate effect, is disposed again at the next effect flush, as @alexkuhn noticed here. The flush walks the computeds below it once, not at each write: that would loop again over all the observers of the computed, which is the cost this PR removes.

let hasError = false;
let error: unknown;
if (immediateObservers.length) {
if (eagerComputations) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@seb-odoo
seb-odoo force-pushed the master-immediate-unchanged-computed--seb branch from 0b36450 to 26ecd44 Compare November 5, 2026 16:20
@maki-odoo

Copy link
Copy Markdown

Do we still need this change after odoo/odoo#298795? updateDepth is a plain number now, and the store sets isUpdateInProgress itself.

@seb-odoo

Copy link
Copy Markdown
Contributor Author

No, mail does not need it anymore. Since odoo/odoo#298795, isUpdateInProgress is a signal set only when the outermost update starts and ends, and the immediate onChange read it untracked. So mail no longer has a computed read by many immediate effects.

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:

Insert of 100 chats odoo master owl.js with this PR
writes 20407 20407
observer visits 21005 18805
computation updates 36200 35400

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 computed(() => depth() > 0), then 30000 writes of depth leave the computed unchanged:

30000 unchanged writes odoo master owl.js with this PR
time (3 runs) 2.2 to 3.2 s 35 to 43 ms
effect body runs 0 0

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.
@seb-odoo
seb-odoo force-pushed the master-immediate-unchanged-computed--seb branch from 26ecd44 to 86da380 Compare November 26, 2026 22:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants