Re: git-diff in a worktree is an order of magnitude slower?
- From
Jeff King <peff@peff.net>
- Date
- Jun 21, 2026, 21:28 UTC
- Message-ID
- <20260621212805.GB2297179@coredump.intra.peff.net>
- In-Reply-To
- <xmqqfr2f7iay.fsf@gitster.g>
On Sun, Jun 21, 2026 at 01:24:53PM -0700, Junio C Hamano wrote:
Show 14 quoted lines
> Jeff King <peff@peff.net> writes: > > > I don't know if any of this is really worth digging too far. This feels > > like a case we could do a bit better at, but I wonder how much it > > matters in practice. As soon as you do any index-refresh (including "git > > status"), the racy entries are cleared and everything is faster. It > > just seems kind of lame that we write out the initial working tree with > > so many racy entries. > > Yeah, We didn't want to stall for a full second back when we were > not using subsecond in anywhere, with nanosecond resolution > timestamps in place, we could delay writing the index file by 50 > milliseconds, nobody notices the delay, and raciness would go away, > perhaps?
Yes, though that implies comparing the index and file mtimes with nanosecond precision. We have that precision stored (at least when the system supports it) but I'm not sure if that comparison would run afoul of the reasons USE_NSEC was not the default in the first place.
I guess not? The problem there is that the nanosecond portion would sometimes get wiped if the entry was dropped from the kernel's in-memory cache. And then stat-matching would not work. But if we are talking about strictly asking "is this mtime later than that mtime", then I think the worst case is that we fall back to the current behavior.
But at the point that we are comparing nanoseconds, I don't think we even need to bother with the delay. It takes maybe 5 seconds to write out all of the linux.git files and then the final index. So ~20% of those files will have the same timestamp as the index. With nanosecond resolution, we'd expect that to drop by an order of a billion. Even if we get unlucky and have a single file with the same timestamp, that is not so bad.
The code to do the nanosecond compare is already there! But it's gated on USE_NSEC. So this (plus a bonus debugging trace ;) ):
diff --git a/read-cache.c b/read-cache.c index 21829102ae..f84159a060 100644 --- a/read-cache.c +++ b/read-cache.c @@ -356,14 +356,10 @@ static int is_racy_stat(const struct index_state *istate, const struct stat_data *sd) { return (istate->timestamp.sec && -#ifdef USE_NSEC /* nanosecond timestamped files can also be racy! */ (istate->timestamp.sec < sd->sd_mtime.sec || (istate->timestamp.sec == sd->sd_mtime.sec && istate->timestamp.nsec <= sd->sd_mtime.nsec)) -#else - istate->timestamp.sec <= sd->sd_mtime.sec -#endif ); } @@ -434,6 +430,7 @@ int ie_match_stat(struct index_state *istate, * carefully than others. */ if (!changed && is_racy_timestamp(istate, ce)) { + warning("%s is racy", ce->name); if (assume_racy_is_modified) changed |= DATA_CHANGED; else makes the problem go away. I'm not sure if I'm missing some case where we could be bitten by the problem that led to making USE_NSEC conditional, though. -Peff