Repository navigation
Done
Labels
Description
Problem
One editor serves every query tab. Switching to a tab rebuilds that tab's whole document: a new text storage, a new line tree, a new highlighter with a full tree-sitter parse from scratch, folds and the statement controls. Colours come back only after the full parse. The cost grows with the size of the tab being shown, and it is paid again on every switch back to the same tab. No switch time is measured yet.
Cause
- On a tab change,
SQLEditorController.applysaves the outgoing tab's history and applies the new input (apple/TablePro/Views/Editor/SQLEditorController.swift:98-111). The text reachesTextViewController.setText(apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController.swift:286-301). TextView.setTextcreatesNSTextStorage(string:)andsetTextStorageadds the typing attributes over the whole range and callslayoutManager.reset()(apple/Packages/TableProEditor/Sources/TableProTextEngine/TextView/TextView+SetText.swift:13-51).reset()rebuilds the line tree from the storage (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextLayoutManager/TextLayoutManager.swift:203-213,:185), with oneTextLine, aUUIDand aTypesetterper line (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextLine/TextLine.swift:13,:16). The code's own comment gives about 500 ms for a file over 500k lines (TextLayoutManager.swift:194).setUpHighlighter()builds a newHighlighter(apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+Highlighter.swift:19-33).TreeSitterClient.setUpcopies the whole text into a snapshot and parses from scratch (apple/Packages/TableProEditor/Sources/TableProEditorKit/TreeSitter/TreeSitterClient.swift:119-158).textViewDidReplaceDocumentruns the full statement scan synchronously on the main thread (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:264), which the code measures at 31 ms at the 2,000,000-unit limit, and the fold model starts over (TextViewController.swift:289).- What a hidden tab keeps is text plus undo:
saveTabHistorystoresEditorTabHistorywith the text, the undo stacks, Vim marks, selection and scroll (apple/TablePro/Views/Editor/SQLEditorController.swift:156-166,apple/TablePro/Views/Editor/EditorUndoHistories.swift:11-17).restoreTabHistorycompares the full saved text with the editor's text to decide whether the history still applies (SQLEditorController.swift:170-179).
Change
- Add an editor document object per query tab that owns the
NSTextStorage, the line storage, the tree-sitter state, the style ranges, the fold and statement indexes (perf(editor): keep statement boundaries in an index that edits update #4534) and the undo history. - Switch tabs by swapping the document into the single text view with the existing
setTextStoragepath plus a line-storage swap, so a switch costs only the visible range's layout. No re-parse, no full statement scan, no full-text comparison. - Keep recently shown documents under a byte budget that charges the storage, the line tree and the parse trees, and move that budget into the app-wide one when perf(memory): one app-wide budget for results held off screen #4556 lands. An evicted document falls back to text only, which the tab model already holds, and is rebuilt off the main thread when shown again.
- Have the tab model read the document's text instead of holding a second
Stringof the same contents.
Measure
- Add an editor tab-switch interval to
InteractionTracer(apple/TablePro/Core/Diagnostics/InteractionTrace.swifthas no such path) that ends when the visible range is highlighted. - Switch between two 1.9M-character tabs 20 times: median and p95 of that interval, and main-thread CPU per switch, before and after.
- Memory: 50 query tabs of 1 MB each,
phys_footprintand Allocations before and after.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#4588 ships the part that stands alone: a per-window document cache keeps each hidden tab's text storage, line storage and committed parse, and a switch back with the same text swaps them in without a line split or a parse. It is stacked on #4585.
Measured locally (two 1.9M-character tabs, 20 switches): main thread per switch p50 48.7 ms to 22 to 32 ms, and the tab is parsed again after 22 to 32 ms instead of 2.3 s. A kept 1,000,000-unit document holds 63.6 MB of live heap, mostly its syntax tree, so the default 256 MiB budget keeps about four such tabs.
Left:
textViewDidReplaceDocument; it goes with the statement index in perf(editor): keep statement boundaries in an index that edits update #4534.restoreTabHistorystill compares the saved text with the editor's text to restore undo.Stringof the text.setTexton the main thread; its parse already runs off main since perf(editor): parse edits off the main thread without copying the document #4585.