From: Jeff King Date: Thu, 11 Jun 2026 08:55:26 GMT Subject: Re: git-diff in a worktree is an order of magnitude slower? Message-ID: <20260611085526.GL2191159@coredump.intra.peff.net> In-Reply-To: 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. I don't think that refreshing after making a worktree would help. The problem is one of timestamps: we just wrote an index (so it _should_ be totally up to date), but we err on the side of caution for some entries because the file timestamps and the index timestamp are the same. So what makes it "work" is that one second passed between writing those files and running "update-index". If you ran it from the worktree command automatically, it might all still happen in the same second. And of course, it's not just worktrees. Any time we checkout we may suffer from this problem, though initial clones and worktree creation will write more files than most. > At $DAYJOB, I _think_ some version of "git restore " ended up > also updating the index. Yep, that would make sense. Any index write (after the second-hand ticks) will make it go away, since it means updating the mtime of the index. > > 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