worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

`wt step prune` seems very slow

Open ortonomy opened this issue 8 months ago • 13 comments

Image

willing to accept that if there are calls to github under the hood - it might be being affected by high latency to github from my network, but this call took > ~3 minutes to prune a single tree.

This seems excessive.

ortonomy avatar Apr 02 '26 07:04 ortonomy

Thanks for reporting this! I investigated the wt step prune implementation and there are no GitHub API calls involved — the slowness is entirely from local git operations.

The root cause is that step prune checks whether each branch is integrated into the default branch sequentially. Each integration check runs through a cascade of git operations (same-commit → ancestor → added-changes → trees-match → merge-tree simulation → patch-id). The expensive ones are git merge-tree and git patch-id, which can take 500ms–2s each in large repositories. With multiple branches that don't pass the cheap checks, this compounds quickly.

By contrast, wt list already parallelizes these same integration checks via Rayon, so it doesn't have this problem.

Could you share how many worktrees/local branches you have? (wt list --format=json | jq length and git branch | wc -l) — that would help quantify the scaling issue. Also, wt --version would be helpful.

A maintainer will take a look at parallelizing the integration checks in step prune.

worktrunk-bot avatar Apr 02 '26 07:04 worktrunk-bot

3 worktrees

Image
wt list --format=json | jq length
4
``

4 if you include `master`

ortonomy avatar Apr 02 '26 07:04 ortonomy

Thanks for the details! With only 4 worktrees, the worktree loop itself shouldn't be the bottleneck. However, step prune also scans all local branches (not just worktrees) for orphan branches that are integrated into the default branch. Could you share the local branch count?

git branch | wc -l

If there are many local branches, each one gets a sequential integration check — and in a large repo, the expensive git operations (merge-tree, patch-id) can take seconds each, which would explain the 3-minute runtime.

Also, wt --version would help confirm which version you're on.

worktrunk-bot avatar Apr 02 '26 07:04 worktrunk-bot

git branch | wc -l
     112

112!

ortonomy avatar Apr 02 '26 07:04 ortonomy

That explains it — 112 branches, each getting a sequential integration check. In a large repo where branches don't pass the cheap checks (same-commit, ancestor), each one hits git merge-tree and git patch-id, which can take 500ms–2s each. At ~1–2s per branch across ~100+ non-trivial checks, that's easily 2–3 minutes.

The fix would be parallelizing the orphan branch scan the same way wt list already parallelizes its integration checks via Rayon. That should bring this down to seconds.

worktrunk-bot avatar Apr 02 '26 07:04 worktrunk-bot

wt --version
wt 0.33.0

ortonomy avatar Apr 02 '26 07:04 ortonomy

Thanks — v0.33.0 is the latest, so nothing version-specific going on here. The fix is parallelizing the orphan branch scan, same pattern wt list already uses.

worktrunk-bot avatar Apr 02 '26 07:04 worktrunk-bot

@worktrunk-bot good diagnosis

please could you draft a change to do this?

thanks for the issue @ortonomy

max-sixty avatar Apr 02 '26 16:04 max-sixty

Drafted a fix in #1890 — parallelizes both the worktree and orphan branch integration checks using rayon's par_iter(), matching the pattern wt list already uses. With 112 branches, this should bring the runtime from minutes down to seconds.

worktrunk-bot avatar Apr 02 '26 16:04 worktrunk-bot

I noticed an odd behavior on projects with many branches with wt step prune. It happens consistently for me but it might be hard to reproduce in an isolated test. Might be related to the PR for this issue.

Basically, I'll run prune, it will speed though a bunch of branches, then stall out, then I'll kill it with ctrl-c, then I'll do it again, will do a few more branches, then stall out, etc, etc. It looks like this.

If I let it run it will keep going, but the UX makes it seem like it got stuck.

Image

razor-x avatar May 12 '26 20:05 razor-x

Thanks for the report — that stall pattern is likely a separate bug, since #1890 only parallelized the integration checks. The removal phase that runs after them is still sequential per candidate, and pre-remove hooks run inline before each rename. A slow pre-remove hook in your config would look exactly like "speed through, then stall", with Ctrl-C dropping the in-flight hook so the next run picks up the rest.

If you can run wt -vv step prune once (writes .git/wt/logs/diagnostic.md with wt config show and a full git invocation trace) and gist the file, that should make it obvious. Worth opening a fresh issue with that attached — #1888 is closed, so a new one is easier to track.

worktrunk-bot avatar May 12 '26 21:05 worktrunk-bot

A slow pre-remove hook in your config would look exactly like "speed through, then stall", with Ctrl-C dropping the in-flight hook so the next run picks up the rest.

do we show the pre-remove hook outputs when we're running wt step prune?

max-sixty avatar May 13 '26 01:05 max-sixty

Yes — wt step prune runs pre-remove hooks through the same foreground path as wt remove. Each hook gets an ◎ Running pre-remove <label> @ <path> announce line, the bash-gutter expansion of the command, and the child's stdout/stderr streamed live (announce_command, called from execute_pre_remove_hooks_if_needed inside try_remove).

So if @razor-x's stall isn't preceded by a "Running pre-remove" line, the slow-hook hypothesis is out. The other plausible culprit is the integration-check tail: #1890 parallelized those via rayon, but the removal loop itself still walks results serially as they arrive on the channel, so one slow merge-tree + patch-id pair on a 112-branch repo can stall the next removal even though earlier ones flew through. wt -vv step prune would still pin it down — trace.log has per-git-invocation durations.

worktrunk-bot avatar May 13 '26 01:05 worktrunk-bot