Repository navigation
perf(main): stop allocating a debug snapshot on every PTY delivery event - #7541
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 5c5f958757fccd3d66b89bbd0a3f7f90d8b4b2ee and ff87d90. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughIn 🚥 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/ipc/pty.ts (1)
1437-1457: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCorrect, but the pending-chars loop is now duplicated with
readCurrentPtyRendererDeliveryDebugSnapshot.The inline peak computation here is semantically identical to the same loop in
readCurrentPtyRendererDeliveryDebugSnapshot()(Lines 1413-1419). Since the PR's stated invariant is that peak values stay byte-identical between the two, having the loop duplicated means a future edit to one path but not the other silently breaks that invariant on a hot path.Consider extracting a small shared helper, e.g.
computePendingPeaks(): { pendingChars: number; maxPendingCharsByPty: number }, used by both functions to guarantee they can't drift.♻️ Proposed refactor
+ function computePendingPeaks(): { pendingChars: number; maxPendingCharsByPty: number } { + let pendingChars = 0 + let maxPendingCharsByPty = 0 + for (const pending of pendingData.values()) { + const chars = pending.data.length + pendingChars += chars + maxPendingCharsByPty = Math.max(maxPendingCharsByPty, chars) + } + return { pendingChars, maxPendingCharsByPty } + } + function readCurrentPtyRendererDeliveryDebugSnapshot(): PtyRendererDeliveryDebugSnapshot { - let pendingChars = 0 - let maxPendingCharsByPty = 0 - for (const pending of pendingData.values()) { - const chars = pending.data.length - pendingChars += chars - maxPendingCharsByPty = Math.max(maxPendingCharsByPty, chars) - } + const { pendingChars, maxPendingCharsByPty } = computePendingPeaks() return { pendingPtyCount: pendingData.size, ... } } function recordPtyRendererDeliveryPressure(): void { // Why: ... - let pendingChars = 0 - let maxPendingCharsByPty = 0 - for (const pending of pendingData.values()) { - const chars = pending.data.length - pendingChars += chars - maxPendingCharsByPty = Math.max(maxPendingCharsByPty, chars) - } + const { pendingChars, maxPendingCharsByPty } = computePendingPeaks() peakPendingChars = Math.max(peakPendingChars, pendingChars) ... }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 4b11e71e-baff-4891-b998-1dd102ac24d2
📥 Commits
Reviewing files that changed from the base of the PR and between 3b081c1 and 5c5f958757fccd3d66b89bbd0a3f7f90d8b4b2ee.
📒 Files selected for processing (1)
src/main/ipc/pty.ts
recordPtyRendererDeliveryPressure() ran on every PTY delivery event (per send, per flush, per onData append) and called readCurrentPtyRendererDeliveryDebugSnapshot(), which allocates a fresh 13-field object just to read four fields back out for the peak trackers. Those peaks feed diagnostics only. Compute the four peaks inline from the same sources instead, dropping the per-event object allocation. Peak values are byte-identical, so the debug snapshot getter and its tests are unchanged. Co-authored-by: Orca <[email protected]>
5c5f958 to
ff87d90
Compare
74 commits from main, including the STA-1278 ai-vault transcript perf work, terminal flicker fix (#7540), renderer V8 heap raise (#7531), scrollbar position preservation (#7472), and the per-delivery debug-snapshot allocation removal (#7541). Conflict resolutions (our structure, main's semantics): - agent-detection: kept the split-module barrel; grafted main's Gemini/Pi title-detection refinements (path-fragment false positives, legacy Pi synthetic titles) into agent-title-core, dropped the superseded PI_IDLE_PREFIX. - orca-runtime wait-blocked detection: kept the throttled scheduleWaitBlockedCheck (branch §1 optimization) and ported it to main's new cached TerminalTailWaitState API; main's per-chunk cached scan stays on the leaf mismatch path where it auto-merged. pty.tailWaitState is deliberately never populated on this branch (throttle owns the pty record), so the leaf mirror copy invalidates rather than shares — the fromTail exactness contract is never violated by stale state. - recordPtyRendererDeliveryPressure: took main's allocation-free peak update (#7541) and derived per-pty in-flight from this branch's cumulative sent/acked accounting instead of the removed rendererInFlightCharsByPty map. - rpc/methods/terminal detach: kept both sides — our flushAllAckPendingOutput and main's exitWaiterAbort release. - pty-connection: kept our single-policy handleCommandFinished (invariant) alongside main's new sampleVisiblePaneForegroundAgent (bodies were byte-identical where they overlapped). - e2e pressure scenarios: kept the Phase-4 hidden-gate contract assertions and the extracted pressure-script module; adopted main's MAX_WORST_KEY_LATENCY_UNDER_LOAD_MS budget. Also documents the renderer-pull delivery watchdog in the branch guide. Gates: typecheck clean; ipc/daemon/terminal-pane/pane-manager suites 4256 passed; both fuzz suites pass. Co-authored-by: Orca <[email protected]>
…ent (stablyai#7541) Co-authored-by: Orca <[email protected]>
Part of a fresh adversarially-verified perf/memory audit (10 finders → 3-skeptic refute-by-default verify → synthesis). Survived 3/3 skeptic votes, zero-behavior-change fix.
1. Description
recordPtyRendererDeliveryPressure()runs on every PTY delivery event — per send insendPtyDataToRenderer, per flush influshPendingData, and peronDataappend. Each call invokedreadCurrentPtyRendererDeliveryDebugSnapshot(), which allocates a fresh 13-field object (and walkspendingData+rendererInFlightCharsByPty), purely so fourpeak*fields could be read back out. Those peaks feed diagnostics only (getPtyRendererDeliveryDebugSnapshot, consumed exclusively by tests) and never affect delivery.This change computes the four peaks inline from the same sources, dropping the per-event object allocation. The two map walks — inherent to computing the max-per-PTY peaks — remain, and they're O(active PTYs) ≈ a handful.
readCurrentPtyRendererDeliveryDebugSnapshot()is untouched and still backs the debug getter when it's actually read.2. Undeniable evidence + measurable improvement
pty.test.tstests pass, including the 6 that assert exactpeak*values viagetPtyRendererDeliveryDebugSnapshot(). Because the peaks are computed from the identical sources, the values are byte-identical — the existing peak assertions are the regression guard.catof a large file, delivery events fire at display cadence per active PTY; each previously minted (and immediately discarded) a snapshot object. Attach an allocation profiler to the main process during an output flood and thereadCurrentPtyRendererDeliveryDebugSnapshotallocation frames disappear.3. ELI5
Every time a terminal delivered text, the app built a little 13-slot scorecard of debug stats, copied four numbers off it, and threw the scorecard away. We just read those four numbers directly now — no scorecard, no litter.
4. Trade-offs that regress the end experience
None. Peaks are diagnostics-only and remain byte-identical, so the debug tooling is unaffected and no product behavior changes. Honest magnitude: this is a small, steady garbage-reduction on a hot path, not a dramatic CPU win — the object is short-lived and cheap for V8's young-gen GC. It's real, free waste to remove, but I'd rank it minor rather than "OMFG."
Made with Orca 🐋