Skip to content

Commit 7734d7f

Browse files
npitregitster
authored andcommitted
index-pack: smarter memory usage when appending objects
In the same spirit as commit 9892beb, let's avoid allocating the full buffer for the deflated data in write_compressed() in order to write it. Let's deflate and write the data in chunks instead to reduce memory usage. Signed-off-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ce4721 commit 7734d7f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

builtin-index-pack.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -669,25 +669,25 @@ static void parse_pack_objects(unsigned char *sha1)
669669
static int write_compressed(struct sha1file *f, void *in, unsigned int size)
670670
{
671671
z_stream stream;
672-
unsigned long maxsize;
673-
void *out;
672+
int status;
673+
unsigned char outbuf[4096];
674674

675675
memset(&stream, 0, sizeof(stream));
676676
deflateInit(&stream, zlib_compression_level);
677-
maxsize = deflateBound(&stream, size);
678-
out = xmalloc(maxsize);
679-
680-
/* Compress it */
681677
stream.next_in = in;
682678
stream.avail_in = size;
683-
stream.next_out = out;
684-
stream.avail_out = maxsize;
685-
while (deflate(&stream, Z_FINISH) == Z_OK);
686-
deflateEnd(&stream);
687679

680+
do {
681+
stream.next_out = outbuf;
682+
stream.avail_out = sizeof(outbuf);
683+
status = deflate(&stream, Z_FINISH);
684+
sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
685+
} while (status == Z_OK);
686+
687+
if (status != Z_STREAM_END)
688+
die("unable to deflate appended object (%d)", status);
688689
size = stream.total_out;
689-
sha1write(f, out, size);
690-
free(out);
690+
deflateEnd(&stream);
691691
return size;
692692
}
693693

0 commit comments

Comments
 (0)