From: Patrick Steinhardt Date: Wed, 02 Dec 2026 08:45:47 GMT Subject: [PATCH v2 0/4] Improve performance of reflog expiration Message-ID: <20261202-pks-reflog-expire-performance-v2-0-17b2d6dd4867@pks.im> In-Reply-To: <20261118-pks-reflog-expire-performance-v1-0-10b9263566a8@pks.im> Hi, I recently noticed that a bunch of commands started hanging for almost a second after they're essentially done already. As it turns out, this was caused by auto-maintenance, which was blocking in the foreground for ~800ms. The root cause of this is that the auto-condition of "reflog-expire" doesn't properly evaluate reachability of reflog entries, so it instead considered every single reflog entry to point to an unreachable commit. Hence, all entries were evaluated against "gc.reflogExpireUnreachable" instead of "gc.reflogExpire", and thus the auto-condition significantly overcounts the number of entries that we'd prune. So every single run of git-maintenance(1) ultimately ended up spawning `git reflog expire`. This is fixed by the third patch in this series. But I was a bit surprised that a no-op `git reflog expire` took this long, so the first two patches optimize the reftable backend to be more efficient, too. In my copy of "git.git": Benchmark 1: git maintenance run --auto (commit = master) (10 runs) mean ± σ min … max Wall Time 853.2 ms ± 99.9 ms 570.9 ms … 901.9 ms Memory 117.5 MiB ± 0.3 MiB 117.2 MiB … 118.2 MiB Benchmark 2: git maintenance run --auto (commit = reftable-optimizations) (10 runs) mean ± σ min … max Wall Time 392.3 ms ± 2.3 ms 388.0 ms … 396.6 ms -54.0% (2.2x faster) Memory 117.2 MiB ± 0.2 MiB 116.8 MiB … 117.4 MiB -0.2% Benchmark 3: git maintenance run --auto (commit = auto-condition-optimizations) (10 runs) mean ± σ min … max Wall Time 18.6 ms ± 1.1 ms 17.4 ms … 20.9 ms -97.8% (45.8x faster) Memory 81.2 MiB ± 0.1 MiB 80.9 MiB … 81.4 MiB -30.9% (1.4x less) So yeah, it's a bit more efficient now. :) Changes in v2: - Consistently use `GIT_MAX_RAWSZ` to copy around the old hash. - Document that our dry-run write is not a correctness issue. - Link to v1: https://patch.msgid.link/20261118-pks-reflog-expire-performance-v1-0-10b9263566a8@pks.im Thanks! Patrick --- Patrick Steinhardt (4): refs/reftable: don't write new table on dry-run expire refs/reftable: don't write new table when expire is a no-op builtin/maintenance: only consider `gc.reflogExpire` for "--auto" Documentation/config: fix typo in "maintenance.reflog-expire.auto" Documentation/config/maintenance.adoc | 2 +- builtin/gc.c | 33 +++++++++++++++++++++------------ refs/reftable-backend.c | 20 ++++++++++++++++---- t/t0610-reftable-basics.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 17 deletions(-) Range-diff versus v1: 1: 1b1cf36b9c5 ! 1: 39bff1bfe99 refs/reftable: don't write new table on dry-run expire @@ Commit message But even though the expectation is that the on-disk state won't change, the reftable backend still ends up writing a new reftable to disk that - contains the changes. This is of course rather pointless, as we would - have already printed the entries that we're about to prune at that point - in time. + contains the changes. That table won't get committed of course, and we + unlink it again via `reftable_addition_destroy()`. But it is of course + rather pointless to write the data to disk only to unlink it again. Skip writing the data, which provides a small speedup: 2: f55ad0fb05a ! 2: bb9d601edd2 refs/reftable: don't write new table when expire is a no-op @@ refs/reftable-backend.c: static int reftable_be_reflog_expire(struct ref_store * } else { - if ((flags & EXPIRE_REFLOGS_REWRITE) && last_hash) + if ((flags & EXPIRE_REFLOGS_REWRITE) && last_hash && -+ memcmp(dest->value.update.old_hash, last_hash, -+ ref_store->repo->hash_algo->rawsz)) { ++ memcmp(dest->value.update.old_hash, last_hash, GIT_MAX_RAWSZ)) { memcpy(dest->value.update.old_hash, last_hash, GIT_MAX_RAWSZ); + has_changes = true; + } 3: 0f7364044ed = 3: 034f2e540af builtin/maintenance: only consider `gc.reflogExpire` for "--auto" 4: 60bc823f16e = 4: 300657125b5 Documentation/config: fix typo in "maintenance.reflog-expire.auto" --- base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4 change-id: 20261118-pks-reflog-expire-performance-e051b93f901a