Skip to content

perf(results): hold every Mac result in the core's columnar store #4521

Backlog

Description

@datlechin

Problem

A result costs far more memory than its data, and every whole-result operation walks that structure.

  • 1M rows x 20 columns (SELECT * FROM trips_1m, Execute Without Limit): phys_footprint goes from 573 MB to 1,262 MB on PostgreSQL and from 257 MB to 856 MB on MySQL. The heap holds 1M row arrays (896 MB on PostgreSQL, 512 MB on MySQL), 2.0M heap strings for the two 19-character timestamps per row (130 MB) and a 32 MB row index.
  • No result can pass 5,000,000 rows, so a 10M-row result cannot be held at all.
  • Sort of held rows, masking, the JSON, Map and Chart views and teardown all walk every row, several of them on the main thread.
  • Every tab keeps its rows for as long as it is open.

Cause

  • Each cell is a @frozen enum PluginCellValue of .text(String) or .bytes(Data) (apple/Plugins/TableProPluginKit/PluginCellValue.swift:3-8), 24 B per slot. Text over 15 UTF-8 bytes is its own heap object. The plugin ABI carries rows as [[PluginCellValue]] (apple/Plugins/TableProPluginKit/PluginQueryResult.swift:6), and the app keeps them as ContiguousArray<Row> (apple/TablePro/Models/Query/TableRows.swift:10), each Row a RowID with a UUID payload plus its own value array (apple/TablePro/Models/Query/Row.swift:9-31).
  • The 5M ceiling is PluginRowLimits.emergencyMax (apple/Plugins/TableProPluginKit/PluginRowLimits.swift:4), referenced about 70 times across the plugins.
  • Whole-result work on the main actor:
    • A held sort calls TableRowsSorting.sorted inside a main-actor callback (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:215-227). After it, RowPositions puts every row in a [RowID: Int] dictionary (apple/TablePro/Models/Query/RowPositions.swift:13, :35-43).
    • TableRows.addMasks rewrites masked cells in every row (apple/TablePro/Models/Query/TableRows.swift:225).
    • perf(grid): copy, type-select and format changes walk every row on the main thread #4531 lists the rest (inspector select-all, value filters, display formats, type-select, find).
  • ResultsJsonView, ResultMapView and ResultChartView store the whole TableRows (apple/TablePro/Views/Results/ResultsJsonView.swift:5, ResultMapView.swift:30, ResultChartView.swift:42). While one is on screen, every cell edit copies the row buffer, which updateTableRows exists to avoid (apple/TablePro/Models/Query/TabSessionRegistry.swift:60-74).
  • A tab keeps up to 100 result sets (apple/TablePro/Models/Query/QueryBatchResult.swift:29) and nothing bounds held results across tabs.
  • The core already has the right shape. ResultStore keeps chunked columns of 65,536 rows (core/crates/ffi/src/results/store.rs:11-36). Windows reads it one window at a time through tablepro_cursor_fetch (core/crates/ffi/src/window_abi.rs:60, windows/src/TablePro.Interop/ResultCursor.cs). The Mac cannot use it:
    • The Apple build leaves out the engines (apple/scripts/build-core.sh:38).
    • The store is private to the ffi crate (core/crates/ffi/src/lib.rs:36).
    • The Swift module's header comes from UniFFI only, so no tablepro_cursor_* function reaches Swift.
  • As it stands, the store spends 17 B per cell on overhead (core/crates/ffi/src/results/column_cells.rs:22-28), so for short values it would save little more than the per-row header. perf(core): decode results straight into typed columns the store keeps #4522 fixes that first.

Change

Decision D1: one store for macOS, Windows and Linux, owned by the core.

  • ADR first. Before any code, amend ADR 0010 (core/docs/decisions/0010-macos-on-the-core.md:18-28, slice 5 "Engines, one at a time", and the "No engines in the Apple library" rationale at :76). Result storage comes first: the Apple library links the store and its window ABI without the engines, and engines follow one by one. The amendment also settles ADR 0003's open point (core/docs/decisions/0003-result-windows-for-native-grids.md:48): pending edits, sort, filter and find live in the core.
  • The store. Replace [[PluginCellValue]] and ContiguousArray<Row> in the app with the core's ResultStore, as perf(core): decode results straight into typed columns the store keeps #4522 leaves it: chunked columns, one byte buffer per column, typed lanes for numbers and dates, rows addressed by position. Inserted rows and edits sit in an overlay keyed by row id, as SparseRowState does for unloaded cells today. Masks apply when a window is read, not baked into storage.
  • Filling it. Swift plugins feed the store through the frozen [[PluginCellValue]] path, converted batch by batch at the host boundary, until perf(results): let a plugin write column bytes straight into the result store #4523's column-batch entry lands and each engine later moves to the core.
  • Reading it. Generate a C header for window_abi.rs into the TableProFFIC module. Add a Swift cursor that owns a TpCursor. The grid's data source fetches the visible rows and columns plus a margin off the main actor, and builds strings only for the cells it draws.
  • Operations.
    • Sort, value-filter distinct values, find, the inspector and display-format changes become store calls that run off the main thread and can be cancelled.
    • A sort installs a permutation (ADR 0007) instead of reordering rows.
    • Result views read windows instead of holding the buffer.
  • Teardown. Dropping a result frees O(chunks) buffers, so perf(results): free a replaced or closed result off the main thread #4527's hand-off goes.
  • Limits.
  • Overlaps perf(export): MongoDB export documents, Parquet limits and unmeasured items left from #4434 #4444: its item "16d, compact row storage (101 B a cell): not now, per Dat (D25)" is superseded by decision D1 (2026-11-26). This issue is that work.

Measure

  • 1M x 20 trips_1m on PostgreSQL and MySQL. Record phys_footprint before and with the result open, against today's 573 to 1,262 MB and 257 to 856 MB. Target: the result costs under 150 MB.
  • Instruments Allocations: persistent allocations per result drop from about one per row (plus one per long string) to O(columns x chunks).
  • trips_10m: opens in full, and a column sort finishes with no main-thread hang over 100 ms (Hangs instrument).
  • Close a tab and re-run a query holding 1M rows: no free/swift_release run over 5 ms on the main thread.
  • The grid's scroll Animation Hitches on a 200-column table do not regress.

Part of the performance plan; depends on #4522.

Activity

  1. added
    enhancement
    platform-macos
    rust
    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-macosrust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions