Skip to content

enhance(state): only re-evaluate subscriptions that read a changed path - #13213

Closed
yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:perf/store-changed-paths
Closed

yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:perf/store-changed-paths

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

What changes

A write to the app state store re-evaluates only the subscriptions that read the changed path instead of every mounted subscription.

Mechanism

Depends on the rfx change "Recompute only the subscriptions that read a changed app-db path". deps.edn points at that commit on the fork; it will be repointed to upstream rfx once that change merges.

state/set-state! and state/update-state! know which app-db path they change, and frontend.state registers every state key k as a subscription [k & path] with the single signal [::state-key k] whose function is (get-in value path). frontend.rfx now tells the store both facts: replace-state-paths! binds store/*changed-paths* around next-state!, and sub-key maps [k & path] to [k (first path)] and [::state-key k] to [k nil] (any other subscription is left to its declared signals). Writes without a path (swap-state!, rfx event :db effects) fall back to the store's two level comparison of the previous and next db.

Concretely, typing rewrites [:editor/content uuid] and only the popups subscribed to that path are re-evaluated; moving the editing cursor rewrites :editor/editing? and only the 2 blocks whose [:editor/editing? [container-id uuid]] entry changed are re-evaluated, not every mounted block.

Numbers

On a 204 KB page of a 2,000 page graph, Chromium 148 renderer driven over the DevTools protocol, requestAnimationFrame after each key, p50 / p90 / max: moving the editing cursor between blocks with ArrowDown / ArrowUp, 44.4 / 50.2 / 76.5 ms before the full set of changes and 20.8 / 31.2 / 47 ms after; a click into a block painted after 160 ms (max 240) before and 48 ms (max 64) after; tasks over 50 ms during 40 block moves: 38 before, 0 after. In a CPU profile of typing before this change, the store sweep triggered by the window keydown handler cost about 15 ms per key and the :editor/content write about 12 ms per key; after it, next-state! no longer appears above 1 % of the profile.

Tests

The existing unit suite on the 4 branches combined, compiled with pnpm cljs:test and run with the grouping of bb dev:run-test-namespaces (213 namespaces, 2106 tests, 8650 assertions): every test passes except frontend.handler.db-based.import-test/import-from-sqlite-db-persists-import-marker-through-worker-test, whose 5 assertions fail identically on unpatched master (be800f1) whenever that namespace shares a node process with the 6 namespaces the runner groups it with, and pass when the namespace runs alone. One wall-clock assertion in frontend.worker.search-test (sync-search-indice of 300 blocks under 1000 ms with semantic search disabled) measured 1194 ms in one run and passed on reruns; the worker code is untouched by these branches. With one node process per namespace the suite is fully green. The store behaviour itself is tested in the rfx change.

Allow edits from maintainers.


Devin Review

@CLAassistant

CLAassistant commented Sep 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@tiensonqin

Copy link
Copy Markdown
Contributor

@yuxi-liu-wired Please sign the CLA so that we can review and merge those PRs, thanks!

@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/store-changed-paths branch 6 times, most recently from 9b4b9ee to 084555e Compare September 19, 2026 18:24
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Merged latest master (no conflicts) and simplified:

  • :editor/last-key-code was write-only — its only reader (get-last-key-code) was removed in enhance(editor): stop re-rendering the editor box on every keystroke #13211 — so instead of moving it to a private atom, the atom, set-last-key-code!, and its writes in the keyup handler / edit-block / comments are gone entirely.
  • set-state!'s :changed-paths option had no callers; replace-state! already reports the written path, so the option is removed.
  • The four remaining keys moved to plain atoms (:editor/last-input-time, :editor/last-saved-cursor, :ui/global-last-key-code) are unchanged.

bb dev:lint-and-test is green locally (lint 0 errors/warnings; full unit suite, 0 failures). deps.edn still points at the rfx fork until logseq/rfx#1 lands.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/store-changed-paths branch from 42f0707 to 084555e Compare September 21, 2026 23:30
yuxi-liu-wired and others added 4 commits September 24, 2026 11:47
…store

Every keystroke wrote four values through state/set-state!: the block
text, the last input time, the last key code, and the saved cursor
position. Each write makes the state store re-evaluate every mounted
subscription, so on a page with many blocks a keystroke ran that sweep
four times.

The last input time, the last key code, the saved cursor, and the UI's
last key code are read imperatively by the editor handlers through
their getters; no component subscribes to them. Keep them in plain
atoms, remove their keys from the store's initial state and from the
places that reset them, and drop the store save/restore of the saved
cursor from state_test. A keystroke now writes the store once, for the
text.

Measured on a 2,000-page graph, typing into a block on a 204 KB page
(key press to next frame, p50 / p90 / max, 83 keys):
before 44.7 / 50.7 / 66.3 ms, after this change 34.9 / 49.5 / 88.3 ms.
:editor/last-key-code was only ever written, never read (the store key
had no getter; the keyup handler stored a map that nothing consumed).
Keeping it as a private atom preserves dead bookkeeping, so remove the
atom, the setter, and its call sites entirely.
state/set-state! and state/update-state! know which app-db path they
change, and frontend.state registers every state key k as a subscription
[k & path] reading (get-in (get db k) path). Tell the rfx store both
facts: bind store/*changed-paths* around next-state! and install a
sub-key fn that maps [k & path] to [k (first path)] and [::state-key k]
to [k nil]. The store then evicts and recomputes only the subscriptions
reading those paths on each write instead of every mounted subscription.
Writes without a path (swap-state!, event :db effects) are handled by the
store's two-level comparison of the previous and next db.

Depends on the rfx change "Recompute only the subscriptions that read a
changed app-db path"; deps.edn points at that commit on the fork and will
be repointed to upstream rfx once that change merges.

Measured on a 2,000-page graph, on a 204 KB page: moving the editing
cursor between blocks with ArrowDown/ArrowUp (key press to next frame,
p50 / p90 / max) 44.4 / 50.2 / 76.5 ms before, 20.8 / 31.2 / 47 ms after
the full set of changes; a click into a block paints after 48 ms instead
of 160 ms; long tasks over 50 ms during 40 block moves: 38 before, 0
after.
set-state! took a :changed-paths option that no caller passes;
replace-state! already reports the written path to the store.
@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/store-changed-paths branch from 084555e to 0145d74 Compare September 24, 2026 20:39
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Rebased onto current master, on top of #13210 including its last-key-code removal. Added the other simplification suggested above: set-state! no longer takes the :changed-paths option, which no caller passed. deps.edn still points at the rfx fork until logseq/rfx#1 lands.

@devin-ai-integration devin-ai-integration Bot left a comment

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.

🔍 Devin Review: 2 flags

Not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

The red "Test outliner-rtc" on this PR (run 36056373275) is the delete-test flake, not this change. The same failure, outliner_basic_test.clj:139 "b1" against "b2" and line 140 2 blocks against 1, hit devin/1790409335-import-report-ux in run 36228900562. Between 2026-09-17 and 2026-09-26, 16 CI runs failed in delete-test: 2 on master, 1 on this PR, 13 on 11 other branches, in this shape, as line 138 "b1" against nil, or as a textarea wait timeout.

Rerun locally, the web build with the CI e2e defines, this branch beside its upstream base d05c62542, alternating:

  • delete-test alone: 10 of 10 pass on each.
  • the whole outliner-basic-test namespace: 11 of 11 pass on this branch, 9 of 11 on upstream (1 delete-test failure with this exact shape, 1 delete-end-test failure).

What makes it flaky, from a probe that repeats the test's steps 180 times per build: after the delete, b3's editor appears 38 to 140 ms later, and the test presses Shift+ArrowUp without waiting for it. When the editor arrives after the first press, that press is lost, which gives the line 139/140 shape (3 iterations on this branch, 6 on upstream). The second shape is exit-edit pressing Escape again while the editor is still on its way out, which drops the selection (5 iterations on each build).

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13547 together with #13210 and #13479: they share files, so they now merge once. The commits are the same, on current master.

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