Skip to content

perf(tabs): opening a table spends 167 ms of main-thread CPU, most of it in SwiftUI #4528

Backlog

Description

@datlechin

Problem

Opening a 1M-row PostgreSQL table from the sidebar takes 228-275 ms to the screen. The database is not the cost: the app's own trace (TABLEPRO_TABLE_LOAD_TRACE=1) shows the driver fetch at 4.6-14.9 ms and the grid reload at 2.4-3.8 ms.

Time Profiler over 10 open-and-close cycles from a sidebar click: 1,669 ms of main-thread CPU, about 167 ms per cycle.

  • SwiftUI graph updates: 45% (756 ms). App frames under it: ResultStatusBar.body 34 ms, ReadoutZoneLayout 19 ms, DataGridRepresentable.makeNSViewController 14 ms, MainContentView.body 10 ms. Most of it has no app frame: SwiftUI diffing the tab's view tree.
  • Core Animation commit, layout: 30% (498 ms), mostly NSHostingView layout.
  • Core Animation commit, display: 10% (171 ms), of which DataGridRowView.drawBackground 53 ms and the header 9 ms.
  • Drawing the grid itself: about 5 ms per open.
  • Toolbar validation and SettingsSlice.value.getter: 22-25 ms per 10 cycles.

So about 75% of the open is SwiftUI building and laying out the tab around the grid, not the grid.

Cause

A table tab is a SwiftUI tree inside a hosting controller that is rebuilt and re-laid out on every pass.

  • MainEditorContentView.editorAreaContent wraps every tab kind except the query tab in AnyView (apple/TablePro/Views/Main/Child/MainEditorContentView.swift:166-172). EditorAreaController.show hosts it in an NSHostingController<AnyView> and assigns rootView again on every apply (apple/TablePro/Views/Main/Child/EditorAreaController.swift:122-130). The query tab already left this path: QueryTabSplitController is AppKit down to its panes (EditorAreaController.swift:31-66, from perf(editor): build the query tab in AppKit and read the sidebar listing when the filter takes focus #3834).
  • resultsSection builds a VStack of the result-set bar, error banner, outdated-page notice, filter chrome, the grid and the status bar for each pass (MainEditorContentView.swift:602-644).
  • The status bar sits inside DataGridModelReader (MainEditorContentView.swift:634-641), which observes the whole DataGridModel (apple/TablePro/Views/Main/Child/DataGridModelReader.swift:9-15). The model publishes displayRevision, selectionRevision, editabilityRevision and fittingHeight (apple/TablePro/Core/DataGrid/DataGridModel.swift:60-64), so each of those rebuilds StatusBarSnapshot and the column catalog (MainEditorContentView.swift:872-900).
  • ResultStatusBar.body is a ViewThatFits over three tier rows (apple/TablePro/Views/Results/ResultStatusBar.swift:32-37). SwiftUI builds and measures each row until one fits, and each row runs ReadoutZoneLayout's slot measurement (ResultStatusBar.swift:441-470). That is the 34 ms and 19 ms above.
  • DataTabGrid applies .id(presentation.tabId) (apple/TablePro/Views/Main/Child/DataTabGrid.swift:17-18), so every table tab gets a new DataGridViewController from DataGridRepresentable.makeNSViewController (apple/TablePro/Views/DataGrid/DataGridView.swift:44-46), though DataGridViewController.adopt(_:) can move one grid to another model (apple/TablePro/Views/DataGrid/DataGridViewController.swift:118-134).
  • Above all of it, MainContentView is one SwiftUI tree in the split view's content pane (apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift:1001-1014), and it observes six objects (apple/TablePro/Views/Main/MainContentView.swift:27-41). Why one tab change re-runs this tree is perf(tabs): one tab change invalidates the views of every tab in the window #4529.

Overlaps #4437: G4b, keep one grid controller across table tabs (tabs.lldb.gridInit.perSwitch is still 1.0). This issue keeps the rest of the table tab around the grid.

Change

Build the table tab the way #3834 built the query tab: AppKit down to its panes, SwiftUI only in leaves that do not depend on the tab list.

  • Add a table tab case to EditorAreaContent next to .query, backed by a TableTabController (an NSViewController) that EditorAreaController creates once per window and updates in place when the selected tab or its state changes. No AnyView root and no rootView reassignment per pass.
  • The controller holds one DataGridViewController and calls adopt(_:) with the new tab's model, instead of a new grid per tab (this is perf(grid): wide grid fling and gated items left from #4426 #4437's G4b; land it here or there, once).
  • The bars above the grid (result-set bar, error banner, outdated-page notice, filter chrome) become views the controller shows or hides. Each one that stays SwiftUI gets its own NSHostingView with a stable root that observes a small model, so a change in one bar does not lay out the others.
  • The status bar: the controller picks the tier from its own width in viewDidLayout and hands it to the bar, so SwiftUI lays out one row, not up to three. The bar reads a per-tab status model that changes only when the counts, timing or selection count change, not on every DataGridModel publish.
  • Size the hosted leaves with sizingOptions so AppKit does not ask SwiftUI for intrinsic sizes the layout does not use.

Measure

  • Repeat the trace in "Opening a 1M-row PostgreSQL table" (research/measurements.md): Time Profiler, 10 open-and-close cycles from a sidebar click, Release build, same Mac and fixture. Report main-thread CPU per cycle and the split between SwiftUI graph updates, hosting layout and display.
  • Target: under 30 ms of main-thread CPU per open-and-close.
  • apple/scripts/benchmark-app-paths.sh (1,000,000-row table on SQLite, MySQL and PostgreSQL): click to first page drawn, median and p95, against the recorded baseline.
  • Screenshots of a table tab in light and dark, before and after, at regular, compact and narrow window widths, so the status bar tiers still match.

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