aboutsummaryrefslogtreecommitdiffstats
path: root/refs
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-03-05 13:11:20 +0100
committerJunio C Hamano <gitster@pobox.com>2024-03-05 09:10:07 -0800
commitfcacc2b161b095c99dfd4e0b05dcc1ed8ca80a62 (patch)
tree06c35ca25fae66ee676586f63d62c868b7c6197c /refs
parent7b8abc4d8cd428e4ce044e50f7980baacaccb761 (diff)
downloadgit-fcacc2b161b095c99dfd4e0b05dcc1ed8ca80a62.tar.gz
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>
Diffstat (limited to 'refs')
-rw-r--r--refs/reftable-backend.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 2de57c047a..12960d93ff 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -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;