]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/block: reuse compressed array
authorPatrick Steinhardt <ps@pks.im>
Mon, 8 Apr 2024 12:24:40 +0000 (14:24 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 9 Apr 2024 00:01:42 +0000 (17:01 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
reftable/block.c
reftable/block.h

index 9129305515efe8c5296b4dc9e5fdcd64e2f451b5..f190c05520c5d2147fee3069560d8b3039e069d4 100644 (file)
@@ -150,7 +150,6 @@ int block_writer_finish(struct block_writer *w)
        if (block_writer_type(w) == BLOCK_TYPE_LOG) {
                int block_header_skip = 4 + w->header_off;
                uLongf src_len = w->next - block_header_skip, compressed_len;
-               unsigned char *compressed;
                int ret;
 
                ret = deflateReset(w->zstream);
@@ -163,9 +162,9 @@ int block_writer_finish(struct block_writer *w)
                 * is guaranteed to return `Z_STREAM_END`.
                 */
                compressed_len = deflateBound(w->zstream, src_len);
-               REFTABLE_ALLOC_ARRAY(compressed, compressed_len);
+               REFTABLE_ALLOC_GROW(w->compressed, compressed_len, w->compressed_cap);
 
-               w->zstream->next_out = compressed;
+               w->zstream->next_out = w->compressed;
                w->zstream->avail_out = compressed_len;
                w->zstream->next_in = w->buf + block_header_skip;
                w->zstream->avail_in = src_len;
@@ -177,21 +176,17 @@ int block_writer_finish(struct block_writer *w)
                 * guaranteed to succeed according to the zlib documentation.
                 */
                ret = deflate(w->zstream, Z_FINISH);
-               if (ret != Z_STREAM_END) {
-                       reftable_free(compressed);
+               if (ret != Z_STREAM_END)
                        return REFTABLE_ZLIB_ERROR;
-               }
 
                /*
                 * Overwrite the uncompressed data we have already written and
                 * adjust the `next` pointer to point right after the
                 * compressed data.
                 */
-               memcpy(w->buf + block_header_skip, compressed,
+               memcpy(w->buf + block_header_skip, w->compressed,
                       w->zstream->total_out);
                w->next = w->zstream->total_out + block_header_skip;
-
-               reftable_free(compressed);
        }
 
        return w->next;
@@ -450,6 +445,7 @@ void block_writer_release(struct block_writer *bw)
        deflateEnd(bw->zstream);
        FREE_AND_NULL(bw->zstream);
        FREE_AND_NULL(bw->restarts);
+       FREE_AND_NULL(bw->compressed);
        strbuf_release(&bw->last_key);
        /* the block is not owned. */
 }
index 1375957fc877e606e0fe1015000fef99439e961f..657498014c7827c9d22839f263e4468ab8f041fb 100644 (file)
@@ -19,6 +19,9 @@ https://developers.google.com/open-source/licenses/bsd
  */
 struct block_writer {
        z_stream *zstream;
+       unsigned char *compressed;
+       size_t compressed_cap;
+
        uint8_t *buf;
        uint32_t block_size;