Repository navigation
perf(editor): parse edits off the main thread without copying the document - #4585
Merged
Merged
Conversation
…ument The tree-sitter client keeps the document as a rope of immutable UTF-16 chunks that each edit updates in place, so a parse gets its snapshot in O(log n) instead of a whole-document copy per key, and the read block hands tree-sitter chunk bytes without an NSString, String or transcode. State, pending edits, text and revision sit behind one lock: a task reads them when it starts and commits with a check-and-swap, so neither waits for the main thread, and old trees are freed off it. The main thread's parse attempt has a 3 ms budget; one that does not finish is resumed by the background task for the same revision, and any other parse resets the parser first. Queued tasks wait on a continuation instead of polling every 10 ms, and a busy-queue highlight query runs in its task against the committed snapshot. Changed ranges now diff the new tree against the old one as edited, as tree-sitter requires; before, a comment typed at the top invalidated the whole document.
This was referenced Nov 25, 2026
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.
Summary
TextSnapshotis now a rope (_RopeModule.Rope) of immutable UTF-16 chunks. The client keeps one in step with every edit, rebuilding only the chunks the edit touches, so a parse gets its text in O(log n) instead ofcreateTextSnapshotcopying the whole document per key. Every read, on main or off it, goes through the snapshot: the read block returns the chunk's bytes (Data(bytesNoCopy:)), with noNSStringsubstring,Stringor UTF-16Dataper chunk, and nothing reads the text storage from a parse.DispatchQueue.main.sync, and a replaced state is freed on a utility queue.syncParseBudget); a parse that does not finish is resumed by the background task for the same revision, as tree-sitter resumes a timed-out parse.ParseSessionremembers which revision a parser holds, and any other parse resets it first.TreeSitterExecutorno longer polls every 10 ms: a queued task waits on a continuation, woken when the queue changes or it is cancelled. Cancelling happens outside the lock, since a waiting task's cancellation handler takes it..accesstask against the committed snapshot; only the ranges go to main.ts_tree_get_changed_rangesrequires. Before, a comment typed at the top of a 200K-character script invalidated all 199,967 characters; now 5.os_signpostintervals (com.TablePro, categoryTreeSitter):applyEdit,snapshot,parseandqueryHighlights, taggedmainorbackground.Local Swift Testing probe (debug build, serialized, not committed), main-thread time inside
applyEdit:'at the top of a 900 KB script-- xat the top of a 200 KB script: time / characters invalidatedIn the 1.9M file no parse runs on main any more, and neither does the commit. The remaining main-thread work per key is the rope update (p50 0.04 ms) and the
InputEditpoints. SwiftTreeSitter still copies each chunk into its own input buffer: itsTreeinitializer is internal, so a rawts_parser_parsecannot hand back aMutableTree.Not in this PR: the single-fragment restyle (
TextLine.restyleon an unwrapped line). It is the same change as JV3 in #4442 and is left there; the comment on #4535 says so.Test plan
TreeSitterPipelineTests(new): random edits keep the snapshot equal to the storage and earlier copies keep their text; emptying the document; a parse that runs out of its main-thread budget finishes off main with the tree a fresh parse gives; a parse left unfinished is not resumed over changed text; changed ranges of a comment typed at the top stay near it; a waiter cancelled behind a running task reports at once; queued tasks run in order.verify.sh package TableProEditor 'TreeSitter|LargePaste|Highlighter|SyntaxHighlighting|TextViewController|SourceEditor': 171 passed.TreeSitterBackgroundParseTestschanges call sites only:findChangedByteRangestakes a revision and a budget, andpendingEditsis a property.Closes #4535