Skip to content

perf(memory): one app-wide budget for results held off screen #4556

Backlog

Description

@datlechin

Problem

Nothing bounds the memory of results across a session. Every background tab keeps its rows, every result of a multi-statement run is kept, and pinned results stay, in every window and connection, until the user closes or re-runs them. All of it is dirty anonymous memory, which macOS can only compress or swap.

One 1M x 20 result (SELECT * FROM trips_1m, Execute Without Limit) raised phys_footprint by about 690 MB on PostgreSQL (573 to 1,262 MB) and about 600 MB on MySQL (257 to 856 MB). #4521 makes each result much smaller, but the number of results held still has no bound.

Cause

  • TabSessionRegistry holds each tab's TableRows until the tab closes, reloads or replaces them (apple/TablePro/Models/Query/TabSessionRegistry.swift:9-10, :52-58, :76-82).
  • A tab switch only drops formatted text, as the code says: "The only memory a tab switch releases: formatted text, rebuilt per drawn cell. Rows stay until the user replaces them." (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift:280-289).
  • Each ResultSet holds its own tableRows (apple/TablePro/Models/Query/ResultSet.swift:30). The active result shares its buffer with the session (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:149-155), so pinned and non-active results are the extra copies.
  • resultSetsKeptPerRun defaults to 0, which keeps every result a run returns (apple/TablePro/Models/Settings/AppSettings.swift:116-117, :136), up to 100 per run (apple/TablePro/Models/Query/QueryBatchResult.swift:29).
  • The Mac app has no memory-pressure handling. fix(tabs): keep a table tab's rows until you reload it #3848 (fcc7073) removed MemoryPressureAdvisor and the evict-and-refetch schedulers, because a refetch changed what a clean tab showed. TableTabRowRetentionTests pins that contract: a tab shows the rows it last read. A comment still names a "memory-pressure monitor" that no longer exists (apple/TablePro/Core/Services/Infrastructure/LaunchEnvironment.swift:52).

Change

This builds on #4521's store and does not change it. A2 owns the columnar store, the window reads and teardown. This issue owns which held results stay resident.

  • One budget for the app, in bytes, derived from physical memory the way GridDisplayCacheBudget picks its tier. It counts the bytes each core store holds. ResultStore has no such read today (core/crates/ffi/src/results/store.rs), so add one, with the same measure ADR 0020 uses for a run's byte limit (core/docs/decisions/0020-a-run-keeps-a-byte-limit-of-rows.md).
  • What counts: every result not on screen. That is background tabs' buffers, non-active results of a multi-statement run, and pinned results, across all windows and connections. A visible result never counts against eviction. Staged edits stay in memory in the store's overlay and never spill.
  • Over the budget, least recently shown first:
    • Spill. The core store writes its full chunks to a file in the caches directory and maps them back read-only. Those pages are clean and file-backed, so the kernel can drop and re-read them without the app walking any cache. A tab that comes back shows the same rows with no query sent, which keeps the fix(tabs): keep a table tab's rows until you reload it #3848 contract.
    • Drop and reload applies only to a table tab's page, never to a query result, since running a statement again can write or return other rows. Reloading on return changes what a clean tab shows, which fix(tabs): keep a table tab's rows until you reload it #3848 removed on purpose. It needs a product decision before it ships. Until then, spill is the only path.
  • No new limit on rows or on kept results (D3). A single result larger than the budget stays whole while it is shown.
  • Fix the stale LaunchEnvironment comment.

Measure

  • Script: 50 tabs over 10 connections, 5 of them 1M-row query results with no row limit, one multi-statement run that keeps 20 results, and 3 pinned results. Record footprint (dirty plus compressed) and vmmap --summary after each step and after an hour idle. Before: dirty footprint grows with every tab. After: dirty footprint stays near the visible tabs plus the budget, and spilled chunks show as clean file-backed regions in vmmap.
  • Switching back to a spilled tab: identical rows, no statement in the server's statement log, TableTabRowRetentionTests passing, and the switch time from TabSwitchTrace reported.
  • A tab with staged edits keeps them through any amount of spilling of other tabs.

Part of the performance plan; depends on #4521.

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