Re: git-diff in a worktree is an order of magnitude slower?
- From
Jeff King <peff@peff.net>
- Date
- Jun 9, 2026, 00:11 UTC
- Message-ID
- <20260609001134.GD358144@coredump.intra.peff.net>
- In-Reply-To
- <CALnO6CADMJSixqYvL1Yo8qKX5rWhKQ+2OoSEuPUh-yoeK9TseQ@mail.gmail.com>
On Mon, Jun 08, 2026 at 07:36:45PM -0400, D. Ben Knoble wrote:
Show 5 quoted lines
> I'd like to report and offer to help fix what I view as a serious performance > bug: > > "git diff --no-ext-diff --quiet" performs about ~10x slower in a secondary > worktree than in the main worktree.
Hmm, I get the opposite effect: it is much faster in the worktree!
I did:
git clone /path/to/linux.git
git -C linux worktree add --detach ../wt
hyperfine -L dir linux,wt 'git -C {dir} diff'which yielded:
Benchmark 1: git -C linux diff
Time (mean ± σ): 188.9 ms ± 2.5 ms [User: 166.4 ms, System: 130.7 ms]
Range (min … max): 185.5 ms … 194.8 ms 16 runs
Benchmark 2: git -C wt diff
Time (mean ± σ): 20.0 ms ± 1.5 ms [User: 23.4 ms, System: 103.5 ms]
Range (min … max): 17.2 ms … 24.6 ms 132 runs
Summary
git -C wt diff ran
9.43 ± 0.71 times faster than git -C linux diffRunning:
perf record -g git -C wt --no-pager diff perf record -g git -C linux --no-pager diff perf diff
implies that the slow case is spending a lot more time computing sha1s. Which implies that the entries are stat dirty. And indeed, if I run:
git -C linux update-index --refresh
now they both take ~20ms.
I wonder if it's just a racy-git problem? Many files are written in the same second as the index, so they end up with the same mtimes, and we have to err on the side of checking the contents.
See Documentation/technical/racy-git.adoc for a larger discussion.
So it is not really about worktrees at all, but just "bad luck" in generating that initial index (that goes away next time you actually make an index update that rewrites the whole thing).
I'd have thought USE_NSEC was the default these days, but looks like it isn't? Try building with that and I'll bet it goes away entirely.
> PS I almost CC'd Peff and Patrick, whose names stood out in "git
> shortlog builtin/{worktree,diff}* object-file* | sort -t\( -k2 -g",
> but decided they'd be their own best judge of whether they can
> understand what's going on? :)You might be interested in "git shortlog -ns". :)
-Peff