Skip to contentSkip to conversation

zstd: import upstream v1.5.5

PATCH In review Nick Terrell 3 participants 9 messages Nov 21, 2023 – Nov 22, 2023 Zstd

lore
Share & tools
lore
#0 lore raw

cover zstd: import upstream v1.5.5

From: Nick Terrell <[email protected]>

Import upstream zstd v1.5.5 to expose upstream's QAT integration.

The following changes since commit 77618db346455129424fadbbaec596a09feaf3bb:

zstd: Fix array-index-out-of-bounds UBSAN warning (2023-11-14 17:12:52 -0800)

are also available in the Git repository at:

https://github.com/terrelln/linux.git tags/2023-11-20-zstd-v1.5.5-v1

In addition to keeping the kernel's copy of zstd up to date, this update was requested by Intel to expose upstream zstd's external match provider API to the kernel, which allows QAT to accelerate the LZ match finding stage.

The result of the series is that zstd is cleanly imported from the tag v1.5.5-kernel [0], which is signed with upstream's signing key EF8FE99528B52FFD [1]. Meaning that running this script creates no diff:

export ZSTD=/path/to/repo/zstd/
export LINUX=/path/to/repo/linux/
cd "$ZSTD/contrib/linux-kernel"
git checkout v1.5.5-kernel
make import LINUX="$LINUX"

The first patch updates to v1.5.5 with three cherry picked backports from the Linux kernel and the upstream dev branch. The second patch negates a regression in decompression speed caused by the update.

The series has been tested on x86-64, and has been boot tested with a zstd compressed kernel on i386 and aarch64. I benchmarked the series on x86-64 with gcc-13.2.1 on an Intel i9-9900K by measuring the performance of compressed filesystems read & writes.

Component, Level, C. time delta, size delta, D. time delta

Btrfs    ,	    1,	        -1.5%,	     +0.0%,	        +1.8%
Btrfs    ,	    3,	        -5.8%,	     +0.0%,	        +3.2%
Btrfs    ,	    5,	        -5.7%,	     +0.0%,	        +0.0%
Btrfs    ,	    7,	        -6.5%,	     +0.0%,	        +0.4%
Btrfs    ,	    9,	        -6.9%,	     +0.0%,	        +1.5%
Squashfs ,	    1,	          N/A,	      0.0%,	        +1.0%

For levels 3 and above, there is a ~6% reduction in write+compression time. There is also a small increase in read+decompression time.

If everything looks good, I will submit this patch set with the rest of my tree in the v6.8 merge window.

[0] https://github.com/facebook/zstd/tree/v1.5.5-kernel
[1] https://keyserver.ubuntu.com/pks/lookup?search=EF8FE99528B52FFD&fingerprint=on&op=index

Nick Terrell (2):
zstd: import upstream v1.5.5
zstd: Backport Huffman speed improvement from upstream

 include/linux/zstd.h                          |    2 +-
 include/linux/zstd_errors.h                   |   23 +-
 include/linux/zstd_lib.h                      |  697 +++++--
 lib/zstd/Makefile                             |    2 +-
 lib/zstd/common/allocations.h                 |   56 +
 lib/zstd/common/bits.h                        |  149 ++
 lib/zstd/common/bitstream.h                   |   53 +-
 lib/zstd/common/compiler.h                    |   14 +-
 lib/zstd/common/cpu.h                         |    3 +-
 lib/zstd/common/debug.c                       |    3 +-
 lib/zstd/common/debug.h                       |    3 +-
 lib/zstd/common/entropy_common.c              |   42 +-
 lib/zstd/common/error_private.c               |   12 +-
 lib/zstd/common/error_private.h               |    3 +-
 lib/zstd/common/fse.h                         |   89 +-
 lib/zstd/common/fse_decompress.c              |   94 +-
 lib/zstd/common/huf.h                         |  222 +--
 lib/zstd/common/mem.h                         |    2 +-
 lib/zstd/common/portability_macros.h          |   26 +-
 lib/zstd/common/zstd_common.c                 |   38 +-
 lib/zstd/common/zstd_deps.h                   |   16 +-
 lib/zstd/common/zstd_internal.h               |   99 +-
 lib/zstd/compress/clevels.h                   |    3 +-
 lib/zstd/compress/fse_compress.c              |   59 +-
 lib/zstd/compress/hist.c                      |    3 +-
 lib/zstd/compress/hist.h                      |    3 +-
 lib/zstd/compress/huf_compress.c              |  372 ++--
 lib/zstd/compress/zstd_compress.c             | 1762 ++++++++++++-----
 lib/zstd/compress/zstd_compress_internal.h    |  333 +++-
 lib/zstd/compress/zstd_compress_literals.c    |  155 +-
 lib/zstd/compress/zstd_compress_literals.h    |   25 +-
 lib/zstd/compress/zstd_compress_sequences.c   |    7 +-
 lib/zstd/compress/zstd_compress_sequences.h   |    3 +-
 lib/zstd/compress/zstd_compress_superblock.c  |   47 +-
 lib/zstd/compress/zstd_compress_superblock.h  |    3 +-
 lib/zstd/compress/zstd_cwksp.h                |  149 +-
 lib/zstd/compress/zstd_double_fast.c          |  129 +-
 lib/zstd/compress/zstd_double_fast.h          |    6 +-
 lib/zstd/compress/zstd_fast.c                 |  582 ++++--
 lib/zstd/compress/zstd_fast.h                 |    6 +-
 lib/zstd/compress/zstd_lazy.c                 |  518 ++---
 lib/zstd/compress/zstd_lazy.h                 |    7 +-
 lib/zstd/compress/zstd_ldm.c                  |   11 +-
 lib/zstd/compress/zstd_ldm.h                  |    3 +-
 lib/zstd/compress/zstd_ldm_geartab.h          |    3 +-
 lib/zstd/compress/zstd_opt.c                  |  187 +-
 lib/zstd/compress/zstd_opt.h                  |    3 +-
 lib/zstd/decompress/huf_decompress.c          |  770 ++++---
 lib/zstd/decompress/zstd_ddict.c              |    9 +-
 lib/zstd/decompress/zstd_ddict.h              |    3 +-
 lib/zstd/decompress/zstd_decompress.c         |  261 ++-
 lib/zstd/decompress/zstd_decompress_block.c   |  283 ++-
 lib/zstd/decompress/zstd_decompress_block.h   |    8 +-
 .../decompress/zstd_decompress_internal.h     |    7 +-
 lib/zstd/decompress_sources.h                 |    2 +-
 lib/zstd/zstd_common_module.c                 |    5 +-
 lib/zstd/zstd_compress_module.c               |    2 +-
 lib/zstd/zstd_decompress_module.c             |    4 +-
 58 files changed, 4787 insertions(+), 2594 deletions(-)
 create mode 100644 lib/zstd/common/allocations.h
 create mode 100644 lib/zstd/common/bits.h
