git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] refs/reftable: don't write new table when expire is a no-op

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 18, 2026, 09:31 UTC
Message-ID
<20261118-pks-reflog-expire-performance-v1-2-10b9263566a8@pks.im>
In-Reply-To
<20261118-pks-reflog-expire-performance-v1-0-10b9263566a8@pks.im>

When executing `git reflog expire` we may of course end up not expiring any reflog entries because none of them are older than "gc.reflogExpire" or "gc.reflogExpireUnreachable". But even so, the reftable backend would end up writing a new table to disk anyway that contains basically an exact copy of the current reflog. And with the "--all" flag, that would happen once per existing reflog. This doesn't result in any correctness issues, but it is of course a lot of pointless busywork.

Skip writing and committing the new reftable in the case where there aren't any changes, which results in a nice speedup:

  Benchmark 1: git reflog expire --all (commit = HEAD~) (10 runs)
                  mean     ±       σ          min     …     max
    Wall Time    736.0 ms  ±    11.7 ms     720.0 ms  …   752.3 ms
    Memory       117.2 MiB ±     0.1 MiB    117.1 MiB …   117.3 MiB
  Benchmark 2: git reflog expire --all (commit = HEAD) (10 runs)
                  mean     ±       σ          min     …     max
    Wall Time    370.3 ms  ±     3.7 ms     363.4 ms  …   375.6 ms           -49.7%  (2.0x faster)
    Memory       116.8 MiB ±     0.2 MiB    116.5 MiB …   117.1 MiB           -0.3%
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs/reftable-backend.c    | 15 ++++++++++++---
 t/t0610-reftable-basics.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 4216a18684d..e2027bb9c02 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -2607,6 +2607,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 	struct strbuf referent = STRBUF_INIT;
 	uint8_t *last_hash = NULL;
 	size_t logs_nr = 0, logs_alloc = 0, i;
+	bool has_changes = false;
 	unsigned int type = 0;
 	int ret;
 
@@ -2696,17 +2697,25 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 				    logs[i].value.update.message,
 				    policy_cb_data)) {
 			dest->value_type = REFTABLE_LOG_DELETION;
+			has_changes = true;
 		} 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)) {
 				memcpy(dest->value.update.old_hash, last_hash, GIT_MAX_RAWSZ);
+				has_changes = true;
+			}
 			last_hash = logs[i].value.update.new_hash;
 		}
 	}
 
-	if (flags & EXPIRE_REFLOGS_UPDATE_REF && last_hash && !is_null_oid(&oid))
+	if (flags & EXPIRE_REFLOGS_UPDATE_REF && last_hash && !is_null_oid(&oid)) {
 		oidread(&arg.update_oid, last_hash, ref_store->repo->hash_algo);
+		if (!oideq(&arg.update_oid, &oid))
+			has_changes = true;
+	}
 
-	if ((flags & EXPIRE_REFLOGS_DRY_RUN)) {
+	if (!has_changes || (flags & EXPIRE_REFLOGS_DRY_RUN)) {
 		ret = 0;
 		goto done;
 	}
diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh
index a7027458395..d808f1887f6 100755
--- a/t/t0610-reftable-basics.sh
+++ b/t/t0610-reftable-basics.sh
@@ -912,6 +912,36 @@ test_expect_success 'reflog: expiry empties reflog' '
 	)
 '
 
+test_expect_success 'reflog: expiry without changes does not write tables' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+
+		test_commit A &&
+		test_commit B &&
+		test_commit C &&
+		cp .git/reftable/tables.list expect &&
+
+		git reflog expire --all --expire=never --expire-unreachable=never &&
+		test_cmp expect .git/reftable/tables.list &&
+
+		git reflog expire --all --rewrite \
+			--expire=never --expire-unreachable=never &&
+		test_cmp expect .git/reftable/tables.list &&
+
+		git reflog expire --all --updateref \
+			--expire=never --expire-unreachable=never &&
+		test_cmp expect .git/reftable/tables.list &&
+
+		git reflog expire --all --dry-run --expire=all &&
+		test_cmp expect .git/reftable/tables.list &&
+
+		git reflog expire --all --expire=all &&
+		! test_cmp expect .git/reftable/tables.list
+	)
+'
+
 test_expect_success 'reflog: can be deleted' '
 	test_when_finished "rm -rf repo" &&
 	git init repo &&
-- 
2.56.1.831.g720f3f5937.dirty
Previous: Patrick SteinhardtNext: Karthik Nayak
Message 7 of 18 in “Improve performance of reflog expiration”
  1. 0/4 Improve performance of reflog expirationPatrick Steinhardt, Nov 18, 2026
  2. 1/4 refs/reftable: don't write new table on dry-run expirePatrick Steinhardt, Nov 18, 2026
  3. Karthik NayakNov 18, 2026
  4. Patrick SteinhardtNov 18, 2026
  5. Junio C HamanoNov 18, 2026
  6. Patrick SteinhardtNov 19, 2026
  7. 2/4 refs/reftable: don't write new table when expire is a no-opPatrick Steinhardt, Nov 18, 2026
  8. Karthik NayakNov 18, 2026
  9. Patrick SteinhardtNov 18, 2026
  10. Toon ClaesNov 18, 2026
  11. Patrick SteinhardtNov 19, 2026
  12. 3/4 builtin/maintenance: only consider `gc.reflogExpire` for "--auto"Patrick Steinhardt, Nov 18, 2026
  13. Toon ClaesNov 18, 2026
  14. Patrick SteinhardtNov 19, 2026
  15. 4/4 Documentation/config: fix typo in "maintenance.reflog-expire.auto"Patrick Steinhardt, Nov 18, 2026
  16. Toon ClaesNov 18, 2026
  17. Toon ClaesNov 18, 2026
  18. Patrick SteinhardtNov 19, 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.