aboutsummaryrefslogtreecommitdiff
path: root/deflate.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2023-09-18 21:17:00 -0700
committerMark Adler <madler@alumni.caltech.edu>2023-09-21 00:14:56 -0700
commitac8f12c97d1afd9bafa9c710f827d40a407d3266 (patch)
treecc8841032d996c2f5c41d75c7edf65558d2d90ce /deflate.c
parentbd9c329c1055a9265812352655ed2eec93f36e92 (diff)
downloadzlib-ac8f12c97d1afd9bafa9c710f827d40a407d3266.tar.gz
zlib-ac8f12c97d1afd9bafa9c710f827d40a407d3266.tar.bz2
zlib-ac8f12c97d1afd9bafa9c710f827d40a407d3266.zip
Add LIT_MEM define to use more memory for a small deflate speedup.
A bug fix in zlib 1.2.12 resulted in a slight slowdown (1-2%) of deflate. This commit provides the option to #define LIT_MEM, which uses more memory to reverse most of that slowdown. The memory for the pending buffer and symbol buffers is increased by 25%, which increases the total memory usage with the default parameters by about 6%.
Diffstat (limited to 'deflate.c')
-rw-r--r--deflate.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/deflate.c b/deflate.c
index 5e72cd9..263bbc8 100644
--- a/deflate.c
+++ b/deflate.c
@@ -493,7 +493,11 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
493 * symbols from which it is being constructed. 493 * symbols from which it is being constructed.
494 */ 494 */
495 495
496#ifdef LIT_MEM
497 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 5);
498#else
496 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4); 499 s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
500#endif
497 s->pending_buf_size = (ulg)s->lit_bufsize * 4; 501 s->pending_buf_size = (ulg)s->lit_bufsize * 4;
498 502
499 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 503 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
@@ -503,8 +507,14 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
503 deflateEnd (strm); 507 deflateEnd (strm);
504 return Z_MEM_ERROR; 508 return Z_MEM_ERROR;
505 } 509 }
510#ifdef LIT_MEM
511 s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
512 s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
513 s->sym_end = s->lit_bufsize - 1;
514#else
506 s->sym_buf = s->pending_buf + s->lit_bufsize; 515 s->sym_buf = s->pending_buf + s->lit_bufsize;
507 s->sym_end = (s->lit_bufsize - 1) * 3; 516 s->sym_end = (s->lit_bufsize - 1) * 3;
517#endif
508 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K 518 /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
509 * on 16 bit machines and because stored blocks are restricted to 519 * on 16 bit machines and because stored blocks are restricted to
510 * 64K-1 bytes. 520 * 64K-1 bytes.
@@ -720,9 +730,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
720 730
721 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 731 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
722 s = strm->state; 732 s = strm->state;
733#ifdef LIT_MEM
734 if (bits < 0 || bits > 16 ||
735 (uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
736 return Z_BUF_ERROR;
737#else
723 if (bits < 0 || bits > 16 || 738 if (bits < 0 || bits > 16 ||
724 s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3)) 739 s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
725 return Z_BUF_ERROR; 740 return Z_BUF_ERROR;
741#endif
726 do { 742 do {
727 put = Buf_size - s->bi_valid; 743 put = Buf_size - s->bi_valid;
728 if (put > bits) 744 if (put > bits)
@@ -1308,7 +1324,12 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1308 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); 1324 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1309 1325
1310 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1326 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1327#ifdef LIT_MEM
1328 ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1329 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1330#else
1311 ds->sym_buf = ds->pending_buf + ds->lit_bufsize; 1331 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1332#endif
1312 1333
1313 ds->l_desc.dyn_tree = ds->dyn_ltree; 1334 ds->l_desc.dyn_tree = ds->dyn_ltree;
1314 ds->d_desc.dyn_tree = ds->dyn_dtree; 1335 ds->d_desc.dyn_tree = ds->dyn_dtree;