Repository navigation
perf(runtime): reduce terminal ANSI scanning work - #27344
Conversation
📝 Walkthrough
Priority: ➖ Normal Merge Risk: 🔵 Low · up to Some terminal controls can leave stray characters in preview text, particularly when output arrives across chunks. The impact is narrow, but the boundary handling should be fixed before merging. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
088d43c1-7a00-4cc5-9ac0-cd5514aa5025
📒 Files selected for processing (2)
src/main/runtime/terminal-ansi-normalization.tssrc/main/runtime/terminal-vertical-control-scan.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
| return null | ||
| } | ||
| return { kind: 'other', endIndex: escapeIndex + 1 } | ||
| return escapeIndex + 1 |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Keep intermediate escape sequences open until their final byte.
For \x1b(Bx, this return ends the escape at (. normalizeTerminalChunk then emits Bx instead of x. If the chunk ends at \x1b(, normalization also drops the incomplete escape instead of carrying it in pendingAnsi. Scan intermediate bytes through a final byte, and return -1 when that byte has not arrived. Xterm documents ESC ( B as a character-set control. (invisible-island.net)
Adapted from outof-place normalization work in #27170. Preserve tail behavior while avoiding repeated CSI scans and newline-density regressions. Co-authored-by: outof-place <[email protected]>
5a3928f to
b29b5c1
Compare
Follow-up to #27344. isTerminalPreviewLineControl now gates on isTerminalPreviewLineControlFinal, so the normalizeTerminalChunk pre-filter cannot drop a final the predicate accepts. Add a test over every CSI final 0x40-0x7e and replace String.prototype.charCodeAt spies with behavior assertions.
ELI5
Fast agent output makes Orca spend time rebuilding terminal previews. This keeps exactly the same text while reducing that work.
What Changed
The terminal normalizer reads character codes and copies printable spans. It finds each escape's end once and parses parameters only for controls that affect preview rows. Existing tail limits, blocked-prompt checks and line splitting keep their behavior.
Why
Based on @outof-place's normalization work in #27170. During adversarial review, its newline splitting was 4.75 times slower on dense blank lines, and its retained cursor controls scanned their parameters twice. This narrower replacement keeps the material gain, removes the second scan, and uses one predicate for retained control kinds.
Correction (post-merge): an earlier version of this description claimed about 68% less tail-processing CPU for captured Codex output at 64 KiB chunks (and 12–16% for captured Claude output at 4 KiB chunks). The captured transcripts and replay harness behind those figures are not in the repo, so they cannot be reproduced and should not be relied on.
An independent benchmark (merge-base vs this PR, each bundled with esbuild and run in separate Node 24 processes) on synthetic Codex-style redraws measured about 17% less CPU end to end through tail processing, and about 30–35% less on normalization alone. These are tail-processing measurements, not whole-app CPU claims.
Linked Issue
Part of #27023; supersedes the normalization portion of #27170.
Visual Proof
N/A — terminal output and visible behavior are unchanged.
Testing
macOS, Node 24.20.0: 15 focused suites / 210 tests pass, including pending escape retention, tail redraw, blocked prompt indexing and active rule updates.
New regression tests preserve mixed Unicode/control spans across chunks and ensure retained cursor parameters are scanned once (that
charCodeAt-spy assertion was replaced with behavior tests in test(runtime): enforce ANSI preview-final pre-filter invariant #27376). The latter fails against perf(runtime): cut main's per-chunk terminal tail scanning cost #27170's original implementation (8,199 inspections versus the 4,107 bound).300,000 seeded mixed streaming chunks match main exactly, including malformed CSI, OSC/DCS, C0/C1, CRLF and lone surrogates. Captured Codex, Claude and OpenCode transcripts produce identical full tails and cursor/pending state at 11, 64, 1,024 and 65,536 code-unit chunk boundaries.
CPU measurements (not reproducible from the repo; see the correction under Why): separate baseline/change/baseline Node processes, 4 MiB captured-output replays, seven runs each, matching final-state hashes. Dense-newline processing stays on the existing code path; retained CSI parsing performs one scan.
Separate warmed adverse-input runs show no remaining throughput regression: retained-control streams 8.9% faster, long valid cursor parameters 7.2% faster, tiny controls 7.8% faster, single-character echo flat.
pnpm tc:node, focusedoxlint, formatting and the changed-code quality gate pass. Broader checks remain for CI.I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Review
One production file,
terminal-ansi-normalization.ts, plus two tests in the existing vertical-control suite. Check the single escape scan, preserved CRLF/control spans and shared retained-control predicate.Agent skill upstream boundary
Notes
Low merge risk; no negative user-facing trade-offs found. No new cache, timer, state, setting, wire format or retained allocation. The implementation is platform-independent and applies equally to local and SSH terminal output and folder workspaces. Linux/Windows were not run locally.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)