Repository navigation
perf(runtime): cache parsed ps rows on POSIX so panes share one parse - #7518
Conversation
getProcessTableSnapshot deduped the ps fork (#6288/#6667) but cached only the raw stdout string on POSIX, so every concurrent agent pane re-ran parsePsRows over the identical output within each 500ms TTL window — O(M*P) redundant tokenization + row allocation. The Windows reader already caches parsed rows; this makes the POSIX default reader do the same by parsing inside the deduped scan and returning ProcessTableRow[]. Collapses the duplicate parsePsRows in the main and relay foreground resolvers into one shared parseProcessTableRows. Co-authored-by: Orca <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughChangesThis PR refactors process-table handling to use structured data instead of raw Sequence Diagram(s)See hidden artifact above for the snapshot caching flow diagram. Estimated code review effort: 3/5 (High complexity across type refactor touching two consumers, though changes are mechanical and covered by tests) Related issues: None specified Related PRs: None specified Suggested labels: refactor, process-management Suggested reviewers: None specified Poem: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Co-authored-by: Orca <[email protected]>
…stablyai#7518) getProcessTableSnapshot deduped the ps fork (stablyai#6288/stablyai#6667) but cached only the raw stdout string on POSIX, so every concurrent agent pane re-ran parsePsRows over the identical output within each 500ms TTL window — O(M*P) redundant tokenization + row allocation. The Windows reader already caches parsed rows; this makes the POSIX default reader do the same by parsing inside the deduped scan and returning ProcessTableRow[]. Collapses the duplicate parsePsRows in the main and relay foreground resolvers into one shared parseProcessTableRows. Co-authored-by: Orca <[email protected]>
(stablyai#7529) Co-authored-by: Orca <[email protected]>
1. Description
getProcessTableSnapshot()is the shared, deduped process-table scan behind agent foreground-process inspection (the 750ms/2000ms per-pane cadence, plus liveness reconcile and daemon session enrichment). The earlier #6288/#6667 fix collapsed the concurrentpsforks into one scan behind a 500ms TTL — but on POSIX it caches only the raw stdout string. Each of the M concurrent panes that pull that one cached string then re-runs its ownparsePsRows(stdout)+collectDescendants(...)within the same TTL window, re-tokenizing byte-identical output and re-allocating a row object per process, every window.The Windows path already avoids this:
windows-foreground-process-rows.tsinstantiatescreateProcessTableSnapshotReader<WindowsProcessRow[]>to cache parsed rows. This PR makes the POSIX default reader symmetric — it parses inside the deduped scan and returnsProcessTableRow[], so all callers in a TTL window share one parse. It also collapses the two duplicatedparsePsRowsimplementations (mainagent-foreground-process.ts+ relaypty-shell-utils.ts) into a single sharedparseProcessTableRows.2. Evidence
Before:
getProcessTableSnapshot(): Promise<string>→ each caller doesconst rows = parsePsRows(stdout). With M panes and P host processes, that's M × (P-line regex tokenization + P row allocations) per 500ms window over identical input.After:
getProcessTableSnapshot(): Promise<ProcessTableRow[]>→ the parse runs once inside the deduped scan; callers consume the shared array. Work drops from O(M×P) → O(P) parse per window, independent of pane count.New tests in
process-table-snapshot.test.ts:Full affected suite green —
process-table-snapshot,agent-foreground-process,agent-foreground-process-ps-scan-volume,pty-shell-utils: 40 tests passed. Typecheck (tsgo -p config/tsconfig.node.json) EXIT 0; oxlint/oxfmt clean. Theps-scan-volume test still asserts the fork count is unchanged (the dedup contract is preserved; only per-caller parsing is removed).Micro-benchmark of the removed work (parse of a ~400-process
pstable): ~0.15 ms/parse. On a shared SSH relay with 8 active agent panes that is ~8 × 0.15 ms ≈ 1.2 ms → 0.15 ms per 500 ms window of pure re-tokenization + the matching per-row GC pressure eliminated. Small in absolute terms, but it scales with pane count and is exactly the shared-relay idle-CPU case #6288 targeted.3. ELI5
Imagine everyone in the office needs today's phone directory. We already fixed it so we print one copy instead of everyone printing their own (that was the expensive part). But then each person still re-typed the entire printout into their own spreadsheet by hand, even though the printout is identical for all of them. This change types it up once and hands everyone the same finished spreadsheet. Windows already worked this way; now Mac/Linux do too.
4. Trade-offs / behavior that could regress
/\r?\n/split (the main path previously split on\nonly; POSIXpsemits no CR, so output is identical).ProcessTableRow[]instance. Both consumers only read it (.find,collectDescendantsbuilds its own index; the{...row}spread incollectDescendantscopies before addingdepth) — no caller mutates the rows, so sharing is safe. If a future caller mutates the array it would affect others; the rows should be treated as read-only.getProcessTableSnapshot(string → rows) is internal — only the two foreground resolvers consume it, both updated here. The genericcreateProcessTableSnapshotReaderfactory and its tests are untouched.Made with Orca 🐋