Skip to content

enhance(editor): keep per-keystroke bookkeeping out of the app state store - #13210

Closed
yuxi-liu-wired wants to merge 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/editor-state-atoms
Closed

yuxi-liu-wired wants to merge 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/editor-state-atoms

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

A keystroke in the editor writes the app state store once (the block text) instead of 5 times.

Mechanism

Every keystroke wrote :editor/content, :editor/last-input-time, :editor/last-key-code, :editor/last-saved-cursor and :ui/global-last-key-code through state/set-state!. Each write makes the rfx store re-evaluate every mounted subscription, so on a page with many blocks a keystroke paid that sweep 5 times. Nothing subscribes to the 4 bookkeeping values; they are read imperatively (state/input-idle?, state/get-editor-last-pos, state/get-ui-last-key-code). They now live in plain atoms in frontend.state, reset where clear-edit! and set-editing! reset them, with the same getters and setters.

Numbers

Typing into a block on a 204 KB page of a 2,000 page graph, key press to next frame, p50 / p90 / max over 83 keys at a 60 ms cadence, Chromium 148 renderer driven over the DevTools protocol: before 44.7 / 50.7 / 66.3 ms, after 34.9 / 49.5 / 88.3 ms. Keyup handling per key dropped from 32 ms (Event Timing keyup duration p50) to under 16 ms.

Tests

The existing unit suite, 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. With one node process per namespace the suite is fully green on this change. No new test: the change moves values between storage mechanisms with identical getters.

Link to Devin session: https://app.devin.ai/sessions/a0806935382643ff89101cd35b49ffdb
Open in Devin Desktop: https://app.devin.ai/desktop/session/a0806935382643ff89101cd35b49ffdb?variant=devin
Requested by: @tiensonqin


Devin Review

@CLAassistant

CLAassistant commented Sep 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/editor-state-atoms branch 5 times, most recently from d18fdc7 to abe8fda Compare September 19, 2026 18:24
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Rebased onto current master, still one commit. The lint pass that followed #13211 removed get-last-key-code, which nothing called, so this branch no longer defines it; set-last-key-code! and the other three atoms are as before.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Rebased the branch onto current master (054bff4518) — applies cleanly, no conflicts. Since the head branch lives on a fork, the rebased branch plus one follow-up commit is on logseq/logseq at devin/pr13210-fixes for review.

Review result: the mechanism checks out — no store subscriber or imperative get-state/set-state! reader of :editor/last-input-time, :editor/last-key-code, :editor/last-saved-cursor, or :ui/global-last-key-code remains anywhere in the tree, and all remaining consumers (input-idle?, get-editor-last-pos, get-ui-last-key-code) go through the updated getters. Resets in clear-edit!/set-editing! match the previous lifecycle.

One simplification committed on top (7083beef3e): :editor/last-key-code is write-only — it has no getter and nothing reads it (true on master as well; the lint pass after #13211 already removed the unused get-last-key-code). Moving it into a private atom just preserves dead bookkeeping, so the atom, set-last-key-code!, and its two call sites are removed instead. Happy to drop this commit if the value is intentionally kept for upcoming work.

Validation: bb lint:kondo-git-changes clean; frontend.state-test 14 tests / 40 assertions pass.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

UI verification — editor bookkeeping moved to plain atoms

Built the web app with pnpm watch (:app compiled, 0 warnings — no stale set-last-key-code! reference) and exercised the affected editor paths in Chrome at localhost:3001 (Demo graph, journal page).

Verified:

  • Block editing works normally — text updates per keystroke and persists on Escape
  • Saved-cursor bookkeeping: inserted [[Journal]] mid-block via the page-search popup; the trigger text was stripped and the ref placed exactly at the saved position → hello world alpha [[Journal]] beta gamma (exercises set-editor-last-pos! on keyup + get-editor-last-pos in the on-chosen handler)
  • Escape closes the Ctrl+K command palette and the [[ page-search popup (exercises the keydown bookkeeping feeding get-ui-last-key-code)
  • Console clean: 0 errors; only 2 unrelated pre-existing OPFS/sqlite-wasm dev-server warnings; nothing referencing last-key-code / last-saved-cursor / last-input-time / global-last-key-code

Not covered: the narrow ;;-marker property-restore branch of edit-original-block! (events/ui.cljs), the only read site of get-ui-last-key-code — the write side and both visible Escape paths were verified.

Editor keystroke and cursor bookkeeping

Mid-block page-ref insert at saved cursor

yuxi-liu-wired and others added 2 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.
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Rebased onto current master and took the follow-up commit from devin/pr13210-fixes as it is, so the write-only :editor/last-key-code is gone rather than moved. The branch is now that commit on top of the original one.

The failed "Test editor" run on the previous head stopped at the job's 8-minute limit, which #13426 has since raised to 12.

@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: 1 flag

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

Devin Review

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13547 together with #13213 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