@matheusbsbs / matheus-bagliano / commits / 7734d7f2f6
Fork 0

index-pack: smarter memory usage when appending objects

In the same spirit as commit 9892bebafe, 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 <nico@fluxnic.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Pitre committed Apr 12, 2010 at 16:50 UTC 7734d7f2f6e57973bce3d7b2af815ac737f4637c
1 file changed +12 -12
builtin-index-pack.c
+12 -12
@@ -669,25 +669,25 @@ static void parse_pack_objects(unsigned char *sha1)
669 static int write_compressed(struct sha1file *f, void *in, unsigned int size)
670 {
671 z_stream stream;
672 - unsigned long maxsize;
673 - void *out;
672 + int status;
673 + unsigned char outbuf[4096];
674
675 memset(&stream, 0, sizeof(stream));
676 deflateInit(&stream, zlib_compression_level);
677 - maxsize = deflateBound(&stream, size);
678 - out = xmalloc(maxsize);
679 -
680 - /* Compress it */
677 stream.next_in = in;
678 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);
679
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);
689 size = stream.total_out;
689 - sha1write(f, out, size);
690 - free(out);
690 + deflateEnd(&stream);
691 return size;
692 }
693