Skip to content

Commit aa0e191

Browse files
committed
reftable: fix quadratic behavior when re-creating deleted refs
When many refs are deleted and then re-created, update-ref exhibits quadratic behavior. With 8000 refs deleted and re-created, the runtime is ~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 two code paths during ref creation: - 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 removing suppress_deletions from the merged iterator 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. This also requires adding deletion checks to the log iteration paths, since suppress_deletions applied to both ref and log iterators. Before: nr=1000 0.306s nr=2000 0.945s nr=4000 3.816s nr=8000 14.93s After: nr=1000 0.020s nr=2000 0.044s nr=4000 0.071s nr=8000 0.145s nr=16000 0.258s nr=32000 0.591s Reported-by: Jeff King <peff@peff.net> Signed-off-by: Kristofer Karlsson <krka@spotify.com>
1 parent df93388 commit aa0e191

5 files changed

Lines changed: 66 additions & 27 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/merged.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct merged_iter {
2626
struct merged_subiter *subiters;
2727
struct merged_iter_pqueue pq;
2828
size_t subiters_len;
29-
int suppress_deletions;
3029
ssize_t advance_index;
3130
};
3231

@@ -166,15 +165,7 @@ static int merged_iter_seek_void(void *it, struct reftable_record *want)
166165

167166
static int merged_iter_next_void(void *p, struct reftable_record *rec)
168167
{
169-
struct merged_iter *mi = p;
170-
while (1) {
171-
int err = merged_iter_next_entry(mi, rec);
172-
if (err)
173-
return err;
174-
if (mi->suppress_deletions && reftable_record_is_deletion(rec))
175-
continue;
176-
return 0;
177-
}
168+
return merged_iter_next_entry(p, rec);
178169
}
179170

180171
static struct reftable_iterator_vtable merged_iter_vtable = {
@@ -278,7 +269,6 @@ int merged_table_init_iter(struct reftable_merged_table *mt,
278269
goto out;
279270
}
280271
mi->advance_index = -1;
281-
mi->suppress_deletions = mt->suppress_deletions;
282272
mi->subiters = subiters;
283273
mi->subiters_len = mt->tables_len;
284274

reftable/merged.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ struct reftable_merged_table {
1717
size_t tables_len;
1818
enum reftable_hash hash_id;
1919

20-
/* If unset, produce deletions. This is useful for compaction. For the
21-
* full stack, deletions should be produced. */
22-
int suppress_deletions;
23-
2420
uint64_t min;
2521
uint64_t max;
2622
};

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)

t/t0610-reftable-basics.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,4 +1163,26 @@ test_expect_success 'writes do not persist peeled value for invalid tags' '
11631163
)
11641164
'
11651165

1166+
test_expect_success 'delete and re-create refs with tombstones' '
1167+
test_when_finished "rm -rf repo" &&
1168+
git init repo &&
1169+
test_commit -C repo A &&
1170+
A=$(git -C repo rev-parse HEAD) &&
1171+
cat >input <<-EOF &&
1172+
create refs/tags/a $A
1173+
create refs/tags/b $A
1174+
create refs/tags/c $A
1175+
EOF
1176+
git -C repo update-ref --stdin <input &&
1177+
1178+
# delete all tags, leaving tombstones
1179+
git -C repo for-each-ref --format="delete %(refname)" refs/tags/ |
1180+
git -C repo update-ref --stdin &&
1181+
1182+
# re-create the same refs and verify they are visible
1183+
git -C repo update-ref --stdin <input &&
1184+
git -C repo tag -l >actual &&
1185+
test_line_count = 3 actual
1186+
'
1187+
11661188
test_done

0 commit comments

Comments
 (0)