Skip to content

enhance(editor): stop re-rendering the editor box on every keystroke - #13211

Merged
tiensonqin merged 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/editor-box-rerender
Sep 19, 2026
Merged

tiensonqin merged 2 commits into
logseq:masterfrom
yuxi-liu-wired:perf/editor-box-rerender

Conversation

@yuxi-liu-wired

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

Copy link
Copy Markdown
Contributor

What changes

The editor box no longer re-renders on every keystroke. The hidden caret mirror is built only when a caret position is needed.

Mechanism

frontend.components.editor/box subscribed to [:editor/content uuid], so each keystroke re-rendered the whole box: the autosize textarea re-measured itself, the hidden #mock-text mirror (one span per grapheme of the block) was rebuilt, and the command trigger check ran as a render effect.

Now the box reads the content from the store when it renders, without subscribing, and the textarea takes it as its default value; the editor popups keep their own :editor/content subscription, so filtering while a popup is open is unchanged. The heading class (uniline-block / multiline-block, h1 to h6) is the textarea's :class prop, computed from the block and the content, so the autosize textarea measures the heading font when it mounts; between renders the change handler writes it on each keystroke through update-heading-class!, and a render recomputes the prop from the live content, so it never writes a stale class. :cacheMeasurements is gone: without a render per keystroke it would freeze at its mount value, and the autosize fallback would keep sizing a heading block with the base font. An :editor-opts caller that supplies its own :on-change (the comments editor) replaces the block change handler, not the class update. frontend.util.cursor/build-mock-text! builds the mirror on demand, only when get-caret-pos is asked for a position and only when the text changed since the last build; it produces the same DOM as before (ids mock-text_<char index>, a newline as 0 followed by <br>), and it resolves the mirror inside the textarea's own .editor-inner, so two mounted editors (the comments UI) each keep their own. Every helper that reads the mirror takes the input and resolves the mirror through that input; none falls back to the first .mock-text in the document, because with two editors mounted the caret rect and the row test would then come from different mirrors. textarea-cursor-rect-first-row? and -last-row? read one offsetTop (first and last span) instead of one per character, and shortcut-select-up-down passes them the input. next-cursor-pos-up-down returns nil for a caret that sits on no row of the mirror, so an arrow key then does nothing instead of moving the caret to the far end of the block; select-up-down acts only on a position it received. A block of 3 lines of 21 characters, caret at offset 32 in the middle line, moves to 10 on up and to 54 on down. The command trigger check (handle-last-input) runs from edit-box-on-change!, guarded by :editor/on-paste? as before; the flag is also cleared when a fresh editor mounts, because block, embed and file pastes mount a new editor without a change event.

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 this change 34.9 / 49.5 / 88.3 ms, after 8.4 / 12.0 / 19.6 ms. Keydown handler processing per key: 11 ms before, 0.7 ms after. In a CPU profile of the same typing the React commit per key fell from about 8 ms to under 1 ms.

Tests

Three end-to-end tests are added to clj-e2e/test/logseq/e2e/editor_basic_test.clj. Two build a block of 3 identical newline-separated rows and assert through the block content, so they never read the mirror: arrow-up-down-move-the-caret-inside-a-block-test presses up, types a letter, presses down, types another, and expects the letters at the end of the middle row and of the last row; shift-arrow-up-selects-inside-a-block-test presses shift with up, types a letter, and expects the last row and the newline before it replaced. Both pass on upstream master and on this branch. On the branch as it was before the caret helpers took the input, the first test fails with the letter at the end of the last row, which is the regression the tests exist for. The third, heading-editor-shows-every-row-test, types a heading whose title wraps to several rows, leaves the block, clicks back into it, and asserts that the textarea's scrollHeight fits in its clientHeight. It passes on master and on this branch; on the revision of this branch that set the class from a layout effect it failed with clientHeight 47 and scrollHeight 136, because the autosize textarea had measured itself with the base font before the class arrived.

The same commit changes one helper of the suite, clj-e2e/src/logseq/e2e/util.clj: get-edit-content reads the editing textarea through inputValue instead of its DOM text. A textarea's child text node holds its default value; its value holds what the user typed. Upstream the two agree because the box re-renders on every keystroke and React writes the new content into the textarea's default value. With this change the box does not re-render on keystrokes, so the DOM text keeps the mount-time content and every get-edit-content assertion in the suite would read the empty string. inputValue reads the live value on both. The whole logseq.e2e.editor-basic-test namespace, built with the defines of .github/workflows/clj-e2e.yml and served locally, passes on this branch with that helper: 67 tests, 241 assertions, 0 failures.

