Repository navigation
perf(editor): keep each query tab's document when switching tabs - #4588
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.
One editor serves every query tab, and a switch built the incoming tab's document from its text: a new text storage, the line tree split again, and a full tree-sitter parse before any color came back. Each window now keeps the documents of its hidden tabs in an EditorDocumentCache: the text storage with its styles, the detached line storage and the committed parse. A tab shown again with the same text and typing attributes swaps them back in; lines typeset before are typeset again when they come into view, since settings or folds may have changed. The cache drops the least recently shown documents past 256 MiB, charged 64 bytes per UTF-16 unit as measured, and a closed tab's document goes with its undo history.
datlechin
enabled auto-merge (squash)
November 26, 2026 01:51
…ents # Conflicts: # apple/Packages/TableProEditor/Sources/TableProEditorKit/TreeSitter/TreeSitterClient.swift
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.
Stacked on #4585: the parked parse uses its tree-sitter pipeline. Until #4585 merges, this diff includes its commits; review the last commit.
Summary
EditorDocumentCache, owned byEditorUndoHistoriesand keyed by tab id: theNSTextStoragewith its styles on it, the detached line storage (TextLayoutManager.detachLines()) and the committed parse (TreeSitterClient.ParkedParse).TextViewController.showDocument(_:id:replacing:). A tab kept there whose text and typing attributes still match comes back throughsetTextStorage(_:adopting:): no typing-attribute pass over the text, no line split, and the client installs the parked parse insetUpinstead of parsing. Anything else, a tab whose text changed while hidden, a font or theme change, or a dropped document, takes the oldsetTextpath.Local Swift Testing probe (debug build, not committed), two 1.9M-character tabs switched 20 times through
SourceEditorHost:Memory, with 50 query tabs of 1,000,000 units each shown in turn:
phys_footprintgrew 8 MB before and 334 MB here, since the cache keeps the last 4 tabs within its budget. Live heap per kept 1,000,000-unit document measured 63.6 MB, most of it the syntax tree, which sets the 64 bytes per unit charge.Left for later, in a comment on #4537: the statement scan on a switch (#4534), the fold model starting over, the undo history's full-text comparison, the tab model's second
String, moving the budget into the app-wide one (#4556), and rebuilding a dropped document off the main thread.Test plan
EditorDocumentCacheTests(new): a tab shown again gets back the same storage, line storage and tree-sitter state, and still highlights; a tab whose text changed while hidden is built from the new text; past the limit the least recently shown tab is dropped first; a closed tab's document is dropped; other typing attributes are not reused.verify.sh package TableProEditorover the editor, layout, highlighter, tree-sitter and undo suites: 283 passed.SQLEditorControllerand two inEditorUndoHistories.Closes #4537