Signature · 1 line
2.42.1
#2 lore raw

PATCH 2/2 zstd: Backport Huffman speed improvement from upstream

From: Nick Terrell <[email protected]>

Backport upstream commit c7269ad [0] to improve zstd decoding speed.

Updating the kernel to zstd v1.5.5 earlier in this patch series regressed zstd decoding speed. This turned out to be because gcc was not unrolling the inner loops of the Huffman decoder which are executed a constant number of times [1]. This really hurts performance, as we expect this loop to be completely branch-free. This commit fixes the issue by unrolling the loop manually [2].

The commit fixes one more minor issue, which is to mask a variable shift by 0x3F. The shift was guaranteed to be less than 64, but gcc couldn't prove that, and emitted suboptimal code.

Finally, the upstream commit added a build macro HUF_DISABLE_FAST_DECODE which is not used in the kernel, but is maintained to keep a clean import from upstream.

This commit was generated from upstream signed tag v1.5.5-kernel [3] by:

export ZSTD=/path/to/repo/zstd/
export LINUX=/path/to/repo/linux/
cd "$ZSTD/contrib/linux-kernel"
git checkout v1.5.5-kernel
make import LINUX="$LINUX"

I ran my benchmark & test suite before and after this commit to measure the overall decompression speed benefit. It benchmarks zstd at several compression levels. These benchmarks measure the total time it takes to read data from the compressed filesystem.

Component, Level, Read time delta

Btrfs    ,	    1,	-7.0%
Btrfs    ,	    3,	-3.9%
Btrfs    ,	    5,	-4.7%
Btrfs    ,	    7,	-5.5%
Btrfs    ,	    9,	-2.4%
Squashfs ,	    1,	-9.1%
---
 lib/zstd/decompress/huf_decompress.c | 171 ++++++++++++++++-----------
 1 file changed, 105 insertions(+), 66 deletions(-)
lib/zstd/decompress/huf_decompress.c +105 -66
index d172e35fbd9a..db670d71fdab 100644
@@ -35,6 +35,12 @@
 *  Macros
 ****************************************************************/
 
+#ifdef HUF_DISABLE_FAST_DECODE
+# define HUF_ENABLE_FAST_DECODE 0
+#else
+# define HUF_ENABLE_FAST_DECODE 1
+#endif
+
 /* These two optional macros force the use one way or another of the two
  * Huffman decompression implementations. You can't force in both directions
  * at the same time.
@@ -289,6 +295,24 @@ static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArg
     return 0;
 }
 
+/* Calls X(N) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM(X) \
+    {                             \
+        X(0)                      \
+        X(1)                      \
+        X(2)                      \
+        X(3)                      \
+    }
+
+/* Calls X(N, var) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM_WITH_VAR(X, var) \
+    {                                           \
+        X(0, (var))                             \
+        X(1, (var))                             \
+        X(2, (var))                             \
+        X(3, (var))                             \
+    }
+
 
 #ifndef HUF_FORCE_DECOMPRESS_X2
 
@@ -702,7 +726,6 @@ void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
     for (;;) {
         BYTE* olimit;
         int stream;
-        int symbol;
 
         /* Assert loop preconditions */
 #ifndef NDEBUG
@@ -749,27 +772,42 @@ void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
         }
 #endif
 
+#define HUF_4X1_DECODE_SYMBOL(_stream, _symbol)                 \
+    {                                                           \
+        int const index = (int)(bits[(_stream)] >> 53);         \
+        int const entry = (int)dtable[index];                   \
+        bits[(_stream)] <<= (entry & 0x3F);                     \
+        op[(_stream)][(_symbol)] = (BYTE)((entry >> 8) & 0xFF); \
+    }
+
+#define HUF_4X1_RELOAD_STREAM(_stream)                              \
+    {                                                               \
+        int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
+        int const nbBits = ctz & 7;                                 \
+        int const nbBytes = ctz >> 3;                               \
+        op[(_stream)] += 5;                                         \
+        ip[(_stream)] -= nbBytes;                                   \
+        bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1;            \
+        bits[(_stream)] <<= nbBits;                                 \
+    }
+
+        /* Manually unroll the loop because compilers don't consistently
+         * unroll the inner loops, which destroys performance.
+         */
         do {
             /* Decode 5 symbols in each of the 4 streams */
-            for (symbol = 0; symbol < 5; ++symbol) {
-                for (stream = 0; stream < 4; ++stream) {
-                    int const index = (int)(bits[stream] >> 53);
-                    int const entry = (int)dtable[index];
-                    bits[stream] <<= (entry & 63);
-                    op[stream][symbol] = (BYTE)((entry >> 8) & 0xFF);
-                }
-            }
-            /* Reload the bitstreams */
-            for (stream = 0; stream < 4; ++stream) {
-                int const ctz = ZSTD_countTrailingZeros64(bits[stream]);
-                int const nbBits = ctz & 7;
-                int const nbBytes = ctz >> 3;
-                op[stream] += 5;
-                ip[stream] -= nbBytes;
-                bits[stream] = MEM_read64(ip[stream]) | 1;
-                bits[stream] <<= nbBits;
-            }
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 0)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 1)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 2)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 3)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X1_DECODE_SYMBOL, 4)
+
+            /* Reload each of the 4 the bitstreams */
+            HUF_4X_FOR_EACH_STREAM(HUF_4X1_RELOAD_STREAM)
         } while (op[3] < olimit);
