Repository navigation
Description
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.tabsis a computed get/set over_tabswith no_modify(apple/TablePro/Models/Query/QueryTabManager.swift:16-32).mutate(tabId:)andmutate(at:)doblock(&tabs[index])through it (QueryTabManager.swift:784-798), so an element write copies the whole[QueryTab]buffer, then the setter always sendsobjectWillChange, maps ids twice, builds two anchor arrays and two id sets (QueryTabManager.swift:243-262) and reconciles the visible container.mutate(tabId:)also scans withfirstIndexinstead of the index map.QueryTabis a struct of about 40 stored properties (apple/TablePro/Models/Query/QueryTab.swift:45). There are 140mutate(tabId:)/mutate(at:)call sites.setEditorTextalready 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-21notes that the move of tab state into a reference type never happened.- Writes that change nothing still publish:
resetViewStateForNewSourcecallsmutateeven when its closure returns early (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:195-201).installTableRowswritesspatialColumnson every install (MainContentCoordinator+TableRowsMutation.swift:82-87).updateToolbarPendingStatewrites three@Publishedtoolbar properties without comparing (apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift:96-102).toolbarState.isResultsCollapsed = falseis written on every run (apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift:350,:982;QueryExecutionCoordinator+MultiStatement.swift:330,:357;QueryExecutionCoordinator+ExplainRun.swift:143).
onMainActorChangeisobjectWillChange.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 runsrefreshContextplusvalidateVisibleItemsN times (apple/TablePro/Core/Services/Infrastructure/MainWindowToolbar.swift:229-237).ConnectionCatalogSnapshot.scheduleReadalready coalesces with a flag and one task (apple/TablePro/Views/Main/ConnectionCatalogSnapshot.swift:120-128).MainContentViewobservestrailingPaneState,catalogSnapshot,tabManager,selectedEdits,toolbarStateandcoordinator(apple/TablePro/Views/Main/MainContentView.swift:27-41), and passesMainEditorContentViewseven new closures each pass (MainContentView.swift:540-571), which SwiftUI cannot compare.MainEditorContentViewobserves eight objects, including the coordinator with its 22@Publishedproperties (apple/TablePro/Views/Main/Child/MainEditorContentView.swift:18-50;apple/TablePro/Views/Main/MainContentCoordinator.swift:191-396).- Its body builds a new
EditorAreaInputofAnyViews and bindings (MainEditorContentView.swift:58-68).EditorAreaHost.updateNSViewControllerthen callsEditorAreaController.apply, which assignshistory.rootView,results.rootVieworotherContent.rootViewon every pass (apple/TablePro/Views/Main/Child/EditorAreaController.swift:55-60,:98-103,:122-130), andQueryEditorPaneController.applyassignsheader.rootView(apple/TablePro/Views/Editor/QueryEditorPaneController.swift:75-78).SQLEditorController.applyrunsapplyInputandpushEditorevery time (apple/TablePro/Views/Editor/SQLEditorController.swift:98-109); the editor's own diffing absorbs most of that push. HistoryPanelViewobserves 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.QueryTabManagerkeeps 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 (QueryTabisEquatable), and skip the id, anchor and registry passes unless an id, anchor, container or pin changed. - Coalesce
onMainActorChangein the shared helper: the first send sets a flag and schedules one main-actor task for the end of the turn, thescheduleReadpattern. Every subscriber gets one call per turn with no change at the call sites. Deliver through a main-actor task, not theRunLoop.mainscheduler, 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
@Publishedwrites listed above. EditorAreaInputcarries identity (tab id, tab kind, results mode) and the small per-tab models, not prebuiltAnyViews and closures.EditorAreaController.applyandQueryEditorPaneController.applyset arootViewonly when that identity changes; each hosted root observes its own model.SQLEditorController.applyskipspushEditorwhen no input the editor reads changed.- Pass the pagination actions through the coordinator instead of seven closures, so
MainEditorContentViewis not rebuilt byMainContentViewpublishes it does not read. Hold the coordinator unobserved in views that only call actions (HistoryPanelView).
Measure
- A Debug counter (or signpost) of
objectWillChangesends onQueryTabManagerand ofEditorAreaController.apply,EditorTabStripPaneControllerrefreshes andvalidateVisibleItemscalls per table open and per Run. Target: one pass when the tab arrives and one when the rows land. - A swift-testing case timing
mutateat 1, 10, 50 and 200 tabs withContinuousClock. Target: flat across tab counts. - Instruments SwiftUI template: body counts for
MainContentView,MainEditorContentView,HistoryPanelViewandResultStatusBarper 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