From fd354642a7d625013abc32b79c93b7eb1a118c24 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 29 Apr 2025 14:32:26 +0200 Subject: [PATCH 01/20] Hack in ISA-L support for compression Signed-off-by: Ruben Vorderman --- Makefile | 2 +- bgzf.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++--- configure.ac | 2 +- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index efe8573a1..70a7a3c9b 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ AR = ar RANLIB = ranlib # Default libraries to link if configure is not used -htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl +htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl -lisal CPPFLAGS = # TODO: make the 64-bit support for VCF optional via configure, for now add -DVCF_ALLOW_INT64 diff --git a/bgzf.c b/bgzf.c index e21a18e70..3f7b55726 100644 --- a/bgzf.c +++ b/bgzf.c @@ -41,6 +41,13 @@ #include #endif +/* Should be fixed in the build system later */ +#define HAVE_ISAL 1 +#ifdef HAVE_ISAL +#include +#include +#endif + #include "htslib/hts.h" #include "htslib/bgzf.h" #include "htslib/hfile.h" @@ -570,11 +577,68 @@ BGZF *bgzf_hopen(hFILE *hfp, const char *mode) return fp; } -#ifdef HAVE_LIBDEFLATE +#if HAVE_ISAL +uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { + return crc32_gzip_refl(crc, buf, len); +} +#elif HAVE_LIBDEFLATE uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { return libdeflate_crc32(crc, buf, len); } +#else +uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { + return crc32(crc, buf, len); +} +#endif +#if HAVE_ISAL +static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_t slen, int level) +{ + if (level < 0 || level > 3) { + return -1; + } + struct isal_zstream *z = malloc(sizeof(struct isal_zstream)); + if (z == NULL) { + return -1; + } + static int level_buf_sizes[] = { + ISAL_DEF_LVL0_DEFAULT, + ISAL_DEF_LVL1_DEFAULT, + ISAL_DEF_LVL2_DEFAULT, + ISAL_DEF_LVL3_DEFAULT + }; + size_t level_buf_size = level_buf_sizes[level]; + uint8_t *level_buf = malloc(level_buf_size); + if (level_buf == NULL) { + free(z); + } + isal_deflate_init(z); + z->level = level; + z->level_buf = level_buf; + z->level_buf_size = level_buf_size; + z->gzip_flag = IGZIP_GZIP_NO_HDR; // Also calculates length and CRC + z->next_in = (uint8_t *)src; + z->avail_in = slen; + z->next_out = dst + BLOCK_HEADER_LENGTH; + z->avail_out = *dlen - BLOCK_HEADER_LENGTH; + z->flush = FULL_FLUSH; + z->end_of_stream = 1; + + int ret = isal_deflate(z); + if (ret != COMP_OK) { + hts_log_error("Call to _bgzf_compress_isal failed"); + free(level_buf); + free(z); + return -1; + } + *dlen = z->next_out - (uint8_t *)dst; + memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block + packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes + return 0; +} +#endif + +#if HAVE_LIBDEFLATE int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level) { if (slen == 0) { @@ -598,6 +662,13 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le } else { level = level > 0 ? level : 6; // libdeflate doesn't honour -1 as default + + #if HAVE_ISAL + if (level == 1 || level == 2) { + return _bgzf_compress_isal(dst, dlen, src, slen, level); + } + #endif + // NB levels go up to 12 here. int lvl_map[] = {0,1,2,3,5,6,7,8,10,12}; level = lvl_map[level>9 ?9 :level]; @@ -634,9 +705,6 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le #else -uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { - return crc32(crc, buf, len); -} int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level) { diff --git a/configure.ac b/configure.ac index 399e506de..8628bec7e 100644 --- a/configure.ac +++ b/configure.ac @@ -553,7 +553,7 @@ AS_IF([test "x$with_libdeflate" != "xno"], AC_CHECK_LIB([deflate], [libdeflate_deflate_compress],[:],[libdeflate='missing library']) AS_IF([test "$libdeflate" = "ok"], [AC_DEFINE([HAVE_LIBDEFLATE], 1, [Define if libdeflate is available.]) - LIBS="-ldeflate $LIBS" + LIBS="-ldeflate -lisal $LIBS" private_LIBS="$private_LIBS -ldeflate" static_LIBS="$static_LIBS -ldeflate"], [AS_IF([test "x$with_libdeflate" != "xcheck"], From 61419bec8e92d8f0f8b32de82393f4b9f767c70e Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 29 Apr 2025 14:50:19 +0200 Subject: [PATCH 02/20] Fix memory leak Signed-off-by: Ruben Vorderman --- bgzf.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bgzf.c b/bgzf.c index 3f7b55726..02d6dc8bb 100644 --- a/bgzf.c +++ b/bgzf.c @@ -632,6 +632,8 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size return -1; } *dlen = z->next_out - (uint8_t *)dst; + free(level_buf); + free(z); memcpy(dst, g_magic, BLOCK_HEADER_LENGTH); // the last two bytes are a place holder for the length of the block packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes return 0; From 3d68bb899631dbb7fc565cca48c475ee77289a15 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 29 Apr 2025 15:01:16 +0200 Subject: [PATCH 03/20] Fix trailing spaces Signed-off-by: Ruben Vorderman --- bgzf.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bgzf.c b/bgzf.c index 02d6dc8bb..1bb2f09f3 100644 --- a/bgzf.c +++ b/bgzf.c @@ -585,7 +585,7 @@ uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { return libdeflate_crc32(crc, buf, len); } -#else +#else uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { return crc32(crc, buf, len); } @@ -602,9 +602,9 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size return -1; } static int level_buf_sizes[] = { - ISAL_DEF_LVL0_DEFAULT, - ISAL_DEF_LVL1_DEFAULT, - ISAL_DEF_LVL2_DEFAULT, + ISAL_DEF_LVL0_DEFAULT, + ISAL_DEF_LVL1_DEFAULT, + ISAL_DEF_LVL2_DEFAULT, ISAL_DEF_LVL3_DEFAULT }; size_t level_buf_size = level_buf_sizes[level]; @@ -616,7 +616,7 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size z->level = level; z->level_buf = level_buf; z->level_buf_size = level_buf_size; - z->gzip_flag = IGZIP_GZIP_NO_HDR; // Also calculates length and CRC + z->gzip_flag = IGZIP_GZIP_NO_HDR; // Also calculates length and CRC z->next_in = (uint8_t *)src; z->avail_in = slen; z->next_out = dst + BLOCK_HEADER_LENGTH; @@ -638,7 +638,7 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size packInt16(&dst[16], *dlen - 1); // write the compressed length; -1 to fit 2 bytes return 0; } -#endif +#endif #if HAVE_LIBDEFLATE int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level) @@ -667,10 +667,10 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le #if HAVE_ISAL if (level == 1 || level == 2) { - return _bgzf_compress_isal(dst, dlen, src, slen, level); + return _bgzf_compress_isal(dst, dlen, src, slen, level); } #endif - + // NB levels go up to 12 here. int lvl_map[] = {0,1,2,3,5,6,7,8,10,12}; level = lvl_map[level>9 ?9 :level]; From c5967775efa8287c8b5bee518898cb11577b7b7f Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Tue, 29 Apr 2025 15:18:47 +0200 Subject: [PATCH 04/20] Isal decompression Signed-off-by: Ruben Vorderman --- bgzf.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/bgzf.c b/bgzf.c index 1bb2f09f3..2ad940b2f 100644 --- a/bgzf.c +++ b/bgzf.c @@ -812,7 +812,44 @@ static int deflate_block(BGZF *fp, int block_length) return comp_size; } -#ifdef HAVE_LIBDEFLATE +#ifdef HAVE_ISAL +static int bgzf_uncompress(uint8_t *dst, size_t *dlen, + const uint8_t *src, size_t slen, + uint32_t expected_crc) { + struct inflate_state *z = malloc(sizeof(struct inflate_state)); + if (z == NULL) { + hts_log_error("Allocating isal inflate state failed"); + return -1; + } + isal_inflate_init(z); + z->next_in = (uint8_t *)src; + z->avail_in = slen; + z->next_out = dst; + z->avail_out = *dlen; + z->crc_flag = ISAL_GZIP_NO_HDR; + + int ret = isal_inflate(z); + if (ret != ISAL_DECOMP_OK) { + hts_log_error("Inflate operation failed: %d", ret); + free(z); + return -1; + + } + *dlen = z->next_out - dst; + uint32_t crc = z->crc; + free(z); + #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + // Pretend the CRC was OK so the fuzzer doesn't have to get it right + crc = expected_crc; + #endif + if (crc != expected_crc) { + hts_log_error("CRC32 checksum mismatch"); + return -2; + } + return 0; +} + +#elif HAVE_LIBDEFLATE static int bgzf_uncompress(uint8_t *dst, size_t *dlen, const uint8_t *src, size_t slen, From 5e00c8b7f157f27acac87d5e21daf6831f71c860 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Wed, 21 Oct 2026 14:47:48 +0200 Subject: [PATCH 05/20] Add ISA-L to the build system Signed-off-by: Ruben Vorderman --- Makefile | 2 +- bgzf.c | 2 -- configure.ac | 28 +++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 70a7a3c9b..efe8573a1 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ AR = ar RANLIB = ranlib # Default libraries to link if configure is not used -htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl -lisal +htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl CPPFLAGS = # TODO: make the 64-bit support for VCF optional via configure, for now add -DVCF_ALLOW_INT64 diff --git a/bgzf.c b/bgzf.c index 2ad940b2f..376d1def7 100644 --- a/bgzf.c +++ b/bgzf.c @@ -41,8 +41,6 @@ #include #endif -/* Should be fixed in the build system later */ -#define HAVE_ISAL 1 #ifdef HAVE_ISAL #include #include diff --git a/configure.ac b/configure.ac index 8628bec7e..cb9919477 100644 --- a/configure.ac +++ b/configure.ac @@ -286,6 +286,11 @@ AC_ARG_WITH([libdeflate], [use libdeflate for faster crc and deflate algorithms])], [], [with_libdeflate=check]) +AC_ARG_WITH([isal], + [AS_HELP_STRING([--with-isal], + [use isal for faster crc and faster deflate at compression level 1 and 2.])], + [], [with_isal=check]) + AC_ARG_WITH([plugin-dir], [AS_HELP_STRING([--with-plugin-dir=DIR], [plugin installation location [LIBEXECDIR/htslib]])], @@ -553,7 +558,7 @@ AS_IF([test "x$with_libdeflate" != "xno"], AC_CHECK_LIB([deflate], [libdeflate_deflate_compress],[:],[libdeflate='missing library']) AS_IF([test "$libdeflate" = "ok"], [AC_DEFINE([HAVE_LIBDEFLATE], 1, [Define if libdeflate is available.]) - LIBS="-ldeflate -lisal $LIBS" + LIBS="-ldeflate $LIBS" private_LIBS="$private_LIBS -ldeflate" static_LIBS="$static_LIBS -ldeflate"], [AS_IF([test "x$with_libdeflate" != "xcheck"], @@ -568,6 +573,27 @@ are not currently on them. Either configure with --without-libdeflate or resolve this error to build HTSlib.])])])]) +AS_IF([test "x$with_isal" != "xno"], + [isal=ok + AC_CHECK_HEADER([isa-l.h],[],[isal='missing header'],[;]) + AC_CHECK_LIB([isal], [isal_deflate],[:],[isal='missing library']) + AS_IF([test "$isal" = "ok"], + [AC_DEFINE([HAVE_ISAL], 1, [Define if ISAL is available.]) + LIBS="-lisal $LIBS" + private_LIBS="$private_LIBS -lisal" + static_LIBS="$static_LIBS -lisal"], + [AS_IF([test "x$with_isal" != "xcheck"], + [MSG_ERROR([isal development files not found: $isal + +You requested isal, but do not have the required header / library +files. The source for isal is available from +. You may have to adjust +search paths in CPPFLAGS and/or LDFLAGS if the header and library +are not currently on them. + +Either configure with --without-isal or resolve this error to build +HTSlib.])])])]) + libcurl=disabled if test "$enable_libcurl" != no; then libcurl_devel=ok From fc5353eada35fec6e3c838bbb1fee8fe4686f7af Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Wed, 21 Oct 2026 14:55:34 +0200 Subject: [PATCH 06/20] Use ISA-L when libdeflate is not available Signed-off-by: Ruben Vorderman --- bgzf.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bgzf.c b/bgzf.c index 376d1def7..1e1e2b1d0 100644 --- a/bgzf.c +++ b/bgzf.c @@ -721,6 +721,11 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le u16_to_le(~slen, &dst[BLOCK_HEADER_LENGTH+3]); // ones-complement length memcpy(dst + BLOCK_HEADER_LENGTH+5, src, slen); *dlen = slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; + #if HAVE_ISAL + else if (level == 1 || level == 2) { + return _bgzf_compress_isal(dst, dlen, src, slen, level); + } + #endif } else { // compress the body zs.zalloc = NULL; zs.zfree = NULL; From 3abaef04fbad3fcbe8c81f3eeffe223513125e63 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Wed, 21 Oct 2026 14:58:36 +0200 Subject: [PATCH 07/20] Add isal to test matrix Signed-off-by: Ruben Vorderman --- .github/workflows/linux-build.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index c48a6a1a2..5377a72c7 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -21,6 +21,7 @@ jobs: use-configure: use-configure compiler: clang use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: no-extra-checks @@ -29,6 +30,7 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: sanitize run-extra-checks: no-extra-checks @@ -37,6 +39,7 @@ jobs: use-configure: no-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: run-extra-checks @@ -45,6 +48,7 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: use-libdeflate + use-isal: no-isal sanitize: no-sanitize run-extra-checks: no-extra-checks @@ -53,9 +57,27 @@ jobs: use-configure: use-configure compiler: gcc use-libdeflate: no-libdeflate + use-isal: no-isal sanitize: sanitize run-extra-checks: no-extra-checks + # x86-64, gcc, configure, isal, sanitize + - os: ubuntu-latest + use-configure: use-configure + compiler: gcc + use-libdeflate: no-libdeflate + use-isal: use-isal + sanitize: sanitize + run-extra-checks: no-extra-checks + + # x86-64, gcc, configure, isal, libdeflate, sanitize + - os: ubuntu-latest + use-configure: use-configure + compiler: gcc + use-libdeflate: use-libdeflate + use-isal: use-isal + sanitize: sanitize + run-extra-checks: no-extra-checks env: UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1 @@ -78,6 +100,9 @@ jobs: if [ '${{ matrix.use-libdeflate }}' = 'use-libdeflate' ] ; then deflate_pkgs="libdeflate-dev" fi + if [ '${{ matrix.use-libdeflate }}' = 'use-isal' ] ; then + deflate_pkgs="libisal-dev" + fi sudo apt-get update sudo apt-get install -y --no-install-suggests --no-install-recommends $autoconf_pkgs make ${{ matrix.compiler }} perl zlib1g-dev libbz2-dev liblzma-dev libcurl4-gnutls-dev libssl-dev $deflate_pkgs From 11f61b9c4b77321bd3c61f91140be0cb75e89683 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Thu, 22 Oct 2026 10:22:50 +0200 Subject: [PATCH 08/20] Use stateless functions for ISA-L so error is thrown when not enough space is available in the output buffer Signed-off-by: Ruben Vorderman --- bgzf.c | 10 +++++++--- configure.ac | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bgzf.c b/bgzf.c index 1e1e2b1d0..36d7c49e8 100644 --- a/bgzf.c +++ b/bgzf.c @@ -610,7 +610,7 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size if (level_buf == NULL) { free(z); } - isal_deflate_init(z); + isal_deflate_stateless_init(z); z->level = level; z->level_buf = level_buf; z->level_buf_size = level_buf_size; @@ -622,7 +622,9 @@ static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size z->flush = FULL_FLUSH; z->end_of_stream = 1; - int ret = isal_deflate(z); + /* isal_deflate_stateless will throw an overflow error when there is not + * enough room in the output buffer */ + int ret = isal_deflate_stateless(z); if (ret != COMP_OK) { hts_log_error("Call to _bgzf_compress_isal failed"); free(level_buf); @@ -831,7 +833,9 @@ static int bgzf_uncompress(uint8_t *dst, size_t *dlen, z->avail_out = *dlen; z->crc_flag = ISAL_GZIP_NO_HDR; - int ret = isal_inflate(z); + /* isal_deflate_stateless will throw an overflow error when there is not + * enough room in the output buffer */ + int ret = isal_inflate_stateless(z); if (ret != ISAL_DECOMP_OK) { hts_log_error("Inflate operation failed: %d", ret); free(z); diff --git a/configure.ac b/configure.ac index cb9919477..526b77431 100644 --- a/configure.ac +++ b/configure.ac @@ -576,7 +576,7 @@ HTSlib.])])])]) AS_IF([test "x$with_isal" != "xno"], [isal=ok AC_CHECK_HEADER([isa-l.h],[],[isal='missing header'],[;]) - AC_CHECK_LIB([isal], [isal_deflate],[:],[isal='missing library']) + AC_CHECK_LIB([isal], [isal_deflate_stateless],[:],[isal='missing library']) AS_IF([test "$isal" = "ok"], [AC_DEFINE([HAVE_ISAL], 1, [Define if ISAL is available.]) LIBS="-lisal $LIBS" From d25067ed55d573ae40ded9e1a18af4e1e2cb8ecd Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:06:00 +0100 Subject: [PATCH 09/20] Fix copy-paste error Co-authored-by: James Bonfield Signed-off-by: Ruben Vorderman --- .github/workflows/linux-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index 5377a72c7..c86ad2b4b 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -100,7 +100,7 @@ jobs: if [ '${{ matrix.use-libdeflate }}' = 'use-libdeflate' ] ; then deflate_pkgs="libdeflate-dev" fi - if [ '${{ matrix.use-libdeflate }}' = 'use-isal' ] ; then + if [ '${{ matrix.use-isal}}' = 'use-isal' ] ; then deflate_pkgs="libisal-dev" fi sudo apt-get update From d3bbe403bf6afccd63c10ba3be5cc9d5bab5f224 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:07:31 +0100 Subject: [PATCH 10/20] Update bgzf.c Co-authored-by: James Bonfield Signed-off-by: Ruben Vorderman --- bgzf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgzf.c b/bgzf.c index 36d7c49e8..a84b5a8be 100644 --- a/bgzf.c +++ b/bgzf.c @@ -590,7 +590,7 @@ uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { #endif #if HAVE_ISAL -static int _bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_t slen, int level) +static int bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_t slen, int level) { if (level < 0 || level > 3) { return -1; From 1149a45ee3176eb5c6a2e520380503cb57808bd2 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:07:58 +0100 Subject: [PATCH 11/20] Update bgzf.c Co-authored-by: James Bonfield Signed-off-by: Ruben Vorderman --- bgzf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/bgzf.c b/bgzf.c index a84b5a8be..b5377da93 100644 --- a/bgzf.c +++ b/bgzf.c @@ -609,6 +609,7 @@ static int bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_ uint8_t *level_buf = malloc(level_buf_size); if (level_buf == NULL) { free(z); + return -1; } isal_deflate_stateless_init(z); z->level = level; From 35e6c6bd7168bd6eaf3603bc6b6302746e7a54c8 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:08:08 +0100 Subject: [PATCH 12/20] Update bgzf.c Co-authored-by: James Bonfield Signed-off-by: Ruben Vorderman --- bgzf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgzf.c b/bgzf.c index b5377da93..41938769e 100644 --- a/bgzf.c +++ b/bgzf.c @@ -668,7 +668,7 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le #if HAVE_ISAL if (level == 1 || level == 2) { - return _bgzf_compress_isal(dst, dlen, src, slen, level); + return bgzf_compress_isal(dst, dlen, src, slen, level); } #endif From e1a08f152ae8a9932e0ed5a1cb996d98e08cfeda Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:21:26 +0100 Subject: [PATCH 13/20] Fix ISA-L but no libdeflate compilation error Signed-off-by: Ruben Vorderman --- bgzf.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bgzf.c b/bgzf.c index 41938769e..90572bbed 100644 --- a/bgzf.c +++ b/bgzf.c @@ -627,7 +627,7 @@ static int bgzf_compress_isal(uint8_t *dst, size_t *dlen, const void *src, size_ * enough room in the output buffer */ int ret = isal_deflate_stateless(z); if (ret != COMP_OK) { - hts_log_error("Call to _bgzf_compress_isal failed"); + hts_log_error("Call to bgzf_compress_isal failed"); free(level_buf); free(z); return -1; @@ -725,9 +725,8 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le memcpy(dst + BLOCK_HEADER_LENGTH+5, src, slen); *dlen = slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; #if HAVE_ISAL - else if (level == 1 || level == 2) { - return _bgzf_compress_isal(dst, dlen, src, slen, level); - } + } else if (level == 1 || level == 2) { + return bgzf_compress_isal(dst, dlen, src, slen, level); #endif } else { // compress the body From cc7b75ef48e6d5a900ee99c32a3c5519b5900385 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 30 Oct 2026 09:29:10 +0100 Subject: [PATCH 14/20] Fix logic error in apt install preparation for linux build Signed-off-by: Ruben Vorderman --- .github/workflows/linux-build.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index c86ad2b4b..668d631e9 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -94,6 +94,7 @@ jobs: run: | autoconf_pkgs="" deflate_pkgs="" + isal_pkgs="" if [ '${{ matrix.use-configure }}' = 'use-configure' ] ; then autoconf_pkgs="autoconf automake" fi @@ -101,10 +102,10 @@ jobs: deflate_pkgs="libdeflate-dev" fi if [ '${{ matrix.use-isal}}' = 'use-isal' ] ; then - deflate_pkgs="libisal-dev" + isal_pkgs="libisal-dev" fi sudo apt-get update - sudo apt-get install -y --no-install-suggests --no-install-recommends $autoconf_pkgs make ${{ matrix.compiler }} perl zlib1g-dev libbz2-dev liblzma-dev libcurl4-gnutls-dev libssl-dev $deflate_pkgs + sudo apt-get install -y --no-install-suggests --no-install-recommends $autoconf_pkgs make ${{ matrix.compiler }} perl zlib1g-dev libbz2-dev liblzma-dev libcurl4-gnutls-dev libssl-dev $deflate_pkgs $isal_pkgs - name: Configure if: ${{ matrix.use-configure == 'use-configure' }} From 8d3b9f2485225c558f352d9990c02b09db351d8f Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 2 Nov 2026 08:52:15 +0100 Subject: [PATCH 15/20] Only compress with ISA-L at level 1 Signed-off-by: Ruben Vorderman --- bgzf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bgzf.c b/bgzf.c index 90572bbed..0432fcb49 100644 --- a/bgzf.c +++ b/bgzf.c @@ -667,7 +667,7 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le level = level > 0 ? level : 6; // libdeflate doesn't honour -1 as default #if HAVE_ISAL - if (level == 1 || level == 2) { + if (level == 1) { return bgzf_compress_isal(dst, dlen, src, slen, level); } #endif @@ -725,7 +725,7 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le memcpy(dst + BLOCK_HEADER_LENGTH+5, src, slen); *dlen = slen+5 + BLOCK_HEADER_LENGTH + BLOCK_FOOTER_LENGTH; #if HAVE_ISAL - } else if (level == 1 || level == 2) { + } else if (level == 1) { return bgzf_compress_isal(dst, dlen, src, slen, level); #endif } else { From 289884ad014ddc93bbcbad604f29ef5f1f92716a Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 2 Nov 2026 08:58:55 +0100 Subject: [PATCH 16/20] Update build dependencies in INSTALL Signed-off-by: Ruben Vorderman --- INSTALL | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/INSTALL b/INSTALL index 220ac13f7..2c04e5cb4 100644 --- a/INSTALL +++ b/INSTALL @@ -39,10 +39,14 @@ a development ('-dev' or '-devel') package separate from the main library. libcurl (optional, but strongly recommended) libcrypto (optional for Amazon S3 support; not needed on MacOS) libdeflate (optional, but strongly recommended for faster gzip) + isa-l (optional, but strongly recommended for faster "fast" level of gzip) Disabling libbzip2 and liblzma will make some CRAM files unreadable, so is not recommended. +ISA-L is available as libisal on Debian-based linux distributions and as isa-l +on conda-forge. + Using libcurl provides HTSlib with network protocol support, for example it enables the use of ftp://, http://, and https:// URLs. It is also required if direct access to Amazon S3 or Google Cloud @@ -182,6 +186,13 @@ various features and specify further optional external requirements: By default, ./configure will probe for libdeflate and use it if available. To prevent this, use --without-libdeflate. +--with-isal + ISA-L is another heavily optimized library for DEFLATE-based compression + and decompression. It provides a few very fast compression levels at + similar size of level 1. It also includes a fast gzip stream decoder. + By default, ./configure will probe for isa-l and use it if + available. To prevent this, use --without-isal. + Each --enable-FEATURE/--disable-FEATURE/--with-PACKAGE/--without-PACKAGE option listed also has an opposite, e.g., --without-external-htscodecs or --disable-plugins. However, apart from those options for which the From 06d72993e4bd906532dd4c56d374543666c34a5e Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Wed, 28 Oct 2026 14:59:48 +0100 Subject: [PATCH 17/20] Decompress streamed gzip data using ISA-L if available Signed-off-by: Ruben Vorderman --- bgzf.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++- htslib/bgzf.h | 2 + 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/bgzf.c b/bgzf.c index 0432fcb49..c5bb4d808 100644 --- a/bgzf.c +++ b/bgzf.c @@ -402,6 +402,34 @@ static const char *bgzf_zerr(int errnum, z_stream *zs) } } +#if HAVE_ISAL +static const char *isal_inflate_err(int errnum) +{ + static char buffer[32]; + + switch (errnum) { + case ISAL_INVALID_BLOCK: + return "invalid deflate block found"; + case ISAL_INVALID_SYMBOL: + return "invalid deflate symbol found"; + case ISAL_INVALID_LOOKBACK: + return "invalid lookback distance found"; + case ISAL_INVALID_WRAPPER: + return "invalid gzip/zlib wrapper found"; + case ISAL_UNSUPPORTED_METHOD: + return "gzip/zlib wrapper specifies unsupported compress method"; + case ISAL_INCORRECT_CHECKSUM: + return "incorrect checksum found"; + case ISAL_NEED_DICT: + return "data was compressed using a dictionary"; + case ISAL_DECOMP_OK: // 0: maybe gzgets error Z_NULL + default: + snprintf(buffer, sizeof(buffer), "[%d] unknown", errnum); + return buffer; // FIXME: Not thread-safe. + } +} +#endif + static BGZF *bgzf_read_init(hFILE *hfpr, const char *filename) { BGZF *fp; @@ -492,6 +520,7 @@ static BGZF *bgzf_write_init(const char *mode) fp->gz_stream->zalloc = NULL; fp->gz_stream->zfree = NULL; fp->gz_stream->msg = NULL; + fp->isal_inflate_state= NULL; int ret = deflateInit2(fp->gz_stream, fp->compress_level, Z_DEFLATED, 15|16, 8, Z_DEFAULT_STRATEGY); if (ret!=Z_OK) { @@ -954,6 +983,61 @@ static int inflate_block(BGZF* fp, int block_length) return dlen; } +#if HAVE_ISAL +static int inflate_gzip_block(BGZF *fp) +{ + // we will set this to true when we detect EOF, so we don't bang against the EOF more than once per call + int input_eof = 0; + + // write to the part of the output buffer after block_offset + fp->isal_inflate_state->next_out = (Bytef*)fp->uncompressed_block + fp->block_offset; + fp->isal_inflate_state->avail_out = BGZF_MAX_BLOCK_SIZE - fp->block_offset; + + while ( fp->isal_inflate_state->avail_out != 0 ) { + // until we fill the output buffer (or hit EOF) + + if ( !input_eof && fp->isal_inflate_state->avail_in == 0 ) { + // we are out of input data in the buffer. Get more. + fp->isal_inflate_state->next_in = fp->compressed_block; + int ret = hread(fp->fp, fp->compressed_block, BGZF_BLOCK_SIZE); + if ( ret < 0 ) { + // hread had an error. Pass it on. + return ret; + } + fp->isal_inflate_state->avail_in = ret; + if ( fp->isal_inflate_state->avail_in < BGZF_BLOCK_SIZE ) { + // we have reached EOF but the decompressor hasn't necessarily + input_eof = 1; + } + } + + // decompress as much data as we can + int ret = isal_inflate(fp->isal_inflate_state); + + if ( ret != ISAL_DECOMP_OK ) { + // an error occurred, other than running out of space + hts_log_error("Inflate operation failed: %s", isal_inflate_err(ret)); + fp->errcode |= BGZF_ERR_ZLIB; + return -1; + } else if ( fp->isal_inflate_state->block_state == ISAL_BLOCK_FINISH ) { + // we finished a GZIP member + // scratch for peeking to see if the file is over + char c; + if (fp->isal_inflate_state->avail_in > 0 || hpeek(fp->fp, &c, 1) == 1) { + // there is more data; try and read another GZIP member in the remaining data + isal_inflate_reset(fp->isal_inflate_state); + } else { + // we consumed all the input data and hit Z_STREAM_END + // so stop looping, even if we never fill the output buffer + break; + } + } + } + + // when we get here, the buffer is full or there is an EOF after a complete gzip member + return BGZF_MAX_BLOCK_SIZE - fp->isal_inflate_state->avail_out; +} +#else // Decompress the next part of a non-blocked GZIP file. // Return the number of uncompressed bytes read, 0 on EOF, or a negative number on error. // Will fill the output buffer unless the end of the GZIP file is reached. @@ -1022,7 +1106,7 @@ static int inflate_gzip_block(BGZF *fp) // when we get here, the buffer is full or there is an EOF after a complete gzip member return BGZF_MAX_BLOCK_SIZE - fp->gz_stream->avail_out; } - +#endif // Returns: 0 on success (BGZF header); -1 on non-BGZF GZIP header; -2 on error static int check_header(const uint8_t *header) { @@ -1260,7 +1344,7 @@ static int bgzf_read_block_core(BGZF *fp) } // Reading compressed file - if ( fp->is_gzip && fp->gz_stream ) // is this is an initialized gzip stream? + if ( fp->is_gzip && (fp->gz_stream || fp->isal_inflate_state )) // is this is an initialized gzip stream? { count = inflate_gzip_block(fp); if ( count<0 ) @@ -1305,6 +1389,19 @@ static int bgzf_read_block_core(BGZF *fp) count = hread(fp->fp, cblock+sizeof(header), BGZF_BLOCK_SIZE - sizeof(header)) + sizeof(header); fp->is_gzip = 1; +#if HAVE_ISAL + fp->isal_inflate_state = (struct inflate_state *)calloc(1, sizeof(struct inflate_state)); + if (fp->isal_inflate_state == NULL) { + hts_log_error("Out of memory"); + fp->errcode = BGZF_ERR_ZLIB; + return -1; + } + isal_inflate_init(fp->isal_inflate_state); + fp->isal_inflate_state->crc_flag = ISAL_GZIP; + fp->isal_inflate_state->hist_bits = 15; + fp->isal_inflate_state->avail_in = count; + fp->isal_inflate_state->next_in = cblock; +#else fp->gz_stream = (z_stream*) calloc(1,sizeof(z_stream)); // Set up zlib, using a window size of 15, and its built-in GZIP header processing (+16). int ret = inflateInit2(fp->gz_stream, 15 + 16); @@ -1316,6 +1413,7 @@ static int bgzf_read_block_core(BGZF *fp) } fp->gz_stream->avail_in = count; fp->gz_stream->next_in = cblock; +#endif count = inflate_gzip_block(fp); if ( count<0 ) { diff --git a/htslib/bgzf.h b/htslib/bgzf.h index 420faed16..3a90869b6 100644 --- a/htslib/bgzf.h +++ b/htslib/bgzf.h @@ -64,6 +64,7 @@ struct bgzf_mtaux_t; typedef struct bgzidx_t bgzidx_t; typedef struct bgzf_cache_t bgzf_cache_t; struct z_stream_s; +struct inflate_state; struct BGZF { // Reserved bits should be written as 0; read as "don't care" @@ -80,6 +81,7 @@ struct BGZF { bgzidx_t *idx; // BGZF index int idx_build_otf; // build index on the fly, set by bgzf_index_build_init() struct z_stream_s *gz_stream; // for gzip-compressed files + struct inflate_state *isal_inflate_state; // For faster gzip decompression with ISA-L int64_t seeked; // virtual offset of last seek }; #ifndef HTS_BGZF_TYPEDEF From c6846e57124f9d34fdbbde41b1e3039f819c9a86 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 27 Nov 2026 09:58:48 +0100 Subject: [PATCH 18/20] Limit ISA-L decompression to x86-64 architectures ISA-L is the fastest compressor on every architecture, but only the fastest reader on x86-64. See https://github.com/pycompression/xopen/pull/188 Signed-off-by: Ruben Vorderman --- bgzf.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/bgzf.c b/bgzf.c index c5bb4d808..0667e979a 100644 --- a/bgzf.c +++ b/bgzf.c @@ -46,6 +46,14 @@ #include #endif +#ifdef __x86_64__ +#define X86_64 __x86_64__ +#elifdef _M_AMD64 +#define X86_64 _M_AMD64 +#else +#define X86_64 0 +#endif + #include "htslib/hts.h" #include "htslib/bgzf.h" #include "htslib/hfile.h" @@ -604,13 +612,14 @@ BGZF *bgzf_hopen(hFILE *hfp, const char *mode) return fp; } -#if HAVE_ISAL + +#if HAVE_LIBDEFLATE uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { - return crc32_gzip_refl(crc, buf, len); + return libdeflate_crc32(crc, buf, len); } -#elif HAVE_LIBDEFLATE +#elif HAVE_ISAL uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { - return libdeflate_crc32(crc, buf, len); + return crc32_gzip_refl(crc, buf, len); } #else uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) { @@ -846,7 +855,7 @@ static int deflate_block(BGZF *fp, int block_length) return comp_size; } -#ifdef HAVE_ISAL +#if HAVE_ISAL && X86_64 static int bgzf_uncompress(uint8_t *dst, size_t *dlen, const uint8_t *src, size_t slen, uint32_t expected_crc) { @@ -983,7 +992,7 @@ static int inflate_block(BGZF* fp, int block_length) return dlen; } -#if HAVE_ISAL +#if HAVE_ISAL && X86_64 static int inflate_gzip_block(BGZF *fp) { // we will set this to true when we detect EOF, so we don't bang against the EOF more than once per call @@ -1389,7 +1398,7 @@ static int bgzf_read_block_core(BGZF *fp) count = hread(fp->fp, cblock+sizeof(header), BGZF_BLOCK_SIZE - sizeof(header)) + sizeof(header); fp->is_gzip = 1; -#if HAVE_ISAL +#if HAVE_ISAL && X86_64 fp->isal_inflate_state = (struct inflate_state *)calloc(1, sizeof(struct inflate_state)); if (fp->isal_inflate_state == NULL) { hts_log_error("Out of memory"); From 271c3ff3ec62524cd43073a0ac3b245a408d8f35 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 27 Nov 2026 10:32:23 +0100 Subject: [PATCH 19/20] Fix trailing space Signed-off-by: Ruben Vorderman --- bgzf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bgzf.c b/bgzf.c index 0667e979a..722e0db3e 100644 --- a/bgzf.c +++ b/bgzf.c @@ -50,7 +50,7 @@ #define X86_64 __x86_64__ #elifdef _M_AMD64 #define X86_64 _M_AMD64 -#else +#else #define X86_64 0 #endif @@ -2405,6 +2405,7 @@ int bgzf_close(BGZF* fp) hts_log_error("Call to inflateEnd/deflateEnd failed: %s", bgzf_zerr(ret, NULL)); } free(fp->gz_stream); + free(fp->isal_inflate_state); } ret = hclose(fp->fp); if (ret != 0) return -1; From e5302c6f1c669c37e40242aa9d59dedb97271f83 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 27 Nov 2026 10:40:24 +0100 Subject: [PATCH 20/20] Remove elifdef Signed-off-by: Ruben Vorderman --- bgzf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgzf.c b/bgzf.c index 722e0db3e..101beb877 100644 --- a/bgzf.c +++ b/bgzf.c @@ -48,7 +48,7 @@ #ifdef __x86_64__ #define X86_64 __x86_64__ -#elifdef _M_AMD64 +#elif defined(_M_AMD64) #define X86_64 _M_AMD64 #else #define X86_64 0