Skip to content

Commit fa74f32

Browse files
pks-tgitster
authored andcommitted
reftable/block: reuse compressed array
Similar to the preceding commit, let's reuse the `compressed` array that we use to store compressed data in. This results in a small reduction in memory allocations when writing many refs. Before: HEAP SUMMARY: in use at exit: 671,931 bytes in 151 blocks total heap usage: 22,620,528 allocs, 22,620,377 frees, 1,245,549,984 bytes allocated After: HEAP SUMMARY: in use at exit: 671,931 bytes in 151 blocks total heap usage: 22,618,257 allocs, 22,618,106 frees, 1,236,351,528 bytes allocated So while the reduction in allocations isn't really all that big, it's a low hanging fruit and thus there isn't much of a reason not to pick it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a155ab2 commit fa74f32

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

reftable/block.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ int block_writer_finish(struct block_writer *w)
150150
if (block_writer_type(w) == BLOCK_TYPE_LOG) {
151151
int block_header_skip = 4 + w->header_off;
152152
uLongf src_len = w->next - block_header_skip, compressed_len;
153-
unsigned char *compressed;
154153
int ret;
155154

156155
ret = deflateReset(w->zstream);
@@ -163,9 +162,9 @@ int block_writer_finish(struct block_writer *w)
163162
* is guaranteed to return `Z_STREAM_END`.
164163
*/
165164
compressed_len = deflateBound(w->zstream, src_len);
166-
REFTABLE_ALLOC_ARRAY(compressed, compressed_len);
165+
REFTABLE_ALLOC_GROW(w->compressed, compressed_len, w->compressed_cap);
167166

168-
w->zstream->next_out = compressed;
167+
w->zstream->next_out = w->compressed;
169168
w->zstream->avail_out = compressed_len;
170169
w->zstream->next_in = w->buf + block_header_skip;
171170
w->zstream->avail_in = src_len;
@@ -177,21 +176,17 @@ int block_writer_finish(struct block_writer *w)
177176
* guaranteed to succeed according to the zlib documentation.
178177
*/
179178
ret = deflate(w->zstream, Z_FINISH);
180-
if (ret != Z_STREAM_END) {
181-
reftable_free(compressed);
179+
if (ret != Z_STREAM_END)
182180
return REFTABLE_ZLIB_ERROR;
183-
}
184181

185182
/*
186183
* Overwrite the uncompressed data we have already written and
187184
* adjust the `next` pointer to point right after the
188185
* compressed data.
189186
*/
190-
memcpy(w->buf + block_header_skip, compressed,
187+
memcpy(w->buf + block_header_skip, w->compressed,
191188
w->zstream->total_out);
192189
w->next = w->zstream->total_out + block_header_skip;
193-
194-
reftable_free(compressed);
195190
}
196191

197192
return w->next;
@@ -450,6 +445,7 @@ void block_writer_release(struct block_writer *bw)
450445
deflateEnd(bw->zstream);
451446
FREE_AND_NULL(bw->zstream);
452447
FREE_AND_NULL(bw->restarts);
448+
FREE_AND_NULL(bw->compressed);
453449
strbuf_release(&bw->last_key);
454450
/* the block is not owned. */
455451
}

reftable/block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ license that can be found in the LICENSE file or at
1919
*/
2020
struct block_writer {
2121
z_stream *zstream;
22+
unsigned char *compressed;
23+
size_t compressed_cap;
24+
2225
uint8_t *buf;
2326
uint32_t block_size;
2427

0 commit comments

Comments
 (0)