Repository navigation
enhance(editor): stop re-rendering the editor box on every keystroke - #13211
Conversation
834f882 to
bc8cd05
Compare
|
Pushed one more change inside this branch. 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 |
26b83c7 to
ef009cd
Compare
|
Pushed one more revision inside this branch, still one commit on current master.
|
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.
ef009cd to
3ca3bc5
Compare
|
@cursoragent review |
tiensonqin
left a comment
There was a problem hiding this comment.
Awesome fix, thanks for the contribution! 🚢 👍
…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
…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]>
…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]>
…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]>
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/boxsubscribed to[:editor/content uuid], so each keystroke re-rendered the whole box: the autosize textarea re-measured itself, the hidden#mock-textmirror (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/contentsubscription, so filtering while a popup is open is unchanged. The heading class (uniline-block/multiline-block,h1toh6) is the textarea's:classprop, 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 throughupdate-heading-class!, and a render recomputes the prop from the live content, so it never writes a stale class.:cacheMeasurementsis 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-optscaller 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 whenget-caret-posis asked for a position and only when the text changed since the last build; it produces the same DOM as before (idsmock-text_<char index>, a newline as0followed 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-textin 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 oneoffsetTop(first and last span) instead of one per character, andshortcut-select-up-downpasses them the input.next-cursor-pos-up-downreturns 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-downacts 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 fromedit-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-testpresses 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-testpresses 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'sscrollHeightfits in itsclientHeight. It passes on master and on this branch; on the revision of this branch that set the class from a layout effect it failed withclientHeight47 andscrollHeight136, 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-contentreads the editing textarea throughinputValueinstead 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 everyget-edit-contentassertion in the suite would read the empty string.inputValuereads the live value on both. The wholelogseq.e2e.editor-basic-testnamespace, built with the defines of.github/workflows/clj-e2e.ymland served locally, passes on this branch with that helper: 67 tests, 241 assertions, 0 failures.The existing unit suite, compiled with
pnpm cljs:testand run with the grouping ofbb dev:run-test-namespaces(231 namespaces in 40 node processes, 2376 tests, 9656 assertions): every test passes except one inlogseq.api.db-based.cli-testand one inlogseq.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.