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