Skip to content

perf(tabs): one tab change invalidates the views of every tab in the window #4529

Backlog

Description

@datlechin

Problem

Any write to one tab, including writes that change nothing, wakes the whole window: the editor area, the tab strip and the toolbar. A table open or a query run writes a tab many times (the tab context, the schema, the default sort, the rows, the view state, the result, keys, the phase-2 count), and each write is one full pass. The direct CPU per write is small (tens of microseconds at 50 tabs in one connection); the cost is the view work each write fans out to, which lands in the SwiftUI and hosting layout time measured in #4528 (75% of 167 ms per table open). How many passes an open takes has not been counted.

Cause

  • QueryTabManager.tabs is a computed get/set over _tabs with no _modify (apple/TablePro/Models/Query/QueryTabManager.swift:16-32). mutate(tabId:) and mutate(at:) do block(&tabs[index]) through it (QueryTabManager.swift:784-798), so an element write copies the whole [QueryTab] buffer, then the setter always sends objectWillChange, maps ids twice, builds two anchor arrays and two id sets (QueryTabManager.swift:243-262) and reconciles the visible container. mutate(tabId:) also scans with firstIndex instead of the index map. QueryTab is a struct of about 40 stored properties (apple/TablePro/Models/Query/QueryTab.swift:45). There are 140 mutate(tabId:)/mutate(at:) call sites.
  • setEditorText already writes in place and publishes only when a fact drawn from the text flips (QueryTabManager.swift:800-819). Every other write still publishes. TabSession.swift:9-21 notes that the move of tab state into a reference type never happened.
  • Writes that change nothing still publish:
    • resetViewStateForNewSource calls mutate even when its closure returns early (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:195-201).
    • installTableRows writes spatialColumns on every install (MainContentCoordinator+TableRowsMutation.swift:82-87).
    • updateToolbarPendingState writes three @Published toolbar properties without comparing (apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift:96-102).
    • toolbarState.isResultsCollapsed = false is written on every run (apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift:350, :982; QueryExecutionCoordinator+MultiStatement.swift:330, :357; QueryExecutionCoordinator+ExplainRun.swift:143).
  • onMainActorChange is objectWillChange.receive(on: RunLoop.main).sink (apple/TablePro/Extensions/ObservableObject+MainActorSink.swift:19-23). Each send runs the subscriber once, so a burst of N writes rebuilds the tab strip snapshot N times (apple/TablePro/Core/Services/Infrastructure/EditorTabStripPaneController.swift:31-41) and runs refreshContext plus validateVisibleItems N times (apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift:229-237). ConnectionCatalogSnapshot.scheduleRead already coalesces with a flag and one task (apple/TablePro/Views/Main/ConnectionCatalogSnapshot.swift:120-128).
  • MainContentView observes trailingPaneState, catalogSnapshot, tabManager, selectedEdits, toolbarState and coordinator (apple/TablePro/Views/Main/MainContentView.swift:27-41), and passes MainEditorContentView seven new closures each pass (MainContentView.swift:540-571), which SwiftUI cannot compare. MainEditorContentView observes eight objects, including the coordinator with its 22 @Published properties (apple/TablePro/Views/Main/Child/MainEditorContentView.swift:18-50; apple/TablePro/Views/Main/MainContentCoordinator.swift:191-396).
  • Its body builds a new EditorAreaInput of AnyViews and bindings (MainEditorContentView.swift:58-68). EditorAreaHost.updateNSViewController then calls EditorAreaController.apply, which assigns history.rootView, results.rootView or otherContent.rootView on every pass (apple/TablePro/Views/Main/Child/EditorAreaController.swift:55-60, :98-103, :122-130), and QueryEditorPaneController.apply assigns header.rootView (apple/TablePro/Views/Editor/QueryEditorPaneController.swift:75-78). SQLEditorController.apply runs applyInput and pushEditor every time (apple/TablePro/Views/Editor/SQLEditorController.swift:98-109); the editor's own diffing absorbs most of that push.
  • HistoryPanelView observes the coordinator only to call actions (apple/TablePro/Views/Editor/History/HistoryPanelView.swift:15).

Overlaps #4286 (closes #4270): keystrokes no longer publish tabs (PF-06), and the main views observe a per-connection ConnectionCatalogSnapshot instead of the app-wide services (PF-07). This issue covers every other tab write.

Change

Built by hand with ObservableObject, one model per tab. The macOS 13 floor stays, so no @Observable.

  • Give each tab its own observable model (a QueryTabModel: ObservableObject) for the state that changes while the tab runs: execution, pagination, display, result and metadata. QueryTabManager keeps ids, order, selection, titles and pins, and publishes only when those change. A result landing in one tab then wakes only the views showing that tab.
  • For what stays in the array, write _tabs[index] in place, look the index up in _tabIndexMap, skip the publish when the element is equal before and after (QueryTab is Equatable), and skip the id, anchor and registry passes unless an id, anchor, container or pin changed.
  • Coalesce onMainActorChange in the shared helper: the first send sets a flag and schedules one main-actor task for the end of the turn, the scheduleRead pattern. Every subscriber gets one call per turn with no change at the call sites. Deliver through a main-actor task, not the RunLoop.main scheduler, so callbacks do not queue up behind event tracking. The tab strip also skips the snapshot when the ids, titles, selection and busy set it reads did not change.
  • Guard the equal-value @Published writes listed above.
  • EditorAreaInput carries identity (tab id, tab kind, results mode) and the small per-tab models, not prebuilt AnyViews and closures. EditorAreaController.apply and QueryEditorPaneController.apply set a rootView only when that identity changes; each hosted root observes its own model. SQLEditorController.apply skips pushEditor when no input the editor reads changed.
  • Pass the pagination actions through the coordinator instead of seven closures, so MainEditorContentView is not rebuilt by MainContentView publishes it does not read. Hold the coordinator unobserved in views that only call actions (HistoryPanelView).

Measure

  • A Debug counter (or signpost) of objectWillChange sends on QueryTabManager and of EditorAreaController.apply, EditorTabStripPaneController refreshes and validateVisibleItems calls per table open and per Run. Target: one pass when the tab arrives and one when the rows land.
  • A swift-testing case timing mutate at 1, 10, 50 and 200 tabs with ContinuousClock. Target: flat across tab counts.
  • Instruments SwiftUI template: body counts for MainContentView, MainEditorContentView, HistoryPanelView and ResultStatusBar per table open, with 50 tabs open on the connection.
  • The perf(tabs): opening a table spends 167 ms of main-thread CPU, most of it in SwiftUI #4528 trace (10 open-and-close cycles of a 1M-row PostgreSQL table) with 50 tabs open, before and after: main-thread CPU per cycle.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
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