worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Per-invocation git subprocess fan-out: 15-25 spawns make wt list/remove/completion cost 4-8s on machines with per-exec endpoint-security tax

Open luiul opened this issue 1 month ago • 1 comments

Environment

  • macOS 26.6.2, Apple M2 Pro
  • wt v0.80.0 (Homebrew), git 2.56.0
  • Corporate endpoint security (SentinelOne) taxes every process exec ~0.25-0.3s, and execs serialize machine-wide: 20 concurrent git --version spawns take 5.3s wall time here (expected: under 0.5s)

What happens

wt list --format json on a single small repo spawns this sequence (traced with WORKTRUNK_VERBOSE=2, .git/wt/logs/trace.jsonl):

git config --list -z
git --version
git rev-parse --git-common-dir --is-inside-work-tree --show-toplevel --git-dir --symbolic-full-name HEAD
git rev-parse --show-toplevel
git worktree list --porcelain
git for-each-ref ... refs/heads/
git for-each-ref ... refs/remotes/
git log --no-walk ...
git sparse-checkout list
git --no-optional-locks status --porcelain --untracked-files=normal
git ls-files --others --exclude-standard -z
git add -u --sparse
git add --intent-to-add --sparse --pathspec-from-file=- --pathspec-file-nul
git write-tree
git diff-index ... (twice)

That is 15-25 spawns per invocation, mostly sequential phases. With the per-exec tax this is a hard floor:

command wall time
wt --version 0.26s
wt list --format json (one small repo) 4-5s best case; up to 47s under load
wt remove ~30s
shell completion (the clap dynamic completer runs the same discovery) 4-8s per call

Under concurrency it collapses completely. I run a dashboard that polls wt list --format json per known repo (~10 repos), and while that runs, every interactive wt command takes 20-60s, because every new spawn queues behind the concurrent ones.

Ask

Reduce the spawn count per invocation. Some ideas:

  1. Batch the read-only queries: for-each-ref accepts both refs/heads/ and refs/remotes/ in one call; the git --version capability check could be cached (it is constant between upgrades).
  2. Cache the expensive collection (status, divergence) across invocations with a short TTL, or offer a light list mode that skips dirty-status collection. A dashboard caller often needs only branches, paths, and worktree state.
  3. The trace shows long gaps between phases, so stages appear to depend on each other even when the queries are independent. If WORKTRUNK_MAX_CONCURRENT_COMMANDS bounds this, a higher default (or running the independent collection phases concurrently) would cut wall time on machines without the serialized tax.
  4. Shell completion only needs subcommand or branch names, but currently pays the full repo discovery.

Related, from the dashboard side: https://github.com/luiul/understory/issues/9

Happy to attach the full trace.jsonl if useful.

luiul avatar Oct 19 '26 16:10 luiul

This trace is useful. It shows that Worktrunk's latency work so far helps machines that can run git processes in parallel, but not machines like yours, where every exec costs about 0.3s and execs queue behind each other. I'm leaving this open as a design question for a maintainer and not opening a PR. Each lever below changes a product behavior, and none of them is a clear bug fix.

How the current design maps onto your four ideas

  • Concurrency (idea 3). WORKTRUNK_MAX_CONCURRENT_COMMANDS already defaults to 32 (src/shell_exec.rs), and the independent reads already run at the same time. git --version runs on its own thread next to the discovery rev-parse and config --list (src/main.rs), and completion runs its reads in two concurrent waves. On a machine where execs queue, raising the limit won't help. Only the number of forks matters there.
  • git --version (idea 1). It's cached for one process only (a OnceCell in src/git/version.rs). Caching it between runs would need a persisted key that notices a git upgrade.
  • for-each-ref (idea 1). refs/heads/ and refs/remotes/ are separate forks on purpose. The table needs only the local scan before it first draws, so the remote scan is pushed until after that (collect/mod.rs). --format json draws no table, so it could run both in one fork and save one exec.
  • Light list mode (idea 2). Most of your trace comes from the working-tree diff for each worktree (status, ls-files, the temp-index add/write-tree, and diff-index). [list] columns already drops that work from the table when the chosen columns don't need it (#3274). --format json ignores that setting and always fills every field. Applying a field selection to JSON is the natural place for a light mode. Whether that should be a new flag or should reuse columns is a maintainer decision.
  • Completion (idea 4). Subcommand completion has to discover the repo, because project aliases from the repo's config are offered as subcommands. Branch completion has to read refs. That leaves a handful of forks, which run concurrently. That suits most machines, but on yours they queue.

For your dashboard today: if it only needs branches, paths and worktree state, git worktree list --porcelain gives all three in one exec. It runs as the row source in wt list anyway.

worktrunk-bot avatar Oct 19 '26 16:10 worktrunk-bot