Repository navigation
🐛 fix: keep per-line insertion linear and bound undo memory - #243
Open
lobehub-coder wants to merge 5 commits into
Open
lobehub-coder wants to merge 5 commits into
lobehub-coder wants to merge 5 commits into
Conversation
…each update Each per-line insert (insertText + Enter, one commit each) ran a RootNode transform that walked the whole document twice to normalize node IDs, and the heading service / TOC refreshed slugs on every content update because Lexical flags the root as intentionally dirty on every transform pass. - Node IDs: once a committed state is known to be normalized, check only the nodes the update created or wrote against an id index; fall back to the existing full pass for setEditorState, skipTransforms updates, policy changes and any duplicate, so repair semantics are unchanged. - Headings: share one exact change check that ignores the root flag and only walks subtrees of nodes that were inserted, removed or moved.
The DiffNode, table-row-diff and table-cell-diff update listeners (and useHasDiffNode) scanned every node of the document on each update via $nodesOfType / the node map. A change can only produce a node needing normalization through a node the update touched, and Lexical marks every ancestor of a change dirty too, so check the dirty nodes and scan everything only on load/undo (setEditorState marks just the root, history restores carry HISTORIC_TAG). Diff semantics are unchanged.
…igger a shortcut The shortcut listener opened a nested editor.update() on every collapsed-selection change, cloning the whole editor state even when no transformer could apply. Check the same preconditions the transformer runners bail out on in a read first.
Each undo entry holds a full EditorState. Inserting a document line by line pushes about two entries per line, so retained memory grew with lines x document size (~450 MB at 2,000 lines) until the tab crashed. Drop the oldest entries once the stack would keep more than 1M node-map entries at the current document size, always keeping at least 100 steps.
Insert N lines the way a browser does when it bypasses paste (one committed text insert plus one Enter per line) and assert exactly 2N updates and that the editor's own callbacks scale linearly (4x lines -> < 7x time; master is ~16x). Also cover incremental node-ID checks, their fallbacks, and heading-change detection.
|
Thank you for raising your pull request and contributing to our Community. 非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
|
LobeHub is tracking this pull request. Merging accepts the delivery; failing checks and review feedback reach the agent that opened it. Learn more ↗︎
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💻 变更类型 | Change Type
🔀 变更说明 | Description of Change
Fixes the quadratic slowdown (and the memory growth behind the crash) when text reaches the editor one line at a time instead of through
paste.Reproduction / trigger. In Chrome, trusted per-line input —
Input.insertText(line)followed byEnter, as automation tools, extensions and some input methods do — fires onebeforeinput insertTextand onekeydown Enterper line. That makes 2 committed updates per line, never apasteevent, so the markdown paste path's single batch insert is not used. Other paths checked:document.execCommand('insertText', …): nobeforeinput. Lexical reconciles the DOM mutation and drops all but the first line (a separate data-loss issue, not this one).Input.insertText: one update, inserted as line breaks.beforeinputevents in a synchronous loop: Lexical batches them into one commit.Every per-line update then paid for work proportional to the whole document:
$normalizeNodeIdsRootNode transform: walks the whole tree twice per update_cloneNotNeeded) against an id index of the last normalized committed state. Any duplicate,setEditorState,skipTransformsupdate, policy change, or write the transform did not see falls back to the existing full pass, so repair semantics are unchanged.root === truewas always true)$nodesOfTypeper update (plususeHasDiffNodescanning the node map)setEditorStatemarks just the root, history restores carryHISTORIC_TAG).editor.update()on every collapsed-selection change, cloning the editor state even when no transformer can applyCrash. The undo stack keeps a full
EditorState(one node-map entry per node) per entry, and per-line input pushes 2 entries per line. Heap measured after GC: 52 MB → 500 MB at 2,000 lines, growing with lines × document size; at 1,500 lines, clearing the history drops the heap from 322 MB to 56 MB.registerHistoryMemoryLimitdrops the oldest entries once the stack would retain more than 1M node-map entries at the current document size, and always keeps ≥ 100 undo steps. Small documents are unaffected (1,000 nodes → 1,000 steps).Not changed. Paste and its markdown auto-conversion (
PASTE_COMMAND → INSERT_MARKDOWN_COMMAND), litexml diff semantics, and node-ID repair rules (previous owner keeps the ID, copies and splits get fresh ones). No debounce/throttle anywhere: every update is still processed when it happens.📝 补充信息 | Additional Information
Measurements. Chrome 1248 headless renders the real
Editorwith LobeHub's page-editor plugin set. Each line is a trustedinsertText+Enterof 61 characters. "Plain Lexical" is a bare rich-text editor with no LobeHub plugins, the floor.tests/per-line-insert-performance.test.ts)The update count is exactly 2 per line in every build, and the editor's own work now scales linearly. Total wall time in Chrome still grows faster than linear (2× lines ≈ 3.3× time), at the same rate as plain Lexical: what's left is Chrome's forced layout / selection sync in
setBaseAndExtentand Lexical's state clone and root reconcile. The CPU profile at K=2,000 has no editor plugin frames left; on master,$normalizeNodeIdswas 35.7%. In headless Chrome, master did not crash at 2,000 lines, so heap growth is the evidence for the crash fix.Paste: a real
Ctrl+Vof mixed markdown firesPASTE_COMMANDonce, thenINSERT_MARKDOWN_COMMANDonce, and renders the same structure on master and this PR (headings, bold, link, lists, quote, code block, table). One undo returns to the raw text on both. The existing paste/markdown suites pass 42/42 on both.Acceptance (evidence, recordings, raw data): https://lobehub.com/acceptance/8155085c-44d4-4329-aa89-16b2a3ba099e
Tests:
vitest run140 files / 974 tests pass; type-check clean. Each new test was also run against master'ssrc/: the perf, node-visit and nested-update tests fail there; the fallback, heading-shortcut and normalization tests pass on both, as guards for unchanged behavior.A follow-up is needed in the LobeHub app. The page editor's own update listener (
src/features/EditorCanvas/InternalEditor.tsx) exportsgetDocument('json')and deep-compares it on every content update.handleContentChangethen exports markdown and JSON again. That is O(document) per line on the app side, which this PR cannot remove.