From 8b1a143cda128f655720695fa5b26b184704efd8 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 5 Mar 2025 20:40:19 +0100 Subject: [PATCH] Support zlib-ng this provides a 60% speed improvement on an older map I have --- CMakeLists.txt | 11 ++++++++++- src/ZlibDecompressor.cpp | 20 +++++++++++++++----- src/cmake_config.h.in | 1 + 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3e9d99..0d5f860 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,16 @@ endif(NOT LIBGD_LIBRARY OR NOT LIBGD_INCLUDE_DIR) # Libraries: zlib -find_package(ZLIB REQUIRED) +find_package(zlib-ng QUIET) +if(zlib-ng_FOUND) + set(ZLIB_INCLUDE_DIR zlib-ng::zlib) + set(ZLIB_LIBRARY zlib-ng::zlib) + set(USE_ZLIB_NG TRUE) + message(STATUS "Found zlib-ng, using it instead of zlib.") +else() + find_package(ZLIB REQUIRED) + set(USE_ZLIB_NG FALSE) +endif() # Libraries: zstd diff --git a/src/ZlibDecompressor.cpp b/src/ZlibDecompressor.cpp index bfb1530..2222ae4 100644 --- a/src/ZlibDecompressor.cpp +++ b/src/ZlibDecompressor.cpp @@ -1,6 +1,16 @@ -#include -#include +#include #include "ZlibDecompressor.h" +#include "config.h" + +// for convenient usage of both +#if USE_ZLIB_NG +#include +#define z_stream zng_stream +#define Z(x) zng_ ## x +#else +#include +#define Z(x) x +#endif ZlibDecompressor::ZlibDecompressor(const u8 *data, size_t size): m_data(data), @@ -38,7 +48,7 @@ ustring ZlibDecompressor::decompress() strm.next_in = Z_NULL; strm.avail_in = 0; - if (inflateInit(&strm) != Z_OK) + if (Z(inflateInit)(&strm) != Z_OK) throw DecompressError(); strm.next_in = const_cast(data); @@ -49,7 +59,7 @@ ustring ZlibDecompressor::decompress() int ret = 0; do { - ret = inflate(&strm, Z_NO_FLUSH); + ret = Z(inflate)(&strm, Z_NO_FLUSH); if (strm.avail_out == 0) { const auto off = buffer.size(); buffer.reserve(off + BUFSIZE); @@ -62,7 +72,7 @@ ustring ZlibDecompressor::decompress() m_seekPos += strm.next_in - data; buffer.resize(buffer.size() - strm.avail_out); - (void) inflateEnd(&strm); + (void) Z(inflateEnd)(&strm); return buffer; } diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in index 8262394..300f9c6 100644 --- a/src/cmake_config.h.in +++ b/src/cmake_config.h.in @@ -6,6 +6,7 @@ #cmakedefine01 USE_POSTGRESQL #cmakedefine01 USE_LEVELDB #cmakedefine01 USE_REDIS +#cmakedefine01 USE_ZLIB_NG #define SHAREDIR "@SHAREDIR@"