Repository navigation
perf(daemon): stop scanning every cell for OSC links that cannot exist - #20077
Conversation
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.
|
Warning Review limit reachedNext included review available in 32 minutes. View limit detailsLimit 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. Review configuration: ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
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 |
Visual proof: OSC-8 hyperlinks still workCaptured through the exact path this PR changes — Before park — link written, rendered underlined, hover resolves the URI: After cold-park and restore — still underlined, hover still resolves the same URI in the status bar ( Identical rendering either side of the restore, which is the point: this is a pure cost removal with no behavioural change. Behavioural coverageThe screenshots come from Green on this branch:
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Early-out for link-free buffers —
collectHeadlessOscLinkRangesnow 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 toline.getCell(col, scratchCell), removing a per-cellCellDataallocation across the entire scrollback. - New test suite —
headless-osc-link-ranges.test.tspins 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.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏



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
collectHeadlessOscLinkRangesruns on everygetSnapshot(), walking every cell of every row. Two costs, both avoidable:getOscLinkIdAtCellcalledline.getCell(col)without the reuse argument, so xterm allocated a freshCellDataper 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
SerializeAddonwalk, already emits OSC 8 inline during that single pass, and its headless typings document the overload we were not using:No package patching, no vendoring — the fix is entirely in our own file.
Fix
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
collectHeadlessOscLinkRangesover 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:
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:restoredLinkshalf of the guard fails this test and only this test.pnpm tcclean,check:code-quality:changed0 new findings, 1,863 daemon tests pass.