Repository navigation
perf: reduce menu lag with many worktrees - #349
Merged
Merged
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…he terminal Ink wipes and redraws the whole terminal on every render once a frame is as tall as the terminal, which made arrow-key navigation slow with many worktrees. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Starting a git subprocess blocks the thread that starts it, and one poll cycle starts hundreds of them, which kept the thread that renders the menu busy and made arrow-key navigation lag with many worktrees. The polling now runs in a worker, with a same-thread fallback when no worker is available. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ink also wipes the terminal when the frame is exactly as tall as it, so reserve one more row. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ink calls string-width once per character of every rendered row. Under Bun (JavaScriptCore) each call costs several microseconds, so one menu frame took about 85ms. A frame now takes about 13ms in the same benchmark. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…igation-with-many-worktrees Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
widest-line 6 passes whole lines to string-width, and a line with any
non-ASCII character (every menu row has one) takes the per-cluster path
that runs a /v-flag \p{RGI_Emoji} regex on each cluster. Under Bun that
regex is slow enough to make a menu frame ~60ms instead of ~9ms.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merge risk
Note
Low - can be merged without reading the diff. Each path this touches (menu rendering, git status polling, binary build) was exercised; results are below, and a revert fully undoes the change. The rendered screen was checked by replaying the output through a terminal emulator.
Summary
string-widthpath that every menu line hits under Bun, and git process spawning on the main thread.main, key-to-screen latency during the fetch dropped from a median of about 130-240ms to about 30-45ms; some keys (about 400ms) are still slow during the fetch (see below).Problem
The menu became sluggish in repositories with many worktrees, especially while
[Fetching...]was shown. Measurement found three causes:clearTerminalon every frame.Menuwas underestimating its fixed rows (fixedRows), so the list was always too tall.widest-line6 passes whole lines tostring-width. A line with any non-ASCII character (every menu row has❯) missesstring-width's ASCII fast path and runs/^\p{RGI_Emoji}$/von every grapheme cluster, which is very slow in Bun's JavaScriptCore. In a 20s Bun CPU profile that regex took 13.4s of 27.9s sampled; a menu frame cost about 60ms instead of about 9ms.git worktree listthroughexecSync, which blocked the event loop (so Ink could not paint the cached list) while the git status polling started at the same moment kept the machine busy.spawn()blocks the calling thread) took 33% of the main thread's CPU time.What changed:
fixedRowsmatch the real layout (including the background-creation list), and keep the frame one row shorter than the terminal.string-width: abun patch(patches/string-width@8.3.0.patch) remembers the emoji check per grapheme cluster.npm installdoes not applypatchedDependencies, so running from source with npm does not get this speed-up; the released binaries include it.execinstead ofexecSync, so the cached list paints at once. Status polling now runs in aworker_thread(the worker is an extra entry point ofbun build --compile).git branch --show-currentis replaced by reading theHEADfile, cutting git spawns per worktree from 5 to 4 (it falls back to git if the file cannot be read).Not solved here: some slowness remains during the fetch. A control run with fetching disabled still showed stalls over 300ms, so part of it comes from the machine's load (load average 35-67 on 10 cores while measuring). Lowering git's priority with
niceand changing the concurrency limit made no improvement, so neither is included.Profiling logs (Ink frame time, terminal wipes, event loop lag) are kept behind
CCMANAGER_PROFILE_MENU=1; they do nothing otherwise.Verification
Run locally unless noted, on the merge of
mainat c6c6a41.bun run typecheck/bun run lint→ no errors.bun run test→ 71 files, 1084 passed, 5 skipped, including the new tests:getCurrentBranchequalsgit branch --show-currentfor a normal repo, a linked worktree (branch name with slashes), and a detached HEAD.App.test.tsxandMenu.test.tsxtime out intermittently on a loaded machine, and do so onmaintoo (Menu.test.tsxfailed in 6 of 8 runs onmain, 2 of 8 here).string-widthpatch: 25,616 comparisons (fixed inputs, 20,000 random strings, 500 menu-like lines; several option sets) give the same result as the unpatched 8.3.0, including thrown error types, on both Bun and Node (0 mismatches).Binary built with
bun build --compile, fetching through the worker → 120/120 statuses returned, max main-thread lag 3ms.Real terminal (95 rows), 100+ worktrees,
CCMANAGER_PROFILE_MENU=1log → every frameclearTerminal=false, normal framesink frame renderTimeabout 13ms (before:clearTerminal=true, about 85ms).Session → menu, compiled binary in a pseudo-terminal (101 worktrees, session started with a stand-in
claude, return key pressed, time until list rows are on screen):main2.6-3.0s, this branch 79-116ms.Screen contents after 5 ↓ presses, compiled binary run in a pseudo-terminal (60 and 40 rows) and replayed through a terminal emulator (
pyte) → one highlighted row, no duplicated rows (withincrementalRendering: truethe first row stayed highlighted and rows were duplicated, so it is left off).Menu frame cost in the compiled binary, driving a pseudo-terminal that holds ↑ for 16s (101 worktrees;
ink frame renderTimefrom the profiling log, median after the fetch finishes):mainplus this PR without thestring-widthpatch 61-251ms, with the patch 9-10ms.Key-to-screen latency, driving a pseudo-terminal that sends ↑ every 100ms (101 worktrees, 12s during the fetch, binaries built from
mainand from this branch alternating 3 times each; input is written to a pty, not typed on a real keyboard):main🤖 Generated with Claude Code