+
+#undef HUF_4X1_DECODE_SYMBOL
+#undef HUF_4X1_RELOAD_STREAM
     }
 
 _out:
@@ -865,7 +903,7 @@ static size_t HUF_decompress4X1_usingDTable_internal(void* dst, size_t dstSize,
     }
 #endif
 
-    if (!(flags & HUF_flags_disableFast)) {
+    if (HUF_ENABLE_FAST_DECODE && !(flags & HUF_flags_disableFast)) {
         size_t const ret = HUF_decompress4X1_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);
         if (ret != 0)
             return ret;
@@ -1487,7 +1525,6 @@ void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
     for (;;) {
         BYTE* olimit;
         int stream;
-        int symbol;
 
         /* Assert loop preconditions */
 #ifndef NDEBUG
@@ -1544,54 +1581,56 @@ void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs*
         }
 #endif
 
+#define HUF_4X2_DECODE_SYMBOL(_stream, _decode3)        \
+    if ((_decode3) || (_stream) != 3) {                 \
+        int const index = (int)(bits[(_stream)] >> 53); \
+        HUF_DEltX2 const entry = dtable[index];         \
+        MEM_write16(op[(_stream)], entry.sequence);     \
+        bits[(_stream)] <<= (entry.nbBits) & 0x3F;      \
+        op[(_stream)] += (entry.length);                \
+    }
+
+#define HUF_4X2_RELOAD_STREAM(_stream)                                  \
+    {                                                                   \
+        HUF_4X2_DECODE_SYMBOL(3, 1)                                     \
+        {                                                               \
+            int const ctz = ZSTD_countTrailingZeros64(bits[(_stream)]); \
+            int const nbBits = ctz & 7;                                 \
+            int const nbBytes = ctz >> 3;                               \
+            ip[(_stream)] -= nbBytes;                                   \
+            bits[(_stream)] = MEM_read64(ip[(_stream)]) | 1;            \
+            bits[(_stream)] <<= nbBits;                                 \
+        }                                                               \
+    }
+
+        /* Manually unroll the loop because compilers don't consistently
+         * unroll the inner loops, which destroys performance.
+         */
         do {
-            /* Do 5 table lookups for each of the first 3 streams */
-            for (symbol = 0; symbol < 5; ++symbol) {
-                for (stream = 0; stream < 3; ++stream) {
-                    int const index = (int)(bits[stream] >> 53);
-                    HUF_DEltX2 const entry = dtable[index];
-                    MEM_write16(op[stream], entry.sequence);
-                    bits[stream] <<= (entry.nbBits);
-                    op[stream] += (entry.length);
-                }
-            }
-            /* Do 1 table lookup from the final stream */
-            {
-                int const index = (int)(bits[3] >> 53);
-                HUF_DEltX2 const entry = dtable[index];
-                MEM_write16(op[3], entry.sequence);
-                bits[3] <<= (entry.nbBits);
-                op[3] += (entry.length);
-            }
-            /* Do 4 table lookups from the final stream & reload bitstreams */
-            for (stream = 0; stream < 4; ++stream) {
-                /* Do a table lookup from the final stream.
-                 * This is interleaved with the reloading to reduce register
-                 * pressure. This shouldn't be necessary, but compilers can
-                 * struggle with codegen with high register pressure.
-                 */
-                {
-                    int const index = (int)(bits[3] >> 53);
-                    HUF_DEltX2 const entry = dtable[index];
-                    MEM_write16(op[3], entry.sequence);
-                    bits[3] <<= (entry.nbBits);
-                    op[3] += (entry.length);
-                }
-                /* Reload the bistreams. The final bitstream must be reloaded
-                 * after the 5th symbol was decoded.
-                 */
-                {
-                    int const ctz = ZSTD_countTrailingZeros64(bits[stream]);
-                    int const nbBits = ctz & 7;
-                    int const nbBytes = ctz >> 3;
-                    ip[stream] -= nbBytes;
-                    bits[stream] = MEM_read64(ip[stream]) | 1;
-                    bits[stream] <<= nbBits;
-                }
-            }
+            /* Decode 5 symbols from each of the first 3 streams.
+             * The final stream will be decoded during the reload phase
+             * to reduce register pressure.
+             */
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0)
+            HUF_4X_FOR_EACH_STREAM_WITH_VAR(HUF_4X2_DECODE_SYMBOL, 0)
+
+            /* Decode one symbol from the final stream */
+            HUF_4X2_DECODE_SYMBOL(3, 1)
+
+            /* Decode 4 symbols from the final stream & reload bitstreams.
+             * The final stream is reloaded last, meaning that all 5 symbols
+             * are decoded from the final stream before it is reloaded.
+             */
+            HUF_4X_FOR_EACH_STREAM(HUF_4X2_RELOAD_STREAM)
         } while (op[3] < olimit);
     }
 
+#undef HUF_4X2_DECODE_SYMBOL
+#undef HUF_4X2_RELOAD_STREAM
+
 _out:
 
     /* Save the final values of each of the state variables back to args. */
@@ -1676,7 +1715,7 @@ static size_t HUF_decompress4X2_usingDTable_internal(void* dst, size_t dstSize,
     }
 #endif
 
