refs/reftable: track last log record name via strbuf

The reflog iterator enumerates all reflogs known to a ref backend. In the "reftable" backend there is no way to list all existing reflogs directly. Instead, we have to iterate through all reflog entries and discard all those redundant entries for which we have already returned a reflog entry. This logic is implemented by tracking the last reflog name that we have emitted to the iterator's user. If the next log record has the same name we simply skip it until we find another record with a different refname. This last reflog name is stored in a simple C string, which requires us to free and reallocate it whenever we need to update the reflog name. Convert it to use a `struct strbuf` instead, which reduces the number of allocations. Before: HEAP SUMMARY: in use at exit: 13,473 bytes in 122 blocks total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated After: HEAP SUMMARY: in use at exit: 13,473 bytes in 122 blocks total heap usage: 68,485 allocs, 68,363 frees, 256,234,072 bytes allocated Note that even after this change we still allocate quite a lot of data, even though the number of allocations does not scale with the number of log records anymore. This remainder comes mostly from decompressing the log blocks, where we decompress each block into newly allocated memory. This will be addressed at a later point in time. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2024 at 13:11 UTC fcacc2b161b095c99dfd4e0b05dcc1ed8ca80a62
1 file changed +6 -5
refs/reftable-backend.c
+6 -5
@@ -1575,7 +1575,7 @@ struct reftable_reflog_iterator {
1575 struct reftable_ref_store *refs;
1576 struct reftable_iterator iter;
1577 struct reftable_log_record log;
1578 - char *last_name;
1578 + struct strbuf last_name;
1579 int err;
1580 };
1581
@@ -1594,15 +1594,15 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
1594 * we've already produced this name. This could be faster by
1595 * seeking directly to reflog@update_index==0.
1596 */
1597 - if (iter->last_name && !strcmp(iter->log.refname, iter->last_name))
1597 + if (!strcmp(iter->log.refname, iter->last_name.buf))
1598 continue;
1599
1600 if (check_refname_format(iter->log.refname,
1601 REFNAME_ALLOW_ONELEVEL))
1602 continue;
1603
1604 - free(iter->last_name);
1605 - iter->last_name = xstrdup(iter->log.refname);
1604 + strbuf_reset(&iter->last_name);
1605 + strbuf_addstr(&iter->last_name, iter->log.refname);
1606 iter->base.refname = iter->log.refname;
1607
1608 break;
@@ -1635,7 +1635,7 @@ static int reftable_reflog_iterator_abort(struct ref_iterator *ref_iterator)
1635 (struct reftable_reflog_iterator *)ref_iterator;
1636 reftable_log_record_release(&iter->log);
1637 reftable_iterator_destroy(&iter->iter);
1638 - free(iter->last_name);
1638 + strbuf_release(&iter->last_name);
1639 free(iter);
1640 return ITER_DONE;
1641 }
@@ -1655,6 +1655,7 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl
1655
1656 iter = xcalloc(1, sizeof(*iter));
1657 base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable);
1658 + strbuf_init(&iter->last_name, 0);
1659 iter->refs = refs;
1660
1661 ret = refs->err;