Merge branch 'kk/reftable-tombstone-quadratic-fix'

The performance of ref updates and reads using the 'reftable' backend
in the presence of many deletion tombstone records has been optimized
by removing the tombstone suppression flag from the merged iterator
and instead skipping tombstones at higher-level call sites where
iteration bounds are known.

* kk/reftable-tombstone-quadratic-fix:
  reftable: fix quadratic behavior in the presence of tombstones
  t/perf: add perf test for ref tombstone scenarios
main
Junio C Hamano 2026-07-19 10:42:19 -07:00
commit cc82eafc48
4 changed files with 92 additions and 12 deletions

View File

@ -84,7 +84,8 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
if (ret)
goto done;

if (strcmp(ref.refname, refname)) {
if (strcmp(ref.refname, refname) ||
reftable_ref_record_is_deletion(&ref)) {
ret = 1;
goto done;
}
@ -110,7 +111,6 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
oidread(oid, reftable_ref_record_val1(&ref),
&hash_algos[hash_id]);
} else {
/* We got a tombstone, which should not happen. */
BUG("unhandled reference value type %d", ref.value_type);
}

@ -652,6 +652,9 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
break;
}

if (iter->ref.value_type == REFTABLE_REF_DELETION)
continue;

if (iter->exclude_patterns && should_exclude_current_ref(iter))
continue;

@ -1532,6 +1535,8 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
ret = 0;
break;
}
if (reftable_log_record_is_deletion(&log))
continue;

ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
tombstone = &logs[logs_nr++];
@ -1929,6 +1934,8 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
ret = 0;
break;
}
if (reftable_log_record_is_deletion(&old_log))
continue;

free(old_log.refname);

@ -2061,6 +2068,9 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
if (iter->err)
break;

if (reftable_log_record_is_deletion(&iter->log))
continue;

/*
* We want the refnames that we have reflogs for, so we skip if
* we've already produced this name. This could be faster by
@ -2220,6 +2230,8 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store,
ret = 0;
break;
}
if (reftable_log_record_is_deletion(&log))
continue;

ret = yield_log_record(refs, &log, fn, cb_data);
if (ret)
@ -2272,6 +2284,10 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store,
ret = 0;
break;
}
if (reftable_log_record_is_deletion(&log)) {
reftable_log_record_release(&log);
continue;
}

ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
logs[logs_nr++] = log;
@ -2318,18 +2334,26 @@ static int reftable_be_reflog_exists(struct ref_store *ref_store,
goto done;

/*
* Check whether we get at least one log record for the given ref name.
* If so, the reflog exists, otherwise it doesn't.
* Check whether we get at least one non-deleted log record for the
* given ref name. If so, the reflog exists, otherwise it doesn't.
*/
ret = reftable_iterator_next_log(&it, &log);
if (ret < 0)
goto done;
if (ret > 0) {
ret = 0;
goto done;
while (1) {
ret = reftable_iterator_next_log(&it, &log);
if (ret < 0)
goto done;
if (ret > 0) {
ret = 0;
goto done;
}
if (strcmp(log.refname, refname)) {
ret = 0;
goto done;
}
if (!reftable_log_record_is_deletion(&log))
break;
}

ret = strcmp(log.refname, refname) == 0;
ret = 1;

done:
reftable_iterator_destroy(&it);
@ -2442,6 +2466,8 @@ static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_da
ret = 0;
break;
}
if (reftable_log_record_is_deletion(&log))
continue;

tombstone.refname = (char *)arg->refname;
tombstone.value_type = REFTABLE_LOG_DELETION;
@ -2625,6 +2651,10 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
reftable_log_record_release(&log);
break;
}
if (reftable_log_record_is_deletion(&log)) {
reftable_log_record_release(&log);
continue;
}

oidread(&old_oid, log.value.update.old_hash,
ref_store->repo->hash_algo);
@ -2791,6 +2821,8 @@ static int reftable_be_fsck(struct ref_store *ref_store, struct fsck_options *o,
report.path = refname.buf;

switch (ref.value_type) {
case REFTABLE_REF_DELETION:
continue;
case REFTABLE_REF_VAL1:
case REFTABLE_REF_VAL2: {
struct object_id oid;

View File

@ -42,6 +42,8 @@ struct reftable_stack_options {
*/
void (*on_reload)(void *payload);
void *on_reload_payload;

int suppress_deletions;
};

/* open a new reftable stack. The tables along with the table list will be

View File

@ -337,7 +337,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
/* Update the stack to point to the new tables. */
if (st->merged)
reftable_merged_table_free(st->merged);
new_merged->suppress_deletions = 1;
new_merged->suppress_deletions = st->opts.suppress_deletions;
st->merged = new_merged;

if (st->tables)

View File

@ -0,0 +1,46 @@
#!/bin/sh

test_description="Tests performance of ref operations with many tombstones"

. ./perf-lib.sh

test_expect_success "setup" '
git init --ref-format=reftable repo &&
blob=$(echo foo | git -C repo hash-object -w --stdin) &&
for i in $(test_seq 8000)
do
printf "create refs/tags/tag-%d %s\n" "$i" "$blob" ||
return 1
done >repo/input &&
git -C repo update-ref --stdin <repo/input &&
git -C repo for-each-ref --format="delete %(refname)" |
git -C repo update-ref --stdin
'

test_perf "recreate refs after mass delete" '
git -C repo update-ref --stdin <repo/input &&
git -C repo for-each-ref --format="delete %(refname)" |
git -C repo update-ref --stdin
'

test_expect_success "setup asymmetric" '
git init --ref-format=reftable repo2 &&
blob=$(echo foo | git -C repo2 hash-object -w --stdin) &&
for i in $(test_seq 8000)
do
printf "create refs/tags/old-%d %s\n" "$i" "$blob" ||
return 1
done >repo2/input-old &&
sed "s/old-/new-/" <repo2/input-old >repo2/input-new &&
git -C repo2 update-ref --stdin <repo2/input-old &&
git -C repo2 for-each-ref --format="delete %(refname)" |
git -C repo2 update-ref --stdin
'

test_perf "create new refs after deleting differently-named refs" '
git -C repo2 update-ref --stdin <repo2/input-new &&
git -C repo2 for-each-ref --format="delete %(refname)" refs/tags/ |
git -C repo2 update-ref --stdin
'

test_done