Skip to content

perf(idle): save tabs only after a change and let the app nap with a tunnel open #4558

Done

Description

@datlechin

Problem

An app left open all day keeps working while nobody touches it.

  • Every 30 s, for each connection, the tab save rebuilds every tab's saved form on the main thread, JSON-encodes and hashes it, and checks the files on disk, only to find nothing changed. Since perf(storage): stop idle and repeated writes, and the caret timer with no drawn caret #4435 it writes nothing in that case, but it still does the work.
  • One SOCKS, Cloudflare, Cloud SQL Proxy or tunnel command connection disables App Nap for the whole process for as long as it is open. Every timer in the app then runs unthrottled, even when the app is hidden.

Cause

  • Each MainContentCoordinator starts its own 30 s loop (apple/TablePro/Views/Main/MainContentCoordinator.swift:715, apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift:27-37). Only the first coordinator of a connection saves. The others wake only to continue (:33).
  • The save builds aggregatedTabs and toPersistedTab for every tab on the main actor (apple/TablePro/Core/Services/Infrastructure/TabPersistenceCoordinator+AggregatedSave.swift:12-17, apple/TablePro/Views/Main/MainContentCoordinator.swift:476-486, apple/TablePro/Core/Services/Infrastructure/TabPersistenceCoordinator.swift:128).
  • On TabDiskActor, TabDiskInput encodes the state with queries emptied and takes its SHA-256 (apple/TablePro/Core/Storage/TabDiskActor.swift:63-80, :215-223). It then compares every query and stats the state file and sidecars before it returns without writing (:224-227, :254-264). Change detection happens by doing the work.
  • A change-driven save already exists: scheduleDraftSave debounces 1 s after an edit (MainContentCoordinator+DraftSave.swift:14-25).
  • Each tunnel manager calls ProcessInfo.beginActivity(options: .userInitiatedAllowingIdleSystemSleep) while any of its tunnels exists, and ends it only when the last one closes:
    • apple/TablePro/Core/SOCKS/SOCKSProxyManager.swift:360-370
    • apple/TablePro/Core/Cloudflare/CloudflareTunnelManager.swift:351-361
    • apple/TablePro/Core/CloudSQL/CloudSQLProxyManager.swift:438-448
    • apple/TablePro/Core/TunnelCommand/TunnelCommandManager.swift:349-359
  • For Cloudflare, Cloud SQL and tunnel commands, the bytes flow through a child process. TablePro only reads its stderr and waits for it to exit. The SOCKS proxy relays in process with NWConnection receive callbacks (SOCKSProxyManager.swift:544-549), and socket readiness still wakes a napped process.
  • perf(ssh): relay each session's channels from one event-driven reader and drop the App Nap hold #4248 dropped the same hold for SSH tunnels after measuring the relay, and listed these four as still holding it.

Change

  • Tab saves. Keep a persistence generation per connection. Bump it on every path that changes what is saved: the tabs setter (apple/TablePro/Models/Query/QueryTabManager.swift:16-31), selection (:35), setEditorText (:802), and every input enrichedForPersistence reads, such as sort columns and selection snapshots. A save returns before building aggregatedTabs when the generation equals the last saved one. A test pins that each persisted field, changed through its normal path, moves the generation.
  • Drive saves from that generation through the existing debounced save. Replace the per-coordinator loops with that, so nothing wakes while nothing changes. Quit, window close and disconnect keep their synchronous saves.
  • App Nap. Remove the activity from the Cloudflare, Cloud SQL and tunnel command managers, since their relay runs in another process. Remove it from the SOCKS proxy too. If the measurement below shows relay latency under App Nap, hold the activity only while a relay is moving bytes, not for the life of the tunnel.
  • Health checks are perf(connections): reconnect when the network returns and ping less while inactive #4546's: it slows the ping while the app is inactive and keeps it behind the session gate (apple/TablePro/Core/Database/ConnectionHealthMonitor.swift:78-86). This issue does not change ConnectionHealthMonitor. Overlaps perf(storage): Server Dashboard reads and gated idle items left from #4435 #4462: WI13 (count an answered statement as a connection check) is health-check work and goes with perf(connections): reconnect when the network returns and ping less while inactive #4546.

Overlaps #4435: WI11 already skips the disk write when nothing changed. This issue removes the encode, hash and file checks that still run. Overlaps #4439: LN-C covers saves during the restore pass, not idle saves.

Measure

  • 10 connections, 50 tabs, app idle for 10 minutes. Count save passes and TabDiskInput builds with signposts. Before: about 20 passes a minute. After: none after the last change.
  • Edit a query, change a sort, switch tabs, then quit and relaunch: every change is restored.
  • One SOCKS and one Cloudflare connection open, app hidden for 15 minutes: Activity Monitor shows App Nap "Yes". Compare idle wakeups with powermetrics --samplers tasks before and after.
  • Relay latency while napped: 500 SELECT 1 round trips through each tunnel, p50 and p99, napped against active, as perf(ssh): relay each session's channels from one event-driven reader and drop the App Nap hold #4248 did for SSH.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    #4597 replaces the 30 s loops with a persistence generation that drives the existing debounced save, lets an unchanged save return before building anything, and removes the App Nap activity from the SOCKS, Cloudflare, Cloud SQL and tunnel command managers. Left: the powermetrics idle wakeups and the napped relay latency (500 SELECT 1 round trips through each tunnel, p50 and p99, napped against active), which need live tunnels. If that latency shows a cost for SOCKS, the follow-up is an activity held only while a relay moves bytes.

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