Skip to content

Commit d94ac23

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa248b8 commit d94ac23

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

reftable/block.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,21 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
110110
.buf = w->block + w->next,
111111
.len = w->block_size - w->next,
112112
};
113-
114113
struct string_view start = out;
115-
116114
int is_restart = 0;
117-
struct reftable_buf key = REFTABLE_BUF_INIT;
118115
int n = 0;
119116
int err;
120117

121-
err = reftable_record_key(rec, &key);
118+
err = reftable_record_key(rec, &w->buf);
122119
if (err < 0)
123120
goto done;
124121

125-
if (!key.len) {
122+
if (!w->buf.len) {
126123
err = REFTABLE_API_ERROR;
127124
goto done;
128125
}
129126

130-
n = reftable_encode_key(&is_restart, out, last, key,
127+
n = reftable_encode_key(&is_restart, out, last, w->buf,
131128
reftable_record_val_type(rec));
132129
if (n < 0) {
133130
err = -1;
@@ -143,9 +140,8 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
143140
string_view_consume(&out, n);
144141

145142
err = block_writer_register_restart(w, start.len - out.len, is_restart,
146-
&key);
143+
&w->buf);
147144
done:
148-
reftable_buf_release(&key);
149145
return err;
150146
}
151147

@@ -569,6 +565,7 @@ void block_writer_release(struct block_writer *bw)
569565
REFTABLE_FREE_AND_NULL(bw->zstream);
570566
REFTABLE_FREE_AND_NULL(bw->restarts);
571567
REFTABLE_FREE_AND_NULL(bw->compressed);
568+
reftable_buf_release(&bw->buf);
572569
reftable_buf_release(&bw->last_key);
573570
/* the block is not owned. */
574571
}

reftable/block.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct block_writer {
3939
uint32_t restart_cap;
4040

4141
struct reftable_buf last_key;
42+
struct reftable_buf buf;
4243
int entries;
4344
};
4445

0 commit comments

Comments
 (0)