Skip to content

perf(results): free a replaced or closed result off the main thread #4527

Backlog

Description

@datlechin

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) and ResultSet.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:
    • TabSessionRegistry is @MainActor. setTableRows and removeTableRows overwrite session.tableRows (apple/TablePro/Models/Query/TabSessionRegistry.swift:52-58, :76-82), and unregister drops the whole session (:20-22).
    • On a re-run, setActiveTableRows replaces the buffer (apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift:297). Then replaceResult or replaceUnpinnedResults drops the old ResultSet inside tabManager.mutate (:332, :334).
    • The tabs setter keeps let oldValue = _tabs and 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 unregister and removeTableRows (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, removeTableRows and unregister, take the outgoing TableRows out 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's tableRows, 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_release and free under 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 .bytes cell whose Data has 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

  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