git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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 diff
Running:
  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
Previous: D. Ben KnobleNext: D. Ben Knoble
Message 2 of 19 in “git-diff in a worktree is an order of magnitude slower?”
  1. D. Ben KnobleJun 8, 2026
  2. Jeff KingJun 9, 2026
  3. D. Ben KnobleJun 9, 2026
  4. Jeff KingJun 11, 2026
  5. Junio C HamanoJun 11, 2026
  6. brian m. carlsonJun 11, 2026
  7. D. Ben KnobleJun 20, 2026
  8. Junio C HamanoJun 21, 2026
  9. Junio C HamanoJun 21, 2026
  10. Jeff KingJun 21, 2026
  11. Jeff KingJun 21, 2026
  12. Junio C HamanoJun 21, 2026
  13. Jeff KingJun 21, 2026
  14. Junio C HamanoJun 21, 2026
  15. Junio C HamanoJun 22, 2026
  16. Jeff KingJun 28, 2026
  17. Junio C HamanoJun 21, 2026
  18. Jeff KingJun 21, 2026
  19. D. Ben KnobleJul 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.