Files
git/compat/zlib-compat.h
Patrick Steinhardt b9d6f64393 compat/zlib: allow use of zlib-ng as backend
The zlib-ng library is a hard fork of the old and venerable zlib
library. It describes itself as zlib replacement with optimizations for
"next generation" systems. As such, it contains several implementations
of central algorithms using for example SSE2, AVX2 and other vectorized
CPU intrinsics that supposedly speed up in- and deflating data.

And indeed, compiling Git against zlib-ng leads to a significant speedup
when reading objects. The following benchmark uses git-cat-file(1) with
`--batch --batch-all-objects` in the Git repository:

    Benchmark 1: zlib
      Time (mean ± σ):     52.085 s ±  0.141 s    [User: 51.500 s, System: 0.456 s]
      Range (min … max):   52.004 s … 52.335 s    5 runs

    Benchmark 2: zlib-ng
      Time (mean ± σ):     40.324 s ±  0.134 s    [User: 39.731 s, System: 0.490 s]
      Range (min … max):   40.135 s … 40.484 s    5 runs

    Summary
      zlib-ng ran
        1.29 ± 0.01 times faster than zlib

So we're looking at a ~25% speedup compared to zlib. This is of course
an extreme example, as it makes us read through all objects in the
repository. But regardless, it should be possible to see some sort of
speedup in most commands that end up accessing the object database.

The zlib-ng library provides a compatibility layer that makes it a
proper drop-in replacement for zlib: nothing needs to change in the
build system to support it. Unfortunately though, this mode isn't easy
to use on most systems because distributions do not allow you to install
zlib-ng in that way, as that would mean that the zlib library would be
globally replaced. Instead, many distributions provide a package that
installs zlib-ng without the compatibility layer. This version does
provide effectively the same APIs like zlib does, but all of the symbols
are prefixed with `zng_` to avoid symbol collisions.

Implement a new build option that allows us to link against zlib-ng
directly. If set, we redefine zlib symbols so that we use the `zng_`
prefixed versions thereof provided by that library. Like this, it
becomes possible to install both zlib and zlib-ng (without the compat
layer) and then pick whichever library one wants to link against for
Git.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-01-28 13:03:23 -08:00

54 lines
1.6 KiB
C

#ifndef COMPAT_ZLIB_H
#define COMPAT_ZLIB_H
#ifdef HAVE_ZLIB_NG
# include <zlib-ng.h>
# define z_stream zng_stream
#define gz_header_s zng_gz_header_s
# define crc32(crc, buf, len) zng_crc32(crc, buf, len)
# define inflate(strm, bits) zng_inflate(strm, bits)
# define inflateEnd(strm) zng_inflateEnd(strm)
# define inflateInit(strm) zng_inflateInit(strm)
# define inflateInit2(strm, bits) zng_inflateInit2(strm, bits)
# define inflateReset(strm) zng_inflateReset(strm)
# define deflate(strm, flush) zng_deflate(strm, flush)
# define deflateBound(strm, source_len) zng_deflateBound(strm, source_len)
# define deflateEnd(strm) zng_deflateEnd(strm)
# define deflateInit(strm, level) zng_deflateInit(strm, level)
# define deflateInit2(stream, level, method, window_bits, mem_level, strategy) zng_deflateInit2(stream, level, method, window_bits, mem_level, strategy)
# define deflateReset(strm) zng_deflateReset(strm)
# define deflateSetHeader(strm, head) zng_deflateSetHeader(strm, head)
#else
# include <zlib.h>
# if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
# define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
# endif
/*
* zlib only gained support for setting up the gzip header in v1.2.2.1. In
* Git we only set the header to make archives reproducible across different
* operating systems, so it's fine to simply make this a no-op when using a
* zlib version that doesn't support this yet.
*/
# if ZLIB_VERNUM < 0x1221
struct gz_header_s {
int os;
};
static int deflateSetHeader(z_streamp strm, struct gz_header_s *head)
{
(void)(strm);
(void)(head);
return Z_OK;
}
# endif
#endif /* HAVE_ZLIB_NG */
#endif /* COMPAT_ZLIB_H */