diff options
author | Patrick Steinhardt <ps@pks.im> | 2024-11-20 08:51:39 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2024-11-20 23:59:17 +0100 |
commit | d94ac23d3b9e223c6cacfdab7047d3d4efae3218 (patch) | |
tree | bd83198005a1b65c50d08d8ec94857b05eb0583c /reftable/block.c | |
parent | reftable/block: rename `block_writer::buf` variable (diff) | |
download | git-d94ac23d3b9e223c6cacfdab7047d3d4efae3218.tar.xz git-d94ac23d3b9e223c6cacfdab7047d3d4efae3218.zip |
reftable/block: optimize allocations by using scratch buffer
The block writer needs to compute the key for every record that one adds
to the writer. The buffer for this key is stored on the stack and thus
reallocated on every call to `block_writer_add()`, which is inefficient.
Refactor the code so that we store the buffer in the `block_writer`
struct itself so that we can reuse it. This reduces the number of
allocations when writing many refs, e.g. when migrating one million refs
from the "files" backend to the "reftable backend. Before this change:
HEAP SUMMARY:
in use at exit: 80,048 bytes in 49 blocks
total heap usage: 3,025,864 allocs, 3,025,815 frees, 372,746,291 bytes allocated
After this change:
HEAP SUMMARY:
in use at exit: 80,048 bytes in 49 blocks
total heap usage: 2,013,250 allocs, 2,013,201 frees, 347,543,583 bytes allocated
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'reftable/block.c')
-rw-r--r-- | reftable/block.c | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/reftable/block.c b/reftable/block.c index 3fa36c002a..1aa7e8cd3c 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -110,24 +110,21 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) .buf = w->block + w->next, .len = w->block_size - w->next, }; - struct string_view start = out; - int is_restart = 0; - struct reftable_buf key = REFTABLE_BUF_INIT; int n = 0; int err; - err = reftable_record_key(rec, &key); + err = reftable_record_key(rec, &w->buf); if (err < 0) goto done; - if (!key.len) { + if (!w->buf.len) { err = REFTABLE_API_ERROR; goto done; } - n = reftable_encode_key(&is_restart, out, last, key, + n = reftable_encode_key(&is_restart, out, last, w->buf, reftable_record_val_type(rec)); if (n < 0) { err = -1; @@ -143,9 +140,8 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) string_view_consume(&out, n); err = block_writer_register_restart(w, start.len - out.len, is_restart, - &key); + &w->buf); done: - reftable_buf_release(&key); return err; } @@ -569,6 +565,7 @@ void block_writer_release(struct block_writer *bw) REFTABLE_FREE_AND_NULL(bw->zstream); REFTABLE_FREE_AND_NULL(bw->restarts); REFTABLE_FREE_AND_NULL(bw->compressed); + reftable_buf_release(&bw->buf); reftable_buf_release(&bw->last_key); /* the block is not owned. */ } |