Use the editor's syntax tree instead of re-parsing the full document#41
Open
moudy wants to merge 1 commit into
Open
Use the editor's syntax tree instead of re-parsing the full document#41moudy wants to merge 1 commit into
moudy wants to merge 1 commit into
Conversation
moudy
marked this pull request as ready for review
July 17, 2026 01:46
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.
Why
TextState.updateImplre-parses the entire document synchronously —parser.parse(state.doc.toString(), treeFragments)— inside the ViewPlugin update path. This runs unbudgeted on the main thread on mount and again on every qualifying update. On large documents, or when the language parser has pathological cases (e.g.@lezer/markdown< 1.3.2's quadratic GFM autolink scan on long unbroken tokens), a single parse takes multiple seconds and the editor freezes at ~100% CPU whenever the minimap is enabled.Measured against a real workspace (31KB markdown file containing one 30k-char token, minimap
displayText: 'characters'): editor mount blocked the main thread for 7.1s + 3.6s + 3.5s ≈ 15s with a blank pane. With this change the minimap's own parse cost drops to zero (only the editor's budgeted parser runs).This is the amplifier behind replit/repl-it-web AGI-1427 (Zendesk #469883): CodeMirror's own language integration parses asynchronously with work budgets, so the editor alone survives slow parsers — the minimap's synchronous full parse is what turns a slow parse into a hard freeze, and it double-parses every document even in the happy path.
References AGI-1427
What changed
TextStatenow reads the editor's incrementally-maintained, work-budgeted tree viasyntaxTree(state)instead of running its own fullparser.parse(removes the_previousTree/TreeFragmentmachinery and the per-updatedoc.toString()for parsing).shouldUpdatealso fires whensyntaxTree(update.state) !== syntaxTree(update.startState), so minimap highlighting converges as the editor's background parse advances (including right after mount, when the tree may only cover part of the document).syntaxTreereturns an empty tree, which yields no highlight spans (previouslytreewasundefinedand highlighting was skipped).Verified in the repl-it-web workspace with this build: pathological file opens instantly, minimap renders with correct highlighting in both
blocksandcharactersmodes, and highlighting fills in as background parsing progresses.~ written by Zerg 👾 (mutated-thor-88c5)