-    if (!(flags & HUF_flags_disableFast)) {
+    if (HUF_ENABLE_FAST_DECODE && !(flags & HUF_flags_disableFast)) {
         size_t const ret = HUF_decompress4X2_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn);
         if (ret != 0)
             return ret;
Signature · 1 line
2.42.1
#1 lore raw

PATCH 1/2 zstd: import upstream v1.5.5

In reply to Nick Terrell

From: Nick Terrell <[email protected]>

Import upstream zstd v1.5.5 to expose upstream's QAT integration.

Import from upstream commit 58b3ef79 [0]. This is one commit before the tag v1.5.5-kernel [1], which is signed with upstream's signing key. The next patch in the series imports from v1.5.5-kernel, and is included in the series, rather than just importing directly from v1.5.5-kernel, because it is a non-trivial patch applied to improve the kernel's decompression speed. This commit contains 3 backported patches on top of v1.5.5: Two from the Linux copy of zstd, and one from upstream's dev branch.

In addition to keeping the kernel's copy of zstd up to date, this update was requested by Intel to expose upstream zstd's external match provider API to the kernel, which allows QAT to accelerate the LZ match finding stage.

This commit was generated by:

export ZSTD=/path/to/repo/zstd/
export LINUX=/path/to/repo/linux/
cd "$ZSTD/contrib/linux-kernel"
git checkout v1.5.5-kernel~
make import LINUX="$LINUX"

I tested and benchmarked this commit on x86-64 with gcc-13.2.1 on an Intel i9-9900K by running my benchmark scripts that benchmark zstd's performance in btrfs and squashfs compressed filesystems. This commit improves compression speed, especially for higher compression levels, and regresses decompression speed. But the decompression speed regression is addressed by the next patch in the series.

Component, Level, C. time delta, size delta, D. time delta

Btrfs    ,	    1,	        -1.9%,	     +0.0%,	        +9.5%
Btrfs    ,	    3,	        -5.6%,	     +0.0%,	        +7.4%
Btrfs    ,	    5,	        -4.9%,	     +0.0%,	        +5.0%
Btrfs    ,	    7,	        -5.7%,	     +0.0%,	        +5.2%
Btrfs    ,	    9,	        -5.7%,	     +0.0%,	        +4.0%
Squashfs ,	    1,	          N/A,	      0.0%,	       +11.6%

I also boot tested with a zstd compressed kernel on i386 and aarch64.

---
 include/linux/zstd.h                          |    2 +-
 include/linux/zstd_errors.h                   |   23 +-
 include/linux/zstd_lib.h                      |  697 +++++--
 lib/zstd/Makefile                             |    2 +-
 lib/zstd/common/allocations.h                 |   56 +
 lib/zstd/common/bits.h                        |  149 ++
 lib/zstd/common/bitstream.h                   |   53 +-
 lib/zstd/common/compiler.h                    |   14 +-
 lib/zstd/common/cpu.h                         |    3 +-
 lib/zstd/common/debug.c                       |    3 +-
 lib/zstd/common/debug.h                       |    3 +-
 lib/zstd/common/entropy_common.c              |   42 +-
 lib/zstd/common/error_private.c               |   12 +-
 lib/zstd/common/error_private.h               |    3 +-
 lib/zstd/common/fse.h                         |   89 +-
 lib/zstd/common/fse_decompress.c              |   94 +-
 lib/zstd/common/huf.h                         |  222 +--
 lib/zstd/common/mem.h                         |    2 +-
 lib/zstd/common/portability_macros.h          |   26 +-
 lib/zstd/common/zstd_common.c                 |   38 +-
 lib/zstd/common/zstd_deps.h                   |   16 +-
 lib/zstd/common/zstd_internal.h               |   99 +-
 lib/zstd/compress/clevels.h                   |    3 +-
 lib/zstd/compress/fse_compress.c              |   59 +-
 lib/zstd/compress/hist.c                      |    3 +-
 lib/zstd/compress/hist.h                      |    3 +-
 lib/zstd/compress/huf_compress.c              |  372 ++--
 lib/zstd/compress/zstd_compress.c             | 1762 ++++++++++++-----
 lib/zstd/compress/zstd_compress_internal.h    |  333 +++-
 lib/zstd/compress/zstd_compress_literals.c    |  155 +-
 lib/zstd/compress/zstd_compress_literals.h    |   25 +-
 lib/zstd/compress/zstd_compress_sequences.c   |    7 +-
 lib/zstd/compress/zstd_compress_sequences.h   |    3 +-
 lib/zstd/compress/zstd_compress_superblock.c  |   47 +-
 lib/zstd/compress/zstd_compress_superblock.h  |    3 +-
 lib/zstd/compress/zstd_cwksp.h                |  149 +-
 lib/zstd/compress/zstd_double_fast.c          |  129 +-
 lib/zstd/compress/zstd_double_fast.h          |    6 +-
 lib/zstd/compress/zstd_fast.c                 |  582 ++++--
 lib/zstd/compress/zstd_fast.h                 |    6 +-
 lib/zstd/compress/zstd_lazy.c                 |  518 ++---
 lib/zstd/compress/zstd_lazy.h                 |    7 +-
 lib/zstd/compress/zstd_ldm.c                  |   11 +-
 lib/zstd/compress/zstd_ldm.h                  |    3 +-
 lib/zstd/compress/zstd_ldm_geartab.h          |    3 +-
 lib/zstd/compress/zstd_opt.c                  |  187 +-
 lib/zstd/compress/zstd_opt.h                  |    3 +-
 lib/zstd/decompress/huf_decompress.c          |  731 ++++---
 lib/zstd/decompress/zstd_ddict.c              |    9 +-
 lib/zstd/decompress/zstd_ddict.h              |    3 +-
 lib/zstd/decompress/zstd_decompress.c         |  261 ++-
 lib/zstd/decompress/zstd_decompress_block.c   |  283 ++-
 lib/zstd/decompress/zstd_decompress_block.h   |    8 +-
 .../decompress/zstd_decompress_internal.h     |    7 +-
 lib/zstd/decompress_sources.h                 |    2 +-
 lib/zstd/zstd_common_module.c                 |    5 +-
 lib/zstd/zstd_compress_module.c               |    2 +-
 lib/zstd/zstd_decompress_module.c             |    4 +-
 58 files changed, 4748 insertions(+), 2594 deletions(-)
 create mode 100644 lib/zstd/common/allocations.h
 create mode 100644 lib/zstd/common/bits.h
include/linux/zstd.h +1 -1
index 113408eef6ec..f109d49f43f8 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
include/linux/zstd_errors.h +20 -3
index 58b6dd45a969..6d5cf55f0bf3 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -17,8 +18,17 @@
 
 
 /* =====   ZSTDERRORLIB_API : control library symbols visibility   ===== */
-#define ZSTDERRORLIB_VISIBILITY 
-#define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBILITY
+#define ZSTDERRORLIB_VISIBLE 
+
+#ifndef ZSTDERRORLIB_HIDDEN
+#  if (__GNUC__ >= 4) && !defined(__MINGW32__)
+#    define ZSTDERRORLIB_HIDDEN __attribute__ ((visibility ("hidden")))
+#  else
+#    define ZSTDERRORLIB_HIDDEN
+#  endif
+#endif
+
+#define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBLE
 
 /*-*********************************************
  *  Error codes list
@@ -43,14 +53,17 @@ typedef enum {
   ZSTD_error_frameParameter_windowTooLarge = 16,
   ZSTD_error_corruption_detected = 20,
   ZSTD_error_checksum_wrong      = 22,
+  ZSTD_error_literals_headerWrong = 24,
   ZSTD_error_dictionary_corrupted      = 30,
   ZSTD_error_dictionary_wrong          = 32,
   ZSTD_error_dictionaryCreation_failed = 34,
   ZSTD_error_parameter_unsupported   = 40,
+  ZSTD_error_parameter_combination_unsupported = 41,
   ZSTD_error_parameter_outOfBound    = 42,
   ZSTD_error_tableLog_tooLarge       = 44,
   ZSTD_error_maxSymbolValue_tooLarge = 46,
   ZSTD_error_maxSymbolValue_tooSmall = 48,
+  ZSTD_error_stabilityCondition_notRespected = 50,
   ZSTD_error_stage_wrong       = 60,
   ZSTD_error_init_missing      = 62,
   ZSTD_error_memory_allocation = 64,
@@ -58,11 +71,15 @@ typedef enum {
   ZSTD_error_dstSize_tooSmall = 70,
   ZSTD_error_srcSize_wrong    = 72,
   ZSTD_error_dstBuffer_null   = 74,
+  ZSTD_error_noForwardProgress_destFull = 80,
+  ZSTD_error_noForwardProgress_inputEmpty = 82,
   /* following error codes are __NOT STABLE__, they can be removed or changed in future versions */
   ZSTD_error_frameIndex_tooLarge = 100,
   ZSTD_error_seekableIO          = 102,
   ZSTD_error_dstBuffer_wrong     = 104,
   ZSTD_error_srcBuffer_wrong     = 105,
+  ZSTD_error_sequenceProducer_failed = 106,
+  ZSTD_error_externalSequences_invalid = 107,
   ZSTD_error_maxCode = 120  /* never EVER use this value directly, it can change in future versions! Use ZSTD_isError() instead */
 } ZSTD_ErrorCode;
 
