Skip to content

perf(editor): parse edits off the main thread without copying the document - #4585

Merged
datlechin merged 3 commits into
mainfrom
perf/editor-parse-off-main
Nov 26, 2026
Merged

datlechin merged 3 commits into
mainfrom
perf/editor-parse-off-main

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

  • TextSnapshot is 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 of createTextSnapshot copying 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 no NSString substring, String or UTF-16 Data per chunk, and nothing reads the text storage from a parse.
  • The committed state, its text, the pending edits, the latest text and the revision sit behind one lock. A parse task reads them when it starts and commits with a check-and-swap under the same lock, so the commit no longer goes through DispatchQueue.main.sync, and a replaced state is freed on a utility queue.
  • No unbounded parse on main. The main-thread attempt has a 3 ms budget (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. ParseSession remembers which revision a parser holds, and any other parse resets it first.
  • TreeSitterExecutor no 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.
  • A highlight query that finds the queue busy runs inside its .access task against the committed snapshot; only the ranges go to main.
  • Changed ranges now diff the new tree against the old tree as edited, as ts_tree_get_changed_ranges requires. Before, a comment typed at the top of a 200K-character script invalidated all 199,967 characters; now 5.
  • A set up clears the previous document's state, so a caller waiting for a state gets the new document's.
  • os_signpost intervals (com.TablePro, category TreeSitter): applyEdit, snapshot, parse and queryHighlights, tagged main or background.

Local Swift Testing probe (debug build, serialized, not committed), main-thread time inside applyEdit:

main this PR
Typing in a 1.9M-character script, 200 keys: mean / p50 / p95 1.20 / 1.25 / 1.61 ms 0.20 / 0.10 / 0.71 ms
Same, total over 200 keys 240 ms 40 ms
' at the top of a 900 KB script 174.5 ms 3.4 ms
-- x at the top of a 200 KB script: time / characters invalidated 11.1 ms / 199,967 3.6 ms / 5

In 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 InputEdit points. SwiftTreeSitter still copies each chunk into its own input buffer: its Tree initializer is internal, so a raw ts_parser_parse cannot hand back a MutableTree.

Not in this PR: the single-fragment restyle (TextLine.restyle on 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.
  • TreeSitterBackgroundParseTests changes call sites only: findChangedByteRanges takes a revision and a budget, and pendingEdits is a property.
  • The Codex review pass did not run: the Codex account is at its usage limit until 27 Nov 2026 17:42. It needs a re-run before merge.

Closes #4535

…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.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:51
@datlechin
datlechin merged commit f8cacd7 into main Nov 26, 2026
11 checks passed
@datlechin
datlechin deleted the perf/editor-parse-off-main branch November 27, 2026 16:37
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.

perf(editor): parse edits off the main thread without copying the document

1 participant