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
D. Ben Knoble <ben.knoble@gmail.com>
Date
Jun 20, 2026, 15:57 UTC
Message-ID
<CALnO6CAx91kbJ84d6Ef655UNG0y0rhyknBRh6Y+0o7Xn-uVytQ@mail.gmail.com>
In-Reply-To
<20260611085526.GL2191159@coredump.intra.peff.net>
Coming back to index refreshing…
On Thu, Jun 11, 2026 at 4:55 AM Jeff King <peff@peff.net> wrote:
Show 21 quoted lines
>
> On Tue, Jun 09, 2026 at 01:15:11PM -0400, D. Ben Knoble wrote:
>
> > > Which implies that the entries are stat dirty. And indeed, if I run:
> > >
> > >   git -C linux update-index --refresh
> > >
> > > now they both take ~20ms.
> >
> > Ah, TIL about --refresh. I suppose it could be nice if "git diff"
> > updated the index in this way, but that sounds like a band-aid. Maybe
> > creating a fresh worktree should do the equivalent to make sure it's
> > considered "fresh"?
>
> I think "git diff" _does_ refresh the index internally (that's what
> takes so long!). I thought we then wrote out the result, but maybe we
> don't notice that it needs an update for some reason?
>
> I'm pretty sure "git status" does something similar, though running it
> in a slow working tree _does_ seem to make things faster. Maybe it's
> more aggressive about doing the update.
Thanks for the status pointer:
- cmd_status calls refresh_index and repo_updated_index_if_able
- those same calls are wrapped in refresh_index_quietly in builtin/diff.c

But the refresh_index_quietly call is guarded by (effectively; the actual code uses rev.diffopt.skip_stat_unmatch)

    1 < !!diff_auto_refresh_index

which dates to aecbf914c4 (git-diff: resurrect the traditional empty "diff --git" behaviour, 2007-08-31). On my system that comparison is false because the double-negation produces 1 (diff_auto_refresh_index=1 or the result of git_config_bool). Or at least, I don't see it get written to elsewhere (maybe that's supposed to happen in diff.c:diffcore_skip_stat_unmatch in this case and isn't? Idk. (Even dirtying the worktree as a hypothesis that only when a diff is found does the counter get bumped doesn't seem to work.)

So… has that conditional been quietly dead all this time? I can't imagine that's right, but…

Show 26 quoted lines
> > > 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.
> >
> > Thanks, I'll take a look.
> >
> > I can see on my Macbook that at least Meson does automatically set
> > either USE_ST_TIMESPEC or NO_NSEC automatically, but has no option to
> > enabled USE_NSEC and try that. I can probably write that patch (which
> > I'll do to test), and I can send it along with the "worktree add
> > should refresh the index" if you think that's an appropriate thing to
> > do.
>
> I think NO_NSEC is about not looking at the nsec fields of stat structs
> (since they might not exist). But we don't actually use them for stat
> matching unless USE_NSEC is set.
>
> I guess the distinction goes back to c06ff4908b (Record ns-timestamps if
> possible, but do not use it without USE_NSEC, 2009-03-04), which details
> some reasons you might not want USE_NSEC. Feels like it ought to be a
> run-time config, though, and maybe even something that gets auto-probed
> by git-init.
>
> Definitely not an area I have looked at much, though, nor thought hard
> about. So there might be gotchas. :)
>
> -Peff

Looks like adding USE_NSEC to my build did make the issue go away (the patch is short, and I'll send it anyway for folks to have the knob), but that now seems like a band-aid to me based on my confusion above.

-- 
D. Ben Knoble
Previous: brian m. carlsonNext: Junio C Hamano
Message 7 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.