include/linux/zstd_lib.h +567 -130

1,132 lines not on this page · raw message

lib/zstd/Makefile +1 -1
index 20f08c644b71..464c410b2768 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 # ################################################################
-# Copyright (c) Facebook, Inc.
+# Copyright (c) Meta Platforms, Inc. and affiliates.
 # All rights reserved.
 #
 # This source code is licensed under both the BSD-style license (found in the
lib/zstd/common/allocations.h new file +56 -0

56 lines not on this page · raw message

lib/zstd/common/bits.h new file +149 -0

149 lines not on this page · raw message

lib/zstd/common/bitstream.h +14 -39

113 lines not on this page · raw message

lib/zstd/common/compiler.h +13 -1
index c42d39faf9bd..c437e0975575 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -179,6 +180,17 @@
 *  Sanitizer
 *****************************************************************/
 
+/* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
+ * abundance of caution, disable our custom poisoning on mingw. */
+#ifdef __MINGW32__
+#ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
+#define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
+#endif
+#ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
+#define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
+#endif
+#endif
+
 
 
 #endif /* ZSTD_COMPILER_H */
lib/zstd/common/cpu.h +2 -1
index 0db7b42407ee..d8319a2bef4c 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
lib/zstd/common/debug.c +2 -1
index bb863c9ea616..e56ff6464e91 100644
@@ -1,7 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 /* ******************************************************************
  * debug
  * Part of FSE library
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  *
  * You can contact the author at :
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
lib/zstd/common/debug.h +2 -1
index 6dd88d1fbd02..da0dbfc614b8 100644
@@ -1,7 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /* ******************************************************************
  * debug
  * Part of FSE library
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  *
  * You can contact the author at :
  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
lib/zstd/common/entropy_common.c +13 -29

105 lines not on this page · raw message

lib/zstd/common/error_private.c +10 -2
index 6d1135f8c373..a4062d30d170 100644
@@ -1,5 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -27,9 +28,11 @@ const char* ERR_getErrorString(ERR_enum code)
     case PREFIX(version_unsupported): return "Version not supported";
     case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
     case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding";
-    case PREFIX(corruption_detected): return "Corrupted block detected";
+    case PREFIX(corruption_detected): return "Data corruption detected";
     case PREFIX(checksum_wrong): return "Restored data doesn't match checksum";
+    case PREFIX(literals_headerWrong): return "Header of Literals' block doesn't respect format specification";
     case PREFIX(parameter_unsupported): return "Unsupported parameter";
+    case PREFIX(parameter_combination_unsupported): return "Unsupported combination of parameters";
     case PREFIX(parameter_outOfBound): return "Parameter is out of bound";
     case PREFIX(init_missing): return "Context should be init first";
     case PREFIX(memory_allocation): return "Allocation error : not enough memory";
@@ -38,17 +41,22 @@ const char* ERR_getErrorString(ERR_enum code)
     case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
     case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
     case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
+    case PREFIX(stabilityCondition_notRespected): return "pledged buffer stability condition is not respected";
     case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
     case PREFIX(dictionary_wrong): return "Dictionary mismatch";
     case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples";
     case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
     case PREFIX(srcSize_wrong): return "Src size is incorrect";
     case PREFIX(dstBuffer_null): return "Operation on NULL destination buffer";
+    case PREFIX(noForwardProgress_destFull): return "Operation made no progress over multiple calls, due to output buffer being full";
+    case PREFIX(noForwardProgress_inputEmpty): return "Operation made no progress over multiple calls, due to input being empty";
         /* following error codes are not stable and may be removed or changed in a future version */
     case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
     case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
     case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
     case PREFIX(srcBuffer_wrong): return "Source buffer is wrong";