The existing unit suite, compiled with pnpm cljs:test and run with the grouping of bb dev:run-test-namespaces (231 namespaces in 40 node processes, 2376 tests, 9656 assertions): every test passes except one in logseq.api.db-based.cli-test and one in logseq.api.db-based.tools-test, which fail only while sharing a node process with the namespaces the runner groups them with (a nil database in the second) and pass when each namespace runs alone; the same two fail identically on unpatched master in the same grouping, and neither namespace touches the editor. With one node process per namespace the suite is fully green on this change. An end to end check in a headful Electron build exercised Enter (new block), / command popup with filtering, [[ page search with results, Shift+Enter switching to the multiline class with the textarea growing without overflow, ArrowUp from row 2 staying in the block and from row 1 moving to the previous block, and save on Escape.

Allow edits from maintainers.

@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-box-rerender branch 2 times, most recently from 834f882 to bc8cd05 Compare September 18, 2026 17:27
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Pushed one more change inside this branch. next-cursor-pos-up-down still looked the caret mirror up by the id mock-text, which no element carries since the mirror became a per-editor .mock-text: up and down inside a block returned no position, so the caret stayed on its line, while crossing to the block above or below kept working through textarea-cursor-first-row? and -last-row?. It now takes the input, builds that input's mirror and reads it through mock-text-el like the other caret helpers, and select-up-down acts only on a position it received.

Checked on a build: a block of 3 lines of 21 characters with the caret at offset 32 in the middle line moves to 10 on up and to 54 on down, and typing stays at 2.3 / 3.2 / 8.8 ms (p50 / p90 / max) key press to next frame over 83 keys with no long task.

Two end to end tests covering this went into clj-e2e/test/logseq/e2e/editor_basic_test.clj as well: up then down inside a block of 3 identical rows, and shift with up selecting the last row. They assert through the block content, so they pass on upstream, pass on this branch, and fail on this branch as it was before the fix.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/editor-box-rerender branch 3 times, most recently from 26b83c7 to ef009cd Compare September 18, 2026 19:47
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Pushed one more revision inside this branch, still one commit on current master.

  • The heading class is the textarea's :class prop again, computed from the block and the content, with the change handler writing it between renders. The previous revision set it from a layout effect, which React runs after the autosize textarea's own mount-time measurement, so a heading block opened for editing was measured with the base font and clipped to that height: clientHeight 47 against scrollHeight 136 for a title wrapping to 4 rows. :cacheMeasurements is gone as well, because without a render per keystroke it froze at its mount value and kept that base-font sizing for the life of the editor. An :editor-opts caller with its own :on-change (the comments editor) now replaces only the block change handler; the class update still runs there.
  • mock-text-el no longer falls back to the first .mock-text in the document when it has no input. With two editors mounted (the comments UI) the caret rect and the row test could come from different mirrors. textarea-cursor-rect-first-row? and -last-row? now take the input, and shortcut-select-up-down passes it.
  • next-cursor-pos-up-down returns nil for a caret that sits on no row of the mirror, instead of treating that caret as its own row partition and moving to the far end of the block.
  • handle-last-input runs from the change handler without the try/catch that used to guard it inside a render effect; an error in a command trigger now reaches the console instead of being dropped.
  • Three end-to-end tests in clj-e2e/test/logseq/e2e/editor_basic_test.clj: up then down inside a block of 3 rows, shift with up, and a heading block reopened for editing whose textarea must fit its rows. The caret test fails on the branch as it was when the caret broke, with the typed letter at the end of the last row; the heading test fails on the previous revision of this branch.
  • clj-e2e/src/logseq/e2e/util.clj: get-edit-content reads the textarea through inputValue. The box no longer re-renders on keystrokes, so React no longer refreshes the textarea's default value, which is the DOM text the helper used to read, and every content assertion in the suite read the mount-time content; inputValue reads the live value on both master and this branch. The whole logseq.e2e.editor-basic-test namespace passes with it.

The editor box subscribed to :editor/content, so each keystroke
re-rendered the whole box: the autosize textarea re-measured itself, a
hidden mirror of the text (one span per grapheme) was rebuilt for caret
lookups, and the command-trigger check ran as a render effect.

Read the content from the store when the box renders; the textarea is
uncontrolled and the editor popups keep their own subscription. The
textarea's class is a prop computed from the block and the content, so
the autosize textarea measures the heading font when it mounts, and the
change handler writes the class on each keystroke; the autosize
measurement cache is gone, since without a render per keystroke it
would freeze at its mount value. Build the caret mirror on demand in
frontend.util.cursor, only when a caret position is asked for and only
when the text changed since the last build; resolve the mirror relative
to its own textarea, so two mounted editors keep separate mirrors; read
one offsetTop for the first-row and last-row checks instead of one per
character. Run the command-trigger check from the change handler, and
clear the paste flag when a fresh editor mounts, since block, embed and
file pastes fire no change event.

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 34.9 / 49.5 / 88.3 ms, after 8.4 / 12.0 / 19.6 ms. Keydown
handler time per key: 11 ms before, 0.7 ms after.
@tiensonqin
tiensonqin force-pushed the perf/editor-box-rerender branch from ef009cd to 3ca3bc5 Compare September 19, 2026 07:11
@tiensonqin

Copy link
Copy Markdown
Contributor

@cursoragent review

@tiensonqin tiensonqin 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.

Awesome fix, thanks for the contribution! 🚢 👍

@tiensonqin
tiensonqin merged commit 88d707d into logseq:master Sep 19, 2026
9 of 10 checks passed
tiensonqin added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 21, 2026
…ption

- :editor/last-key-code was write-only (its only reader was removed in
  logseq#13211); remove the atom, setter, and writes instead of moving it
- set-state!'s :changed-paths option had no callers; replace-state!
  already reports the written path
@yuxi-liu-wired
yuxi-liu-wired deleted the perf/editor-box-rerender branch September 30, 2026 20:02
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
…13211)

* enhance(editor): stop re-rendering the editor box on every keystroke

The editor box subscribed to :editor/content, so each keystroke
re-rendered the whole box: the autosize textarea re-measured itself, a
hidden mirror of the text (one span per grapheme) was rebuilt for caret
lookups, and the command-trigger check ran as a render effect.

Read the content from the store when the box renders; the textarea is
uncontrolled and the editor popups keep their own subscription. The
textarea's class is a prop computed from the block and the content, so
the autosize textarea measures the heading font when it mounts, and the
change handler writes the class on each keystroke; the autosize
measurement cache is gone, since without a render per keystroke it
would freeze at its mount value. Build the caret mirror on demand in
frontend.util.cursor, only when a caret position is asked for and only
when the text changed since the last build; resolve the mirror relative
to its own textarea, so two mounted editors keep separate mirrors; read
one offsetTop for the first-row and last-row checks instead of one per
character. Run the command-trigger check from the change handler, and
clear the paste flag when a fresh editor mounts, since block, embed and
file pastes fire no change event.

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 34.9 / 49.5 / 88.3 ms, after 8.4 / 12.0 / 19.6 ms. Keydown
handler time per key: 11 ms before, 0.7 ms after.

* fix: lint

---------

Co-authored-by: Tienson Qin <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
…13211)

* enhance(editor): stop re-rendering the editor box on every keystroke

The editor box subscribed to :editor/content, so each keystroke
re-rendered the whole box: the autosize textarea re-measured itself, a
hidden mirror of the text (one span per grapheme) was rebuilt for caret
lookups, and the command-trigger check ran as a render effect.

Read the content from the store when the box renders; the textarea is
uncontrolled and the editor popups keep their own subscription. The
textarea's class is a prop computed from the block and the content, so
the autosize textarea measures the heading font when it mounts, and the
change handler writes the class on each keystroke; the autosize
measurement cache is gone, since without a render per keystroke it
would freeze at its mount value. Build the caret mirror on demand in
frontend.util.cursor, only when a caret position is asked for and only
when the text changed since the last build; resolve the mirror relative
to its own textarea, so two mounted editors keep separate mirrors; read
one offsetTop for the first-row and last-row checks instead of one per
character. Run the command-trigger check from the change handler, and
clear the paste flag when a fresh editor mounts, since block, embed and
file pastes fire no change event.

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 34.9 / 49.5 / 88.3 ms, after 8.4 / 12.0 / 19.6 ms. Keydown
handler time per key: 11 ms before, 0.7 ms after.

* fix: lint

---------

Co-authored-by: Tienson Qin <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
…13211)

