Skip to content

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

Done

Description

@datlechin

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.apply saves the outgoing tab's history and applies the new input (apple/TablePro/Views/Editor/SQLEditorController.swift:98-111). The text reaches TextViewController.setText (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController.swift:286-301).
  • TextView.setText creates NSTextStorage(string:) and setTextStorage adds the typing attributes over the whole range and calls layoutManager.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 one TextLine, a UUID and a Typesetter per 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 new Highlighter (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+Highlighter.swift:19-33). TreeSitterClient.setUp copies the whole text into a snapshot and parses from scratch (apple/Packages/TableProEditor/Sources/TableProEditorKit/TreeSitter/TreeSitterClient.swift:119-158).
  • textViewDidReplaceDocument runs 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: saveTabHistory stores EditorTabHistory with 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). restoreTabHistory compares 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 setTextStorage path 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 String of the same contents.

Measure

  • Add an editor tab-switch interval to InteractionTracer (apple/TablePro/Core/Diagnostics/InteractionTrace.swift has 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_footprint and Allocations before and after.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions