Skip to content

perf(editor): keep each query tab's document when switching tabs - #4588

Merged
datlechin merged 6 commits into
mainfrom
perf/editor-tab-documents
Nov 27, 2026
Merged

datlechin merged 6 commits into
mainfrom
perf/editor-tab-documents

Conversation

@datlechin

Copy link
Copy Markdown
Member

Stacked on #4585: the parked parse uses its tree-sitter pipeline. Until #4585 merges, this diff includes its commits; review the last commit.

Summary

  • Each window keeps the documents of its hidden query tabs in an EditorDocumentCache, owned by EditorUndoHistories and keyed by tab id: the NSTextStorage with its styles on it, the detached line storage (TextLayoutManager.detachLines()) and the committed parse (TreeSitterClient.ParkedParse).
  • A switch goes through TextViewController.showDocument(_:id:replacing:). A tab kept there whose text and typing attributes still match comes back through setTextStorage(_:adopting:): no typing-attribute pass over the text, no line split, and the client installs the parked parse in setUp instead of parsing. Anything else, a tab whose text changed while hidden, a font or theme change, or a dropped document, takes the old setText path.
  • Lines typeset before are marked to be typeset again when they come into view, since settings or fold attachments may have changed while the tab was hidden. Splitting the text into lines is what is not repeated.
  • The cache drops the least recently shown documents once they hold more than 256 MiB. A document is charged 64 bytes per UTF-16 unit, measured below, plus 96 per line. Closing a tab drops its document with its undo history.

Local Swift Testing probe (debug build, not committed), two 1.9M-character tabs switched 20 times through SourceEditorHost:

#4585 alone this PR
Main thread per switch, p50 / p95 48.7 / 157.8 ms 22.2 to 31.9 / 30.3 to 69.2 ms over three runs
Until the tab is parsed again, p50 2,278 ms 22 to 32 ms (the parse is the kept one)

Memory, with 50 query tabs of 1,000,000 units each shown in turn: phys_footprint grew 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 TableProEditor over the editor, layout, highlighter, tree-sitter and undo suites: 283 passed.
  • The app build did not finish locally: the shared disk filled up during it. CI builds the app; the app-side change is two lines in SQLEditorController and two in EditorUndoHistories.
  • 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 #4537

…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
datlechin enabled auto-merge (squash) November 26, 2026 01:51
…ents

# Conflicts:
#	apple/Packages/TableProEditor/Sources/TableProEditorKit/TreeSitter/TreeSitterClient.swift
@datlechin
datlechin merged commit cc50546 into main Nov 27, 2026
11 checks passed
@datlechin
datlechin deleted the perf/editor-tab-documents 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): keep each query tab's document when switching tabs

1 participant