Repository navigation
Description
Problem
Closing a tab that holds a large result, running its query again, or replacing its rows with Fetch All frees the old result on the main thread. A 1M-row result is about 1M row arrays plus one heap string per long cell. In a scratch binary with the app's types, freeing 1M x 10 rows took 55 ms with short strings and 206 ms with 24-byte strings. Rows stop at 5,000,000 today, so the worst case is about 0.3 to 1 s with the window frozen. These figures are from the scratch binary; the app has not been measured.
Cause
- The session's buffer and the result set share one
ContiguousArray<Row>:TabSession.tableRows(apple/TablePro/Models/Query/TabSession.swift:26) andResultSet.tableRows(apple/TablePro/Models/Query/ResultSet.swift:30). Whoever drops the last reference pays for the whole teardown, inline. - Every path that drops it runs on the main actor:
TabSessionRegistryis@MainActor.setTableRowsandremoveTableRowsoverwritesession.tableRows(apple/TablePro/Models/Query/TabSessionRegistry.swift:52-58,:76-82), andunregisterdrops the whole session (:20-22).- On a re-run,
setActiveTableRowsreplaces the buffer (apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift:297). ThenreplaceResultorreplaceUnpinnedResultsdrops the oldResultSetinsidetabManager.mutate(:332,:334). - The
tabssetter keepslet oldValue = _tabsand releases it when the setter returns (apple/TablePro/Models/Query/QueryTabManager.swift:16-31). - Fetch All installs its rows inside
MainActor.run(apple/TablePro/Core/Coordinators/PaginationCoordinator.swift:405-430). - Closing tabs calls
unregisterandremoveTableRows(apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift:42,:62;apple/TablePro/Views/Main/MainContentCommandActions.swift:729).
- Nothing defers the release anywhere.
Overlaps #4453: L1, close paths (a closed window lets go of its coordinator and panes; the closepaths scenario is not in the suite yet). That issue checks that a closed window's objects are released. This one is about which thread frees a result's rows.
Change
- In the one place that replaces or removes rows, the registry's
setTableRows,removeTableRowsandunregister, take the outgoingTableRowsout of the session. End its last reference in a detached task at utility priority. Do not add a hand-off at each call site. - The hand-off must cover every holder, or the real free still runs on the main thread when the last of them goes:
- the session's buffer;
- the dropped
ResultSet'stableRows, released through the same path when a result set is replaced or removed; - the grid's per-row display cache (
apple/TablePro/Core/DataGrid/RowDisplayBox.swift) for that buffer.
- This is a stopgap. Once perf(results): hold every Mac result in the core's columnar store #4521 holds results in the core's store, a teardown frees O(chunks) buffers, and this hand-off can go.
Measure
- Load 1M x 10 rows with 30-byte text in a query tab. Press Run again, then close the tab. In Instruments (Time Profiler plus Hangs), look for
swift_releaseandfreeunder the main run loop. Target: no main-thread stall over 5 ms from the teardown, where a scratch binary predicts about 200 ms today. - A test holds a
.bytescell whoseDatahas a custom deallocator that records the thread it runs on. It checks that the free happens off the main thread for each of re-run, Fetch All replace, tab close and window close.
Part of the performance plan. Superseded by #4521 once that lands.
Activity