]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs/reftable: track last log record name via strbuf
authorPatrick Steinhardt <ps@pks.im>
Tue, 5 Mar 2024 12:11:20 +0000 (13:11 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 5 Mar 2024 17:10:07 +0000 (09:10 -0800)
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>
refs/reftable-backend.c

index 2de57c047a25abbd28c48a8966d061310dc9b64e..12960d93ff47c712be47527393c5df8c3aec24ea 100644 (file)
@@ -1575,7 +1575,7 @@ struct reftable_reflog_iterator {
        struct reftable_ref_store *refs;
        struct reftable_iterator iter;
        struct reftable_log_record log;
-       char *last_name;
+       struct strbuf last_name;
        int err;
 };
 
@@ -1594,15 +1594,15 @@ static int reftable_reflog_iterator_advance(struct ref_iterator *ref_iterator)
                 * we've already produced this name. This could be faster by
                 * seeking directly to reflog@update_index==0.
                 */
-               if (iter->last_name && !strcmp(iter->log.refname, iter->last_name))
+               if (!strcmp(iter->log.refname, iter->last_name.buf))
                        continue;
 
                if (check_refname_format(iter->log.refname,
                                         REFNAME_ALLOW_ONELEVEL))
                        continue;
 
-               free(iter->last_name);
-               iter->last_name = xstrdup(iter->log.refname);
+               strbuf_reset(&iter->last_name);
+               strbuf_addstr(&iter->last_name, iter->log.refname);
                iter->base.refname = iter->log.refname;
 
                break;
@@ -1635,7 +1635,7 @@ static int reftable_reflog_iterator_abort(struct ref_iterator *ref_iterator)
                (struct reftable_reflog_iterator *)ref_iterator;
        reftable_log_record_release(&iter->log);
        reftable_iterator_destroy(&iter->iter);
-       free(iter->last_name);
+       strbuf_release(&iter->last_name);
        free(iter);
        return ITER_DONE;
 }
@@ -1655,6 +1655,7 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl
 
        iter = xcalloc(1, sizeof(*iter));
        base_ref_iterator_init(&iter->base, &reftable_reflog_iterator_vtable);
+       strbuf_init(&iter->last_name, 0);
        iter->refs = refs;
 
        ret = refs->err;