+    case PREFIX(sequenceProducer_failed): return "Block-level external sequence producer returned an error code";
+    case PREFIX(externalSequences_invalid): return "External sequences are not valid";
     case PREFIX(maxCode):
     default: return notErrorCode;
     }
lib/zstd/common/error_private.h +2 -1
index ca5101e542fa..9a4699a38a88 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
lib/zstd/common/fse.h +6 -83

140 lines not on this page · raw message

lib/zstd/common/fse_decompress.c +8 -86

147 lines not on this page · raw message

lib/zstd/common/huf.h +66 -156

314 lines not on this page · raw message

lib/zstd/common/mem.h +1 -1
index 1d9cc03924ca..a7231822b6e3 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
lib/zstd/common/portability_macros.h +23 -3
index 0e3b2c0a527d..7ede8cf1ffe5 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -12,7 +13,7 @@
 #define ZSTD_PORTABILITY_MACROS_H
 
 /*
- * This header file contains macro defintions to support portability.
+ * This header file contains macro definitions to support portability.
  * This header is shared between C and ASM code, so it MUST only
  * contain macro definitions. It MUST not contain any C code.
  *
@@ -65,7 +66,7 @@
 #endif
 
 /*
- * Only enable assembly for GNUC comptabile compilers,
+ * Only enable assembly for GNUC compatible compilers,
  * because other platforms may not support GAS assembly syntax.
  *
  * Only enable assembly for Linux / MacOS, other platforms may
@@ -90,4 +91,23 @@
  */
 #define ZSTD_ENABLE_ASM_X86_64_BMI2 0
 
+/*
+ * For x86 ELF targets, add .note.gnu.property section for Intel CET in
+ * assembly sources when CET is enabled.
+ *
+ * Additionally, any function that may be called indirectly must begin
+ * with ZSTD_CET_ENDBRANCH.
+ */
+#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \
+    && defined(__has_include)
+# if __has_include(<cet.h>)
+#  include <cet.h>
+#  define ZSTD_CET_ENDBRANCH _CET_ENDBR
+# endif
+#endif
+
+#ifndef ZSTD_CET_ENDBRANCH
+# define ZSTD_CET_ENDBRANCH
+#endif
+
 #endif /* ZSTD_PORTABILITY_MACROS_H */
lib/zstd/common/zstd_common.c +2 -36
index 3d7e35b309b5..44b95b25344a 100644
@@ -1,5 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -14,7 +15,6 @@
 *  Dependencies
 ***************************************/
 #define ZSTD_DEPS_NEED_MALLOC
-#include "zstd_deps.h"   /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
 #include "error_private.h"
 #include "zstd_internal.h"
 
@@ -47,37 +47,3 @@ ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
 /*! ZSTD_getErrorString() :
  *  provides error code string from enum */
 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
