Skip to content

Commit c13f15d

Browse files
committed
reftable: fix quadratic behavior in the presence of tombstones
When many tombstones are present in a reftable, operations that need to look up or iterate over refs exhibit quadratic behavior. With 8000 refs deleted and re-created, update-ref takes ~15s, quadrupling for each doubling of input size. The root cause is the merged iterator's suppress_deletions flag. When set, merged_iter_next_void() silently consumes tombstone records in a tight internal loop before returning to the caller. This prevents higher-level code from checking iteration bounds (such as prefix or refname comparisons) until after all tombstones have been scanned. This affects any code path that seeks into a range containing tombstones, including: - refs_verify_refnames_available() seeks to "refs/tags/foo-1/" to check for D/F conflicts and must scan through all subsequent tombstones before the caller can see that they are past the prefix of interest. - reftable_backend_read_ref() seeks to a specific refname and must scan through all subsequent tombstones before returning "not found", because the merged iterator skips the matching tombstone and searches for the next live record. Fix this by no longer setting suppress_deletions on the stack's merged table and instead handling deletion records at each call site in the reftable backend, where prefix and refname bounds are available. Tombstones are now returned to callers, which skip them after their existing bounds checks. This allows iteration to terminate as soon as a tombstone past the relevant bound is encountered. The suppress_deletions flag and its logic in the merged iterator are retained for downstream users of the reftable library (e.g. libgit2). This also requires adding deletion checks to the log iteration paths, since suppress_deletions applied to both ref and log iterators. Both tests in p1401 go from ~14s to ~0.2s with this change. Reported-by: Jeff King <peff@peff.net> Signed-off-by: Kristofer Karlsson <krka@spotify.com>
1 parent 889d0d3 commit c13f15d

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

refs/reftable-backend.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
8484
if (ret)
8585
goto done;
8686

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

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

655+
if (iter->ref.value_type == REFTABLE_REF_DELETION)
656+
continue;
657+
655658
if (iter->exclude_patterns && should_exclude_current_ref(iter))
656659
continue;
657660

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

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

19331940
free(old_log.refname);
19341941

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

2071+
if (reftable_log_record_is_deletion(&iter->log))
2072+
continue;
2073+
20642074
/*
20652075
* We want the refnames that we have reflogs for, so we skip if
20662076
* 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,
22202230
ret = 0;
22212231
break;
22222232
}
2233+
if (reftable_log_record_is_deletion(&log))
2234+
continue;
22232235

22242236
ret = yield_log_record(refs, &log, fn, cb_data);
22252237
if (ret)
@@ -2272,6 +2284,10 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store,
22722284
ret = 0;
22732285
break;
22742286
}
2287+
if (reftable_log_record_is_deletion(&log)) {
2288+
reftable_log_record_release(&log);
2289+
continue;
2290+
}
22752291

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

23202336
/*
2321-
* Check whether we get at least one log record for the given ref name.
2322-
* If so, the reflog exists, otherwise it doesn't.
2337+
* Check whether we get at least one non-deleted log record for the
2338+
* given ref name. If so, the reflog exists, otherwise it doesn't.
23232339
*/
2324-
ret = reftable_iterator_next_log(&it, &log);
2325-
if (ret < 0)
2326-
goto done;
2327-
if (ret > 0) {
2328-
ret = 0;
2329-
goto done;
2340+
while (1) {
2341+
ret = reftable_iterator_next_log(&it, &log);
2342+
if (ret < 0)
2343+
goto done;
2344+
if (ret > 0) {
2345+
ret = 0;
2346+
goto done;
2347+
}
2348+
if (strcmp(log.refname, refname)) {
2349+
ret = 0;
2350+
goto done;
2351+
}
2352+
if (!reftable_log_record_is_deletion(&log))
2353+
break;
23302354
}
23312355

2332-
ret = strcmp(log.refname, refname) == 0;
2356+
ret = 1;
23332357

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

24462472
tombstone.refname = (char *)arg->refname;
24472473
tombstone.value_type = REFTABLE_LOG_DELETION;
@@ -2625,6 +2651,10 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
26252651
reftable_log_record_release(&log);
26262652
break;
26272653
}
2654+
if (reftable_log_record_is_deletion(&log)) {
2655+
reftable_log_record_release(&log);
2656+
continue;
2657+
}
26282658

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

27932823
switch (ref.value_type) {
2824+
case REFTABLE_REF_DELETION:
2825+
continue;
27942826
case REFTABLE_REF_VAL1:
27952827
case REFTABLE_REF_VAL2: {
27962828
struct object_id oid;

reftable/stack.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
337337
/* Update the stack to point to the new tables. */
338338
if (st->merged)
339339
reftable_merged_table_free(st->merged);
340-
new_merged->suppress_deletions = 1;
341340
st->merged = new_merged;
342341

343342
if (st->tables)

0 commit comments

Comments
 (0)