To: zlib@quest.jpl.nasa.gov
Cc: png-implement@ccrc.wustl.edu
Subject: Small but free speedup for zlib
Date: Wed, 12 Jul 2000 23:15:26 -0400
Message-ID: <26797.963458126@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>

In connection with another project I had occasion to profile zlib
compressing & decompressing relatively small chunks of data, a kilobyte
or so.  (In this application there are a lot of such chunks, so it's
worth paying attention.)

I was bemused to discover that a large fraction of the runtime
was being spent in memset().  It turns out that this is simply
because the default implementation of zalloc calls calloc(),
which of course uses memset to zero the area per its spec.
But zlib doesn't need the allocated memory to be zeroed.
This simple change:

*** zutil.c.orig  Wed Jul  8 13:01:47 1998
--- zutil.c   Wed Jul 12 22:34:20 2000
***************
*** 211,217 ****
      unsigned size;
  {
      if (opaque) items += size - size; /* make compiler happy */
!     return (voidpf)calloc(items, size);
  }

  void  zcfree (opaque, ptr)
--- 211,217 ----
      unsigned size;
  {
      if (opaque) items += size - size; /* make compiler happy */
!     return (voidpf)malloc(items * size);
  }

  void  zcfree (opaque, ptr)

reduces the time to decompress 1K of text by about 25% on my
machine.  The time to compress it drops even more, over 30%.

On larger objects, of course, there's relatively little savings
because the start-up overhead isn't significant.  But if you care
about speed on small amounts of data, this is a worthwhile change.

			regards, tom lane

--
Send the message body "help" to png-implement-request@ccrc.wustl.edu

