Skip to content

Commit a046003

Browse files
spkrkagitster
authored andcommitted
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2ffd098 commit a046003

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
@@ -86,7 +86,8 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
8686
if (ret)
8787
goto done;
8888

89-
if (strcmp(ref.refname, refname)) {
89+
if (strcmp(ref.refname, refname) ||
90+
reftable_ref_record_is_deletion(&ref)) {
9091
ret = 1;
9192
goto done;
9293
}
@@ -112,7 +113,6 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
112113
oidread(oid, reftable_ref_record_val1(&ref),
113114
&hash_algos[hash_id]);
114115
} else {
115-
/* We got a tombstone, which should not happen. */
116116
BUG("unhandled reference value type %d", ref.value_type);
117117
}
118118

@@ -633,6 +633,9 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
633633
break;
634634
}
635635

636+
if (iter->ref.value_type == REFTABLE_REF_DELETION)
637+
continue;
638+
636639
if (iter->exclude_patterns && should_exclude_current_ref(iter))
637640
continue;
638641

@@ -1492,6 +1495,8 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
14921495
ret = 0;
14931496
break;
14941497
}
1498+
if (reftable_log_record_is_deletion(&log))
1499+
continue;
14951500

14961501
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
14971502
tombstone = &logs[logs_nr++];
@@ -1889,6 +1894,8 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
18891894
ret = 0;
18901895
break;
18911896
}
1897+
if (reftable_log_record_is_deletion(&old_log))
1898+
continue;
18921899

18931900
free(old_log.refname);
18941901

@@ -2019,6 +2026,9 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
20192026
if (iter->err)
20202027
break;
20212028

2029+
if (reftable_log_record_is_deletion(&iter->log))
2030+
continue;
2031+
20222032
/*
20232033
* We want the refnames that we have reflogs for, so we skip if
20242034
* we've already produced this name. This could be faster by
@@ -2178,6 +2188,8 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store,
21782188
ret = 0;
21792189
break;
21802190
}
2191+
if (reftable_log_record_is_deletion(&log))
2192+
continue;
21812193

21822194
ret = yield_log_record(refs, &log, fn, cb_data);
21832195
if (ret)
@@ -2230,6 +2242,10 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store,
22302242
ret = 0;
22312243
break;
22322244
}
2245+
if (reftable_log_record_is_deletion(&log)) {
2246+
reftable_log_record_release(&log);
2247+
continue;
2248+
}
22332249

22342250
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
22352251
logs[logs_nr++] = log;
@@ -2276,18 +2292,26 @@ static int reftable_be_reflog_exists(struct ref_store *ref_store,
22762292
goto done;
22772293

22782294
/*
2279-
* Check whether we get at least one log record for the given ref name.
2280-
* If so, the reflog exists, otherwise it doesn't.
2295+
* Check whether we get at least one non-deleted log record for the
2296+
* given ref name. If so, the reflog exists, otherwise it doesn't.
22812297
*/
2282-
ret = reftable_iterator_next_log(&it, &log);
2283-
if (ret < 0)
2284-
goto done;
2285-
if (ret > 0) {
2286-
ret = 0;
2287-
goto done;
2298+
while (1) {
2299+
ret = reftable_iterator_next_log(&it, &log);
2300+
if (ret < 0)
2301+
goto done;
2302+
if (ret > 0) {
2303+
ret = 0;
2304+
goto done;
2305+
}
2306+
if (strcmp(log.refname, refname)) {
2307+
ret = 0;
2308+
goto done;
2309+
}
2310+
if (!reftable_log_record_is_deletion(&log))
2311+
break;
22882312
}
22892313

2290-
ret = strcmp(log.refname, refname) == 0;
2314+
ret = 1;
22912315

22922316
done:
22932317
reftable_iterator_destroy(&it);
@@ -2399,6 +2423,8 @@ static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_da
23992423
ret = 0;
24002424
break;
24012425
}
2426+
if (reftable_log_record_is_deletion(&log))
2427+
continue;
24022428

24032429
tombstone.refname = (char *)arg->refname;
24042430
tombstone.value_type = REFTABLE_LOG_DELETION;
@@ -2580,6 +2606,10 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
25802606
reftable_log_record_release(&log);
25812607
break;
25822608
}
2609+
if (reftable_log_record_is_deletion(&log)) {
2610+
reftable_log_record_release(&log);
2611+
continue;
2612+
}
25832613

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

27482778
switch (ref.value_type) {
2779+
case REFTABLE_REF_DELETION:
2780+
continue;
27492781
case REFTABLE_REF_VAL1:
27502782
case REFTABLE_REF_VAL2: {
27512783
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)