From: Jonathan Nieder <jrnieder@gmail.com>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Shawn Pearce <spearce@spearce.org>
Subject: Re: [PATCH] Reduce zlib deflate code duplication
Date: Thu, 26 Aug 2010 21:37:54 -0500 [thread overview]
Message-ID: <20100827023754.GB23924@burratino> (raw)
In-Reply-To: <1282856164-5126-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy wrote:
> Most of deflation code is simply "given this buffer, just deflate
> it". Make a common routine and reuse it instead.
I like this idea. But:
> There is possibly a regression here.
Right.
> --- a/archive-zip.c
> +++ b/archive-zip.c
Looks good.
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -130,28 +130,10 @@ static void *get_delta(struct object_entry *entry)
>
> static unsigned long do_compress(void **pptr, unsigned long size)
> {
> - z_stream stream;
> - void *in, *out;
> - unsigned long maxsize;
> -
> - memset(&stream, 0, sizeof(stream));
> - deflateInit(&stream, pack_compression_level);
> - maxsize = deflateBound(&stream, size);
> -
> - in = *pptr;
> - out = xmalloc(maxsize);
> + void *out = git_deflate(*pptr, &size, pack_compression_level);
> + free(*pptr);
> *pptr = out;
On error, previously *pptr and size would reflect a truncated result,
but now *pptr is NULL and size is 0. Both results are silly.
It would be nicer if the caller (or do_compress itself) could check
for errors and report them.
> --- a/diff.c
> +++ b/diff.c
> @@ -1713,7 +1689,8 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, char
> * whichever is smaller.
> */
> delta = NULL;
> - deflated = deflate_it(two->ptr, two->size, &deflate_size);
> + deflate_size = two->size;
> + deflated = git_deflate(two->ptr, &deflate_size, zlib_compression_level);
[...]
> @@ -1721,7 +1698,7 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, char
> if (delta) {
> void *to_free = delta;
> orig_size = delta_size;
> - delta = deflate_it(delta, delta_size, &delta_size);
> + delta = git_deflate(delta, &delta_size, zlib_compression_level);
[...]
> --- a/fast-import.c
> +++ b/fast-import.c
> @@ -1025,24 +1025,13 @@ static int store_object(
[...]
> - while (deflate(&s, Z_FINISH) == Z_OK)
> - /* nothing */;
> - deflateEnd(&s);
> + compressed_size = delta ? deltalen : dat->len;
> + out = git_deflate(delta ? delta : dat->buf, &compressed_size,
> + pack_compression_level);
[...]
> @@ -1053,15 +1042,10 @@ static int store_object(
[...]
> - s.next_out = out = xrealloc(out, s.avail_out);
> - while (deflate(&s, Z_FINISH) == Z_OK)
> - /* nothing */;
> - deflateEnd(&s);
> + free(out);
> + compressed_size = dat->len;
> + out = git_deflate(dat->buf, &compressed_size,
> + pack_compression_level);
Likewise.
> --- a/remote-curl.c
> +++ b/remote-curl.c
> @@ -420,33 +420,10 @@ static int post_rpc(struct rpc_state *rpc)
> * we can try to deflate it ourselves, this may save on.
> * the transfer time.
> */
> - size_t size;
> - z_stream stream;
> - int ret;
> -
> - memset(&stream, 0, sizeof(stream));
> - ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
> - Z_DEFLATED, (15 + 16),
As Shawn mentioned, this requests gzip encoding with the default
window size.
> - ret = deflate(&stream, Z_FINISH);
> - if (ret != Z_STREAM_END)
> - die("cannot deflate request; zlib deflate error %d", ret);
> -
> - ret = deflateEnd(&stream);
> - if (ret != Z_OK)
> - die("cannot deflate request; zlib end error %d", ret);
> -
> - size = stream.total_out;
> + unsigned long size = rpc->len;
> + gzip_body = git_deflate(rpc->buf, &size, Z_BEST_COMPRESSION);
> + if (!gzip_body)
> + die("cannot deflate request; zlib deflate error");
The zlib error codes are very helpful for debugging, so I would be sad
to see them go.
Thanks,
Jonathan
next prev parent reply other threads:[~2010-08-27 2:39 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-26 20:56 [PATCH] Reduce zlib deflate code duplication Nguyễn Thái Ngọc Duy
2010-08-27 1:56 ` Shawn Pearce
2010-08-27 2:37 ` Jonathan Nieder [this message]
2010-08-30 1:46 ` Nguyễn Thái Ngọc Duy
2010-08-31 17:46 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100827023754.GB23924@burratino \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox