Skip to content

perf: reduce menu lag with many worktrees - #349

Merged
kbwo merged 13 commits into
mainfrom
perf/improve-menu-navigation-with-many-worktrees
Oct 27, 2026
Merged

kbwo merged 13 commits into
mainfrom
perf/improve-menu-navigation-with-many-worktrees

Conversation

@kbwo

@kbwo kbwo commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

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

  • With about 100 worktrees, every ↑↓ move in the menu lagged, most of all while the background git fetch ran.
  • This removes the causes found by measurement: a full-screen wipe on every frame, a slow string-width path that every menu line hits under Bun, and git process spawning on the main thread.
  • Compared with current 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:

  • When the frame height is not strictly less than the terminal rows, Ink skips differential rendering and wipes the terminal with clearTerminal on every frame. Menu was underestimating its fixed rows (fixedRows), so the list was always too tall.
  • widest-line 6 passes whole lines to string-width. A line with any non-ASCII character (every menu row has ❯) misses string-width's ASCII fast path and runs /^\p{RGI_Emoji}$/v on 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.
  • Returning from a session to the menu showed an empty screen for about 3-4s: the menu's worktree-list load ran git worktree list through execSync, 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.
  • About 650 git process spawns per poll cycle (spawn() blocks the calling thread) took 33% of the main thread's CPU time.

What changed:

  • Rendering: made fixedRows match the real layout (including the background-creation list), and keep the frame one row shorter than the terminal.
  • string-width: a bun patch (patches/string-width@8.3.0.patch) remembers the emoji check per grapheme cluster. npm install does not apply patchedDependencies, so running from source with npm does not get this speed-up; the released binaries include it.
  • git: the worktree list and default branch are loaded with the asynchronous exec instead of execSync, so the cached list paints at once. Status polling now runs in a worker_thread (the worker is an extra entry point of bun build --compile). git branch --show-current is replaced by reading the HEAD file, 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 nice and 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 main at c6c6a41.

  • bun run typecheck / bun run lint → no errors.

  • bun run test → 71 files, 1084 passed, 5 skipped, including the new tests:

    • getCurrentBranch equals git branch --show-current for a normal repo, a linked worktree (branch name with slashes), and a detached HEAD.
    • Status results sent to the worker are plain, structured-cloneable data; git error formatting and interruption also covered.
    • App.test.tsx and Menu.test.tsx time out intermittently on a loaded machine, and do so on main too (Menu.test.tsx failed in 6 of 8 runs on main, 2 of 8 here).
  • string-width patch: 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=1 log → every frame clearTerminal=false, normal frames ink frame renderTime about 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): main 2.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 (with incrementalRendering: true the 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 renderTime from the profiling log, median after the fetch finishes): main plus this PR without the string-width patch 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 main and from this branch alternating 3 times each; input is written to a pty, not typed on a real keyboard):

    main this PR
    median 241 / 243 / 128ms 33 / 44 / 39ms
    p90 1000 / 1000 / 514ms 413 / 478 / 392ms

🤖 Generated with Claude Code

kbwo and others added 13 commits October 25, 2026 13:02
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>
@kbwo
kbwo merged commit da27040 into main Oct 27, 2026
1 check passed
@kbwo
kbwo deleted the perf/improve-menu-navigation-with-many-worktrees branch October 27, 2026 00:03
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