Skip to content

perf(daemon): stop scanning every cell for OSC links that cannot exist - #20077

Merged
nwparker merged 1 commit into
mainfrom
nwparker/osc-link-walk-allocation
Sep 11, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/osc-link-walk-allocation

Conversation

@nwparker

@nwparker nwparker commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​63 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​63
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​23 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19

ELI5

Every time Orca saves a terminal's contents, it also scanned every single character looking for clickable links — even in terminals that have never contained one. That scan created a throwaway object per character, so a long session paid for millions of them to find nothing.

Now it checks first whether the terminal has any links at all, and skips the scan when it doesn't.

Problem

collectHeadlessOscLinkRanges runs on every getSnapshot(), walking every cell of every row. Two costs, both avoidable:

  1. No early-out. A session that never emitted an OSC 8 hyperlink — the overwhelming majority — still walked the whole scrollback for a guaranteed-empty result.
  2. Per-cell allocation. getOscLinkIdAtCell called line.getCell(col) without the reuse argument, so xterm allocated a fresh CellData per cell. At 5000×200 that is a million allocations per snapshot.

This is squarely our bug, not an xterm one. xterm already reuses two alternating cells in its own SerializeAddon walk, already emits OSC 8 inline during that single pass, and its headless typings document the overload we were not using:

line.getCell(x, cell). Use this to avoid costly recreation…
— @xterm/headless typings, IBufferLine.getCell

No package patching, no vendoring — the fix is entirely in our own file.

Fix

  • Skip the scan when xterm's OSC link registry is empty and there are no restored ranges to map.
  • Reuse a single scratch cell across the walk.

The registry read is private (_oscLinkService._dataByLinkId), so it is typed optional: an xterm that renames it simply scans as before rather than breaking.

Proof

collectHeadlessOscLinkRanges over a 5000-row link-free buffer at 200 cols, agent-transcript-shaped content, same harness run back to back, median of 25 iterations:

The two halves are independent, so both cases improve. Same buffer shape, agent-transcript-shaped content, each arm run back to back in the same harness, median of 25 iterations:

5000-row buffer, 200 cols Before After
No hyperlinks (early-out fires) 43.85 ms 0.00 ms scan skipped
250 hyperlinks (early-out cannot fire) 13.49 ms 3.62 ms 3.7x, from cell reuse alone

Both arms of the second row find the same 250 links, so that is a pure speed-up rather than a behaviour change.

Note the link-free buffer was the slower of the two before the fix despite doing less work — with no links the walk touches and allocates for every cell. The allocation, not the scanning, was the dominant cost in both cases.

Where it lands: getSnapshot() runs on daemon reattach (the worktree-switch click path) and on the periodic checkpoint during an active agent session, where it repeats at full depth — so the saving compounds there rather than being a one-off.

Correctness

New headless-osc-link-ranges.test.ts, 5 cases, all of them aimed at the one way an early-out can be wrong — firing while a link is still reachable:

  • finds a link written into the buffer
  • returns nothing when none was ever emitted
  • still maps restored ranges when the buffer itself has no link — restored ranges are seeded without xterm registering anything, so an early-out keyed only on the registry would silently drop them. Verified load-bearing: dropping the restoredLinks half of the guard fails this test and only this test.
  • finds links deep in a 5000-row scrollback, not just the visible screen
  • keeps every distinct link when several are present

pnpm tc clean, check:code-quality:changed 0 new findings, 1,863 daemon tests pass.

collectHeadlessOscLinkRanges walks every cell of every row on each snapshot,
and called xterm's getCell without the reuse argument its own docs recommend,
so a link-free scrollback paid a CellData allocation per cell for a guaranteed
empty result. Skip the scan when xterm holds no OSC 8 registration, and reuse
one cell when it does.

Measured over a 5000-row link-free buffer at 200 cols, same harness back to
back, median of 25: 43.85ms -> 0.00ms.

This is our bug, not xterm's: xterm already reuses cells in its own serializer
and documents the getCell(x, cell) overload for exactly this.
@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 32 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: b8495178-c1b4-46f8-ac4c-6ad56817868d

📥 Commits

Reviewing files that changed from the base of the PR and between 22d1238 and efb3fe5.

📒 Files selected for processing (2)
  • src/main/daemon/headless-osc-link-ranges.test.ts
  • src/main/daemon/headless-osc-link-ranges.ts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker

Copy link
Copy Markdown
Contributor Author

Visual proof: OSC-8 hyperlinks still work

Captured through the exact path this PR changes — getSnapshot() → oscLinks — by cold-parking a terminal holding a hyperlink and revealing it again. The link is restored from the snapshot, so if the scan were wrong the link would come back dead.

Before park — link written, rendered underlined, hover resolves the URI:

proof-1-before-park.png

After cold-park and restore — still underlined, hover still resolves the same URI in the status bar (https://example.com/orca-osc8-…, "Click for actions, ⌘+click to open"):

proof-2-after-restore.png

Identical rendering either side of the restore, which is the point: this is a pure cost removal with no behavioural change.

Behavioural coverage

The screenshots come from terminal-osc8-cold-park-restore.spec.ts, which asserts more than the image shows — bufferType: 'alternate', serializedUri: true, underlined: true, matching uri both before park and after restore, then ⌘-clicks the restored link and verifies a browser tab opens at that URL.

Green on this branch:

  • 8 e2e specs — terminal-osc8-cold-park-restore, issue-12656-terminal-link-tooltip, terminal-link-click-ownership, golden-terminal-file-link
  • 15 OSC unit tests — terminal-snapshot-osc8-roundtrip, headless-emulator-restored-osc-links, headless-osc-link-ranges
  • 1,863 daemon tests, pnpm tc clean, quality gate 0 new findings

@nwparker
nwparker merged commit 94c2f96 into main Sep 11, 2026
10 checks passed

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Early-out for link-free buffers — collectHeadlessOscLinkRanges now returns early when xterm's OSC 8 registry is empty and no restored ranges need mapping, skipping the per-cell walk for sessions that never emitted a hyperlink.
  • Scratch-cell reuse during the walk — a single buffer.getNullCell() is handed to line.getCell(col, scratchCell), removing a per-cell CellData allocation across the entire scrollback.
  • New test suite — headless-osc-link-ranges.test.ts pins link discovery, the empty-buffer early-out, restored-range mapping with no registered links, deep-scrollback discovery, and multiple distinct links.

ℹ️ Empty-registry heuristic rests on a private xterm invariant

The early-out is safe only because xterm prunes _dataByLinkId strictly when a link's last line is trimmed from the scrollback (marker dispose), so an empty registry really does imply no reachable link. I verified this against the vendored source (OscLinkService._removeMarkerFromLink and the per-character addLineToLink in InputHandler.ts), and the scratch-cell reuse is safe (loadCell fully overwrites the target). The _dataByLinkId? typing already degrades to the old full-scan on a rename — but a future xterm that caps or LRU-prunes the map while links remain in the buffer would silently drop links with no guard. That is forward-looking rather than a defect in the current version, so this is informational; the existing getLinkData access already reaches into the same private service.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

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