-
-
-
-/*=**************************************************************
-*  Custom allocator
-****************************************************************/
-void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
-{
-    if (customMem.customAlloc)
-        return customMem.customAlloc(customMem.opaque, size);
-    return ZSTD_malloc(size);
-}
-
-void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
-{
-    if (customMem.customAlloc) {
-        /* calloc implemented as malloc+memset;
-         * not as efficient as calloc, but next best guess for custom malloc */
-        void* const ptr = customMem.customAlloc(customMem.opaque, size);
-        ZSTD_memset(ptr, 0, size);
-        return ptr;
-    }
-    return ZSTD_calloc(1, size);
-}
-
-void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
-{
-    if (ptr!=NULL) {
-        if (customMem.customFree)
-            customMem.customFree(customMem.opaque, ptr);
-        else
-            ZSTD_free(ptr);
-    }
-}
lib/zstd/common/zstd_deps.h +15 -1
index 2c34e8a33a1c..f931f7d0e294 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
@@ -105,3 +105,17 @@ static uint64_t ZSTD_div64(uint64_t dividend, uint32_t divisor) {
 
 #endif /* ZSTD_DEPS_IO */
 #endif /* ZSTD_DEPS_NEED_IO */
+
+/*
+ * Only requested when MSAN is enabled.
+ * Need:
+ * intptr_t
+ */
+#ifdef ZSTD_DEPS_NEED_STDINT
+#ifndef ZSTD_DEPS_STDINT
+#define ZSTD_DEPS_STDINT
+
+/* intptr_t already provided by ZSTD_DEPS_COMMON */
+
+#endif /* ZSTD_DEPS_STDINT */
+#endif /* ZSTD_DEPS_NEED_STDINT */
lib/zstd/common/zstd_internal.h +18 -81

171 lines not on this page · raw message

lib/zstd/compress/clevels.h +2 -1
index d9a76112ec3a..6ab8be6532ef 100644
@@ -1,5 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /*
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  * All rights reserved.
  *
  * This source code is licensed under both the BSD-style license (found in the
lib/zstd/compress/fse_compress.c +8 -51

107 lines not on this page · raw message

lib/zstd/compress/hist.c +2 -1
index 3ddc6dfb6894..0b12587cc14b 100644
@@ -1,7 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
 /* ******************************************************************
  * hist : Histogram functions
  * part of Finite State Entropy project
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  *
  *  You can contact the author at :
  *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
lib/zstd/compress/hist.h +2 -1
index fc1830abc9c6..f7687b0fc20a 100644
@@ -1,7 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
 /* ******************************************************************
  * hist : Histogram functions
  * part of Finite State Entropy project
- * Copyright (c) Yann Collet, Facebook, Inc.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
  *
  *  You can contact the author at :
  *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
lib/zstd/compress/huf_compress.c +235 -137

244 lines not on this page · raw message

lib/zstd/compress/zstd_compress.c +1227 -535

1,762 lines not on this page · raw message

lib/zstd/compress/zstd_compress_internal.h +226 -107

333 lines not on this page · raw message

lib/zstd/compress/zstd_compress_literals.c +116 -39

155 lines not on this page · raw message

lib/zstd/compress/zstd_compress_literals.h +17 -8
index 9775fb97cb70..a2a85d6b69e5 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_compress_sequences.c +4 -3
index 21ddc1b37acf..5c028c78d889 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_compress_sequences.h +2 -1
index 7991364c2f71..7fe6f4ff5cf2 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_compress_superblock.c +26 -21

47 lines not on this page · raw message

lib/zstd/compress/zstd_compress_superblock.h +2 -1
index 224ece79546e..826bbc9e029b 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_cwksp.h +94 -55

149 lines not on this page · raw message

lib/zstd/compress/zstd_double_fast.c +96 -33

129 lines not on this page · raw message

lib/zstd/compress/zstd_double_fast.h +4 -2
index 6822bde65a1d..0204f12e4cf7 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_fast.c +434 -148

582 lines not on this page · raw message

lib/zstd/compress/zstd_fast.h +4 -2
index fddc2f532d21..e64d9e1b2d39 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_lazy.c +287 -231

518 lines not on this page · raw message

lib/zstd/compress/zstd_lazy.h +5 -2
index e5bdf4df8dde..9505bed93c03 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_ldm.c +6 -5
index dd86fc83e7dd..b7da76b0db7c 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_ldm.h +2 -1
index fbc6a5e88fd7..c540731abde7 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_ldm_geartab.h +2 -1
index 647f865be290..cfccfc46f6f7 100644

Lines of this file are not shown. View the raw message.

lib/zstd/compress/zstd_opt.c +107 -80

187 lines not on this page · raw message

lib/zstd/compress/zstd_opt.h +2 -1
index 22b862858ba7..faa73ff4b03d 100644

Lines of this file are not shown. View the raw message.

lib/zstd/decompress/huf_decompress.c +435 -296

731 lines not on this page · raw message

lib/zstd/decompress/zstd_ddict.c +5 -4
index dbbc7919de53..30ef65e1ab5c 100644

Lines of this file are not shown. View the raw message.

lib/zstd/decompress/zstd_ddict.h +2 -1
index 8c1a79d666f8..de459a0dacd1 100644

Lines of this file are not shown. View the raw message.

lib/zstd/decompress/zstd_decompress.c +184 -77

261 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress_block.c +202 -81

283 lines not on this page · raw message

lib/zstd/decompress/zstd_decompress_block.h +7 -1
index 3d2d57a5d25a..5888e6cc788b 100644

Lines of this file are not shown. View the raw message.

lib/zstd/decompress/zstd_decompress_internal.h +5 -2
index 98102edb6a83..32f79fb2873d 100644

Lines of this file are not shown. View the raw message.

lib/zstd/decompress_sources.h +1 -1
index a06ca187aab5..8a47eb2a4514 100644

Lines of this file are not shown. View the raw message.

lib/zstd/zstd_common_module.c +1 -4
index 22686e367e6f..466828e35752 100644

Lines of this file are not shown. View the raw message.

lib/zstd/zstd_compress_module.c +1 -1
index 04e1b5c01d9b..8ecf43226af2 100644

Lines of this file are not shown. View the raw message.

lib/zstd/zstd_decompress_module.c +2 -2
index f4ed952ed485..7d31518e9d5a 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.42.1

Part of the diff is not shown here. View the raw message.

#3 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

In reply to Nick Terrell
Quoted 9 lines · Nick Terrell
lib/zstd/decompress/huf_decompress.c +8 -0
+/* Calls X(N) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM(X) \
+    {                             \
+        X(0)                      \
+        X(1)                      \
+        X(2)                      \
+        X(3)                      \
+    }

lib/zstd/decompress/huf_decompress.c static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArg · line 315

Quoted 10 lines
lib/zstd/decompress/huf_decompress.c +10 -0
+
+/* Calls X(N, var) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM_WITH_VAR(X, var) \
+    {                                           \
+        X(0, (var))                             \
+        X(1, (var))                             \
+        X(2, (var))                             \
+        X(3, (var))                             \
+    }
+

What shitty compilers do you need to be compatible with?

Because at least for Linux, the above is one single #define:

    #define FOUR(X,y...) do { \
        X(0,##y); X(1,##y); X(2,##y); X(3,##y); \
    } while (0)

and it does the right thing for any number of arguments, ie

    FOUR(fn)
    FOUR(fn1,a)
    FOUR(fn2,a,b)

expands to

    do { fn(0); fn(1); fn(2); fn(3); } while (0)
    do { fn1(0,a); fn1(1,a); fn1(2,a); fn1(3,a); } while (0)
    do { fn2(0,a,b); fn2(1,a,b); fn2(2,a,b); fn2(3,a,b); } while (0)

so unless you need to support some completely garbage compiler upstream, please just do the single #define.

Linus

#4 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

Quoted 44 lines · Linus Torvalds

!-------------------------------------------------------------------|
This Message Is From an External Sender

|-------------------------------------------------------------------!

Quoted 9 lines · Nick Terrell
lib/zstd/decompress/huf_decompress.c +8 -0
+/* Calls X(N) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM(X) \
+    {                             \
+        X(0)                      \
+        X(1)                      \
+        X(2)                      \
+        X(3)                      \
+    }

lib/zstd/decompress/huf_decompress.c static size_t HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArg · line 315

Quoted 10 lines
lib/zstd/decompress/huf_decompress.c +10 -0
+
+/* Calls X(N, var) for each stream 0, 1, 2, 3. */
+#define HUF_4X_FOR_EACH_STREAM_WITH_VAR(X, var) \
+    {                                           \
+        X(0, (var))                             \
+        X(1, (var))                             \
+        X(2, (var))                             \
+        X(3, (var))                             \
+    }
+

What shitty compilers do you need to be compatible with?

Because at least for Linux, the above is one single #define:

   #define FOUR(X,y...) do { \
       X(0,##y); X(1,##y); X(2,##y); X(3,##y); \
   } while (0)

and it does the right thing for any number of arguments, ie

   FOUR(fn)
   FOUR(fn1,a)
   FOUR(fn2,a,b)
Quoted 8 lines

expands to

   do { fn(0); fn(1); fn(2); fn(3); } while (0)
   do { fn1(0,a); fn1(1,a); fn1(2,a); fn1(3,a); } while (0)
   do { fn2(0,a,b); fn2(1,a,b); fn2(2,a,b); fn2(3,a,b); } while (0)

so unless you need to support some completely garbage compiler upstream, please just do the single #define.

Upstream zstd maintains strict C89 compatibility. We do use compiler extensions when available, but only when we can detect the feature and provide a correct fallback when it isn’t available. E.g. __builtin_ctz(). Empty variadic macros fails our C89 compatibility CI, as well as our Visual Studios CI [0].

We’ve discussed dropping C89 support. The consensus was that if we were starting the project today we’d stick to C11, but that it is worth keeping C89 support for Zstd. We’ve seen people compile zstd in some wacky environments.

W.r.t. do { } while (0), our older Visual Studios CI jobs failed on the do { } while (0) macros, because it complained about constant false branches. However, it looks like our current Visual Studios CI jobs accept do { } while (0) [1]. So I’ve opened an upstream issue to modernize all of macros [2].

Best,
Nick Terrell

[0] https://github.com/terrelln/zstd/pull/3
[1] https://github.com/terrelln/zstd/pull/4
[2] https://github.com/facebook/zstd/issues/3830

Quoted 1 line

Linus

#5 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

Quoted 4 lines · Nick Terrell

W.r.t. do { } while (0), our older Visual Studios CI jobs failed on the do { } while (0) macros, because it complained about constant false branches.

Wow. That is some truly incompetent compiler people.

I mean, really. As in "Why would you ever use that kind of garbage" incompetence.

Honestly, any coding rule that includes "don't use the do-while-zero construct" is actively broken shit.

Please just fix your upstream rules. Because they are incredible garbage.

Linus

#6 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

Quoted 17 lines · Linus Torvalds

!-------------------------------------------------------------------|
This Message Is From an External Sender

|-------------------------------------------------------------------!

Quoted 4 lines · Nick Terrell

W.r.t. do { } while (0), our older Visual Studios CI jobs failed on the do { } while (0) macros, because it complained about constant false branches.

Wow. That is some truly incompetent compiler people.

Quoted 7 lines

I mean, really. As in "Why would you ever use that kind of garbage" incompetence.

Honestly, any coding rule that includes "don't use the do-while-zero construct" is actively broken shit.

Please just fix your upstream rules. Because they are incredible garbage.

Yeah, that’s the plan. Visual Studios fixed that compiler bug in VS2015 [0], so we should be safe to migrate to safer macros. I’m going through and doing that now, and will backport that to the kernel on top of this series.

[0] https://stackoverflow.com/a/36658783

Quoted 1 line

Linus

#7 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

Quoted 8 lines · Nick Terrell
Quoted 5 lines

Honestly, any coding rule that includes "don't use the do-while-zero construct" is actively broken shit.

Please just fix your upstream rules. Because they are incredible garbage.

Yeah, that’s the plan. Visual Studios fixed that compiler bug in VS2015 [0], so we should be safe to migrate to safer macros.

I don't even use MSVS, but a minute of googling shows that you should never have done that silly "avoid sane C", and you should always just have done

#pragma warning (disable: 4127)

for MSVC.

Honestly, the fact that the result was instead to disable that standard - and required - construct in the project makes me worry about the whole zstd thing. WTF?

The do-while-zero construct is _so_ important that there are (sane) projects that literally *require* the use of it. See for example MISRA code safety rules.

The kernel rules aren't quite that strict, but yes, do-while-zero is very much "you should *absolutely* do this" along with all the usual "make sure you have parentheses around macro arguments" rules.

We had some RFC patches for this area:

https://lore.kernel.org/all/[email protected]/

And on that note, when I googled for the solution to the MSVC brain damage, I was distressed by how many hits I saw where people thought the do-while-zero pattern was some "legacy pattern".

That just shows that there are lots of incompetent people simply do not understand why it's actually *required* for reliable parsing of macros. This is not some "historical stylistic" issue, it's literally a correctness issue for generic macro usage.

Linus

#8 lore raw

Re: [PATCH 2/2] zstd: Backport Huffman speed improvement from upstream

Quoted 18 lines · Linus Torvalds
Quoted 8 lines · Nick Terrell
Quoted 5 lines

Honestly, any coding rule that includes "don't use the do-while-zero construct" is actively broken shit.

Please just fix your upstream rules. Because they are incredible garbage.

Yeah, that’s the plan. Visual Studios fixed that compiler bug in VS2015 [0], so we should be safe to migrate to safer macros.

I don't even use MSVS, but a minute of googling shows that you should never have done that silly "avoid sane C", and you should always just have done

Quoted 7 lines

#pragma warning (disable: 4127)

for MSVC.

Honestly, the fact that the result was instead to disable that standard - and required - construct in the project makes me worry about the whole zstd thing. WTF?

Admittedly our coding guidelines are overly conservative. And here we are updating to our macros to use the do { } while (0) construct in this PR [0].

However, we are also very conservative in our testing. We have very extensive coverage-guided fuzz testing running continuously for safety of (de)compressing untrusted data, round-trip correctness, and more.

We take security & correctness very seriously. If you have any questions I’d be happy to answer them, and I should collect our testing process publicly in one place, so we can reference that.

If you have any further suggestions I’m very open to them, and I am grateful for the time you’re taking to improve zstd.

[0] https://github.com/facebook/zstd/pull/3831

Quoted 11 lines

The do-while-zero construct is _so_ important that there are (sane) projects that literally *require* the use of it. See for example MISRA code safety rules.

The kernel rules aren't quite that strict, but yes, do-while-zero is very much "you should *absolutely* do this" along with all the usual "make sure you have parentheses around macro arguments" rules.

We had some RFC patches for this area:

https://lore.kernel.org/all/[email protected]/

Agreed.

Quoted 10 lines

And on that note, when I googled for the solution to the MSVC brain damage, I was distressed by how many hits I saw where people thought the do-while-zero pattern was some "legacy pattern".

That just shows that there are lots of incompetent people simply do not understand why it's actually *required* for reliable parsing of macros. This is not some "historical stylistic" issue, it's literally a correctness issue for generic macro usage.

Linus