* enhance(editor): stop re-rendering the editor box on every keystroke

The editor box subscribed to :editor/content, so each keystroke
re-rendered the whole box: the autosize textarea re-measured itself, a
hidden mirror of the text (one span per grapheme) was rebuilt for caret
lookups, and the command-trigger check ran as a render effect.

Read the content from the store when the box renders; the textarea is
uncontrolled and the editor popups keep their own subscription. The
textarea's class is a prop computed from the block and the content, so
the autosize textarea measures the heading font when it mounts, and the
change handler writes the class on each keystroke; the autosize
measurement cache is gone, since without a render per keystroke it
would freeze at its mount value. Build the caret mirror on demand in
frontend.util.cursor, only when a caret position is asked for and only
when the text changed since the last build; resolve the mirror relative
to its own textarea, so two mounted editors keep separate mirrors; read
one offsetTop for the first-row and last-row checks instead of one per
character. Run the command-trigger check from the change handler, and
clear the paste flag when a fresh editor mounts, since block, embed and
file pastes fire no change event.

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 34.9 / 49.5 / 88.3 ms, after 8.4 / 12.0 / 19.6 ms. Keydown
handler time per key: 11 ms before, 0.7 ms after.

* fix: lint

---------

Co-authored-by: Tienson Qin <[email protected]>
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