Skip to content

perf(macos): stop idle polling and repeated work in SSH accept, MCP, themes, linked folders, dashboard, SFTP, Snowflake and sync - #4229

Merged
datlechin merged 4 commits into
mainfrom
fix/perf-idle-network
Nov 12, 2026
Merged

datlechin merged 4 commits into
mainfrom
fix/perf-idle-network

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

Idle wakeups, polling and repeated work from the 2026-11-11 performance audit (lane idle-network).

  • SSH and Remote SQLite accept loops (CPU-06): a DispatchSource on the listening socket replaces the 200 ms poll loop in LibSSH2Tunnel, RemoteSQLiteTunnel and the iOS SSHTunnel. The listening socket closes only after the source's cancel handler ran. Measured: 4.4 wakeups/s per tunnel before, 0 after (baseline 0.1/s), and no thread parked in poll.
  • SQLite file watcher (CPU-04): new PluginKit requirement schemaChangeToken() (default nil, kit stays 44). SQLite answers PRAGMA main.schema_version. A write to the file reloads the catalog only when the token moved; a write that changes rows reloads nothing. The reload uses refreshAfterWrite and reports whether it committed, so a failed reload is retried on the next write.
  • MCP connected clients (CPU-07): no 5 s loop while MCP runs. The Connected Clients pane refreshes the list while it is on screen, and the list publishes only when it changes. Before: 1 main-actor wakeup and 1 publish every 5 s while MCP is on. After: 0 with the pane closed.
  • Themes (CPU-08): bundled themes are read once. The user themes folder is watched with FSEvents instead of re-reading every theme on each activation. Each activation cost a 2.9 ms main-thread reload of 14 bundled files plus the user themes; now 0, and a folder change reloads only the user files (0.08 ms).
  • Linked folders, Git (CPU-09): switching back to TablePro no longer runs Git in folders already in a repository (5 processes per folder before, 0 after). Folders outside a repository still get one git rev-parse, so a repository made in a parent folder is found. A .gitignore edit or a new .git in the folder refreshes the badges.
  • Linked folders, index (CPU-10): a rescan reuses rows whose mtime and size match and writes only rows that differ. Events for non-SQL files are dropped before they reach the main actor. Header reads now run off the main actor. 2,000 SQL files: every rescan read 2,000 headers and rewrote every row in 157 ms; one file changed now reads 1 header and writes 1 row in 56 ms, and an unchanged folder reads and writes nothing and sends no update.
  • Server Dashboard (NET-04): polling pauses while the window is minimized, hidden, fully covered or out of its window, from NSWindow.occlusionState, and runs a pass when it shows again. Unchanged sessions, metrics, slow queries and errors no longer publish. Before: 3 catalog queries every 5 s while hidden (2,160/hour). After: 0.
  • SFTP download (NET-05): the read buffer follows the file size, 32 KiB to 2 MiB. libssh2 keeps four buffers of reads in flight (sftp_read, max_read_ahead = buffer_size * 4), so 32 KiB capped a download at 128 KiB per round trip. apple/scripts/probes/check-sftp-read-ahead.sh, 32 MB file, +50 ms round trip: 2.3 MB/s before, 19.8 MB/s after.
  • Snowflake (NET-06): the registry keys a session before building a connection, so a pooled connect no longer builds and leaks two delegate URLSessions. The sessions live in an owner that invalidates them when the last request lets go. The server version is read once per shared session: 1 SELECT CURRENT_VERSION() per pooled driver before, 0 after the first.
  • iCloud sync (NET-07): an activation that arrives before the running sync downloads is dropped, so launch runs 1 cycle instead of 2. After a download, activations wait 60 s and then run once, so a change from another Mac still arrives. A local edit or account change is never held.
  • Tests updated on purpose: the theme tests that posted didBecomeActiveNotification now reload or watch the folder (ThemeCatalogTests, ThemeEngineTests, ThemeCatalogSyncTests); SSHTunnelDeadlineMarginTests.acceptPollIsShort is gone with the poll.

Needs the maintainer

  • NET-07 holds activations for 60 s after a download. A shorter or longer interval is one constant (SyncCoordinator.activationInterval). Push-driven sync (CKDatabaseSubscription) needs the aps-environment entitlement on the App ID, which the app does not have.
  • CPU-06, relays: each relayed connection still polls every 500 ms. Removing it needs one transport reader per SSH session, because libssh2 drains other channels' packets into its queue without waking their poll (the multi-channel lost wakeup). The App Nap activity while a tunnel is open stays for the same reason.

Test plan

  • New: SyncCoordinatorActivationTests (an activation during the launch run adds no second download; held activations run once when the interval ends; a queued edit is not held), SchemaChangeTokenGateTests, SchemaRefreshAfterWriteTests.refreshAfterWriteReportsWhetherItCommitted, SQLiteSchemaChangeTokenTests, MCPServerClientListTests, ServerDashboardViewModelTests (hidden window stops polling), LinkedSQLFolderScannerTests, SQLFolderWatcherTests, FileSystemEventWatcherTests, LinkedFolderGitStatusStoreTests, SFTPDownloadChunkSizeTests, SSHListenerAcceptorTests.
  • Ran 443 Unit plan cases across the theme, sync, MCP, dashboard, linked folder, Git, SSH and schema refresh suites and SQLiteSchemaChangeTokenTests, SSHListenerAcceptorTests (5) under swift test, verify.sh plugins and abi (changed, kit 44, minimum 19). verify.sh ios was inconclusive: no iOS simulator runtime on this Mac, so the iOS job in CI is the check for the iOS SSHTunnel change.
  • Probes: check-sftp-read-ahead.sh for NET-05; a proc_pid_rusage wakeup count of a 200 ms poll loop against a DispatchSource on a listening socket for CPU-06.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant