Commit

976644c3800220166f65e5e0fe1f7499a3923a6a

reftable/stack: avoid reloading the stack when already locked

When making modifications to the reftable stack, the stack obtains a
lock to the list file and removes the lock after the commit phase. Since
most operations reload the stack to ensure we have the latest state, any
branched operation during the locked phase could trigger a state reload.

To prevent data loss due to concurrent writes, state reload is necessary
right after obtaining the lock. But any reloads after that are just a
no-op. Now that the struct has access to the lock file status, simply
skip reloading if the lock is present.

Benchmarking with a fixed, non-symbolic target OID in the 'refs/tags/'
namespace (since it triggers a stack reload when checking if reflog
exists for the given tag name), shows a consistent 15-20% improvement
with these patches:

  refcount   master     patch     speedup
  --------   -------    -------   -------
  2,000       18.5 ms    16.6 ms   1.11x
  20,000     120.7 ms   102.8 ms   1.17x
  50,000     296.5 ms   247.1 ms   1.20x

We can also see the improvements in the number of syscall counts. On
master, the number of calls to `newfstatat()` grows linearly with the
number of refs created. With this patch, the number is now a constant:

  refcount   master   patch
  --------   ------   ------
  1,000      1,059       55
  5,000      5,059       55
  10,000     10,059      55
  20,000     20,059      55

Reported-by: Jeff King <peff@peff.net>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak <karthik.188@gmail.com> · 2026-08-24 09:31 UTC · committed by Junio C Hamano

Browse files
1 file changed, +12 −5
modified reftable/stack.c
@@ -553,14 +553,21 @@ out:
553 553
554 554 /*
555 555 * Check whether the given stack is up-to-date with what we have in memory.
556 + * If skip_if_locked is set skip stack reloading if the stack is currently
557 + * locked. Stack reloading must _not_ be skipped right after obtaining the
558 + * lock, to check for concurrent updates which may have happened.
559 + *
556 560 * Returns 0 if so, 1 if the stack is out-of-date or a negative error code
557 561 * otherwise.
558 562 */
559 - static int stack_uptodate(struct reftable_stack *st)
563 + static int stack_uptodate(struct reftable_stack *st, int skip_if_locked)
560 564 {
561 565 char **names = NULL;
562 566 int err;
563 567
568 + if (skip_if_locked && st->list_lock.fd != -1)
569 + return 0;
570 +
564 571 /*
565 572 * When we have cached stat information available then we use it to
566 573 * verify whether the file has been rewritten.
@@ -623,7 +630,7 @@ done:
623 630
624 631 int reftable_stack_reload(struct reftable_stack *st)
625 632 {
626 - int err = stack_uptodate(st);
633 + int err = stack_uptodate(st, 1);
627 634 if (err > 0)
628 635 return reftable_stack_reload_maybe_reuse(st, 1);
629 636 return err;
@@ -693,7 +700,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
693 700 }
694 701 }
695 702
696 - err = stack_uptodate(st);
703 + err = stack_uptodate(st, 0);
697 704 if (err < 0)
698 705 goto done;
699 706 if (err > 0) {
@@ -1200,7 +1207,7 @@ static int stack_compact_range(struct reftable_stack *st,
1200 1207 * we could check that relevant tables still exist. But for now it's
1201 1208 * good enough to just abort.
1202 1209 */
1203 - err = stack_uptodate(st);
1210 + err = stack_uptodate(st, 0);
1204 1211 if (err < 0)
1205 1212 goto done;
1206 1213 if (err > 0) {
@@ -1319,7 +1326,7 @@ static int stack_compact_range(struct reftable_stack *st,
1319 1326 * tables with our compacted version. If they don't, then we need to
1320 1327 * abort.
1321 1328 */
1322 - err = stack_uptodate(st);
1329 + err = stack_uptodate(st, 0);
1323 1330 if (err < 0)
1324 1331 goto done;
1325 1332 if (err > 0) {