]> git.kaiwu.me - haproxy.git/commitdiff
IMPORT: slz: update slz to v1.4.0 (commit 197db002f1)
authorWilly Tarreau <w@1wt.eu>
Tue, 24 Nov 2026 08:19:00 +0000 (09:19 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 24 Nov 2026 09:50:37 +0000 (10:50 +0100)
This fixes a bug that doesn't affect haproxy (in case USLZ_FAST_BITS
is set higher than 9 bits), cleans up the code by moving all tables to
slz-tables.h, improves uslz's performance on dynamic tables (which we
now use a lot with text) by not interrupting their decoding when they
alternate with matches (+15% perf on web with 16 kB buffers). This is
now a tagged version, fixes will be picked from branch stable-1.4.

include/import/slz-prv.h
include/import/slz-tables.h
src/slz.c
src/uslz.c

index 147b9581c2c516acdb508f0a83f45a47b6627b97..1fbd20965f4c8fd2544dfb0c5c69277857a5a247 100644 (file)
@@ -209,8 +209,8 @@ static inline uint32_t rbit5(uint32_t v)
 #endif
 
 /* helper function to reverse up to 9 bits in <input> which is expected to be
- * <len> bits long. The function might work with more than 9 bits in certain
- * cases with no guarantees, so don't go over 9.
+ * <len> bits long. The generic version is exact up to 10 bits, use rbit15()
+ * for longer codes.
  */
 static inline uint32_t rbit9(uint32_t input, int len)
 {
@@ -235,4 +235,32 @@ static inline uint32_t rbit9(uint32_t input, int len)
 #endif
 }
 
+/* helper function to reverse up to 16 bits in <input> which is expected to be
+ * <len> bits long, len being 1 to 16, the longest deflate code. It is meant
+ * for table generation, not for the decoding path. On ARM the rbit instruction
+ * covers any length. On other archs we do them the usual way in log2 masks.
+ */
+static inline uint32_t rbit16(uint32_t input, int len)
+{
+#if defined(__aarch64__) || defined(__ARM_ARCH_7A__)
+       /* ARM >= v7 has a fast rbit instruction */
+       input <<= 32 - len; // align left before reversal
+# if defined(__ARM_ARCH_ISA_A64)
+       // 64 bit mode
+       __asm__ ("rbit %w0, %w0" : "+r" (input));
+# else
+       // 32 bit mode (e.g. armv7 compiler building for armv8
+       __asm__ ("rbit %0, %0" : "+r" (input));
+# endif
+       return input;
+
+#else /* not arm */
+       input = ((input >> 1) & 0x5555) | ((input & 0x5555) << 1);
+       input = ((input >> 2) & 0x3333) | ((input & 0x3333) << 2);
+       input = ((input >> 4) & 0x0f0f) | ((input & 0x0f0f) << 4);
+       input = ((input >> 8) & 0x00ff) | ((input & 0x00ff) << 8);
+       return input >> (16 - len);
+#endif
+}
+
 #endif
index 10f1b460975dca5bcbc0c9ed3c89f41698927310..64dac187adda3c4512742a9620c45d069c6b6866 100644 (file)
@@ -51,6 +51,49 @@ static const uint16_t fixed_huff[288] = {
        0x0038, 0x0838, 0x0438, 0x0c38, 0x0238, 0x0a38, 0x0638, 0x0e38  // 280
 };
 
+/* Length table for lengths 3 to 258, generated by mklen.sh. Only used by
+ * tools/dump_len.c to generate len_fh[] below.
+ * The entries contain :
+ *   code - 257 = 0..28 in bits 0..4
+ *   bits       = 0..5  in bits 5..7
+ *   value      = 0..31 in bits 8..12
+ */
+static const uint16_t len_sym[259] = {
+       0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, //   0
+       0x0005, 0x0006, 0x0007, 0x0028, 0x0128, 0x0029, 0x0129, 0x002a, //   8
+       0x012a, 0x002b, 0x012b, 0x004c, 0x014c, 0x024c, 0x034c, 0x004d, //  16
+       0x014d, 0x024d, 0x034d, 0x004e, 0x014e, 0x024e, 0x034e, 0x004f, //  24
+       0x014f, 0x024f, 0x034f, 0x0070, 0x0170, 0x0270, 0x0370, 0x0470, //  32
+       0x0570, 0x0670, 0x0770, 0x0071, 0x0171, 0x0271, 0x0371, 0x0471, //  40
+       0x0571, 0x0671, 0x0771, 0x0072, 0x0172, 0x0272, 0x0372, 0x0472, //  48
+       0x0572, 0x0672, 0x0772, 0x0073, 0x0173, 0x0273, 0x0373, 0x0473, //  56
+       0x0573, 0x0673, 0x0773, 0x0094, 0x0194, 0x0294, 0x0394, 0x0494, //  64
+       0x0594, 0x0694, 0x0794, 0x0894, 0x0994, 0x0a94, 0x0b94, 0x0c94, //  72
+       0x0d94, 0x0e94, 0x0f94, 0x0095, 0x0195, 0x0295, 0x0395, 0x0495, //  80
+       0x0595, 0x0695, 0x0795, 0x0895, 0x0995, 0x0a95, 0x0b95, 0x0c95, //  88
+       0x0d95, 0x0e95, 0x0f95, 0x0096, 0x0196, 0x0296, 0x0396, 0x0496, //  96
+       0x0596, 0x0696, 0x0796, 0x0896, 0x0996, 0x0a96, 0x0b96, 0x0c96, // 104
+       0x0d96, 0x0e96, 0x0f96, 0x0097, 0x0197, 0x0297, 0x0397, 0x0497, // 112
+       0x0597, 0x0697, 0x0797, 0x0897, 0x0997, 0x0a97, 0x0b97, 0x0c97, // 120
+       0x0d97, 0x0e97, 0x0f97, 0x00b8, 0x01b8, 0x02b8, 0x03b8, 0x04b8, // 128
+       0x05b8, 0x06b8, 0x07b8, 0x08b8, 0x09b8, 0x0ab8, 0x0bb8, 0x0cb8, // 136
+       0x0db8, 0x0eb8, 0x0fb8, 0x10b8, 0x11b8, 0x12b8, 0x13b8, 0x14b8, // 144
+       0x15b8, 0x16b8, 0x17b8, 0x18b8, 0x19b8, 0x1ab8, 0x1bb8, 0x1cb8, // 152
+       0x1db8, 0x1eb8, 0x1fb8, 0x00b9, 0x01b9, 0x02b9, 0x03b9, 0x04b9, // 160
+       0x05b9, 0x06b9, 0x07b9, 0x08b9, 0x09b9, 0x0ab9, 0x0bb9, 0x0cb9, // 168
+       0x0db9, 0x0eb9, 0x0fb9, 0x10b9, 0x11b9, 0x12b9, 0x13b9, 0x14b9, // 176
+       0x15b9, 0x16b9, 0x17b9, 0x18b9, 0x19b9, 0x1ab9, 0x1bb9, 0x1cb9, // 184
+       0x1db9, 0x1eb9, 0x1fb9, 0x00ba, 0x01ba, 0x02ba, 0x03ba, 0x04ba, // 192
+       0x05ba, 0x06ba, 0x07ba, 0x08ba, 0x09ba, 0x0aba, 0x0bba, 0x0cba, // 200
+       0x0dba, 0x0eba, 0x0fba, 0x10ba, 0x11ba, 0x12ba, 0x13ba, 0x14ba, // 208
+       0x15ba, 0x16ba, 0x17ba, 0x18ba, 0x19ba, 0x1aba, 0x1bba, 0x1cba, // 216
+       0x1dba, 0x1eba, 0x1fba, 0x00bb, 0x01bb, 0x02bb, 0x03bb, 0x04bb, // 224
+       0x05bb, 0x06bb, 0x07bb, 0x08bb, 0x09bb, 0x0abb, 0x0bbb, 0x0cbb, // 232
+       0x0dbb, 0x0ebb, 0x0fbb, 0x10bb, 0x11bb, 0x12bb, 0x13bb, 0x14bb, // 240
+       0x15bb, 0x16bb, 0x17bb, 0x18bb, 0x19bb, 0x1abb, 0x1bbb, 0x1cbb, // 248
+       0x1dbb, 0x1ebb, 0x001c                                          // 256
+};
+
 /* length from 3 to 258 converted to bit strings for use with fixed huffman
  * coding. It was built by tools/dump_len.c. The format is the following :
  *   - bits 0..15  = code
@@ -124,6 +167,37 @@ static const uint32_t len_fh[259] = {
        0x0d1d23,  0x0d1e23,  0x0800a3               /* 256-258 */
 };
 
+/* Order in which a dynamic block header sends the code lengths of the code
+ * length alphabet (RFC1951 3.2.7). Used by the decoder and tools/mktext.c.
+ */
+static const unsigned char codelen_order[19] = {
+       16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+};
+
+/* Base values and number of extra bits of the length codes 257..285 and of
+ * the distance codes 0..29 (RFC1951 3.2.5). Only used by tools/mktext.c.
+ */
+static const int len_base[29] = {
+       3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+       35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
+};
+
+static const int len_extra[29] = {
+       0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
+       3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
+};
+
+static const int dist_base[30] = {
+       1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+       257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193,
+       12289, 16385, 24577
+};
+
+static const int dist_extra[30] = {
+       0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
+       7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
+};
+
 /* Huffman tree for text contents, generated by tools/mktext.c from
  * tools/text-tree.txt. The dynamic block header that declares it is
  * text_hdr[], made of TEXT_HDR_BITS bits, excluding BFINAL and BTYPE.
index 7b853705b6c0158900cb399beb587604f9425d41..eb9dafb7b4afcdde11ac7e802f785bdf8b0c5d0b 100644 (file)
--- a/src/slz.c
+++ b/src/slz.c
@@ -110,10 +110,20 @@ static uint32_t text_dist_table[32768];
 # define SLZ_LITERAL_SKIP 1
 #endif
 
-/* Log2 of the size of the hash table used for the references table. */
+/* Log2 of the size of the hash table used for the references table, for levels
+ * 1 to 3. Level 4 may use a larger table to find more matches (14 bits: -0.33
+ * point, 15 bits: -0.46 point, for 4.4% extra CPU each). The table is already
+ * 64 kB which need to be initialized at each call so better keep L4 at the
+ * default size, but it's easy to change it with a -DSLZ_HASH_BITS_L4 just for
+ * this compression level.
+ */
 #ifndef SLZ_HASH_BITS
 # define SLZ_HASH_BITS 13
 #endif
+#ifndef SLZ_HASH_BITS_L4
+# define SLZ_HASH_BITS_L4 SLZ_HASH_BITS
+#endif
+#define SLZ_HASH_BITS_MAX (SLZ_HASH_BITS > SLZ_HASH_BITS_L4 ? SLZ_HASH_BITS : SLZ_HASH_BITS_L4)
 
 /* First, RFC1951-specific declarations and extracts from the RFC.
  *
@@ -275,24 +285,18 @@ uint32_t slz_hash_bits(uint32_t a, const int bits)
        __asm__ volatile("crc32l %1,%0" : "+r"(a) : "r"(0));
 #elif defined(HAVE_FAST_MULT)
        // optimal factor for SLZ_HASH_BITS=12 and SLZ_HASH_BITS=13 among 48k tested: 0x1af42f
-       a = (a * 0x1af42f); // 12: 44.12, 13: 43.35
-       //return (a * 0x1aaee3) >> (32 - SLZ_HASH_BITS); // 12: 44.14, 13: 43.34
-       //return (a * 0x1aae0b) >> (32 - SLZ_HASH_BITS); // 12: 44.15, 13: 43.35, -30k
-       //return (a * 0x17e7bd) >> (32 - SLZ_HASH_BITS); // 12: 44.15, 13: 43.36
-       //return (a * 0xaae7b) >> (32 - SLZ_HASH_BITS); // 12: 44.22, 13:43.35, -1k
-       //return (a * 0xaaec3) >> (32 - SLZ_HASH_BITS); // 12: 44.20, 13: 43.35, -10k // 275659647
+       if (bits >= 15)
+               a *= 0x60441;  // 14: 40.13, 15: 39.96
+       else if (bits == 14)
+               a *= 0x44041;  // 14: 40.11, 15: 40.01
+       else
+               a *= 0x1af42f; // 12: 41.19, 13: 40.47, 14: 40.14, 15: 40.01
 #else
        a = ((a << 19) + (a << 6) - a);
 #endif
        return a >> (32 - bits);
 }
 
-/* the hash of the main encoder's reference table, on SLZ_HASH_BITS bits */
-static inline uint32_t slz_hash(uint32_t a)
-{
-       return slz_hash_bits(a, SLZ_HASH_BITS);
-}
-
 /* This function compares buffers <a> and <b> and reads 32 or 64 bits at a time
  * during the approach. It makes us of unaligned little endian memory accesses
  * on capable architectures. <max> is the maximum number of bytes that can be
@@ -937,11 +941,11 @@ static void reset_refs(union ref *refs, long count)
        memset(refs, 0xFE, count);
 }
 
-/* Inserts position <p> of input buffer <in> into the reference table <refs>.
- * Reads the 4 bytes at in[p], so the caller must guarantee that in[p + 3] is
- * within the input buffer.
+/* Inserts position <p> of input buffer <in> into the reference table <refs>
+ * of <hbits> bits. Reads the 4 bytes at in[p], so the caller must guarantee
+ * that in[p + 3] is within the input buffer.
  */
-static inline void insert_ref(union ref *refs, const unsigned char *in, unsigned long p)
+static inline void insert_ref(union ref *refs, const unsigned char *in, unsigned long p, const int hbits)
 {
 #ifdef UNALIGNED_LE_OK
        uint32_t w = *(uint32_t *)&in[p];
@@ -949,7 +953,7 @@ static inline void insert_ref(union ref *refs, const unsigned char *in, unsigned
        uint32_t w = ((uint32_t)in[p]) | ((uint32_t)in[p + 1] << 8) |
                     ((uint32_t)in[p + 2] << 16) | ((uint32_t)in[p + 3] << 24);
 #endif
-       uint32_t h = slz_hash(w);
+       uint32_t h = slz_hash_bits(w, hbits);
 
        if (sizeof(long) >= 8)
                refs[h].by64 = ((uint64_t)p) + ((uint64_t)w << 32);
@@ -1254,7 +1258,7 @@ long backward_match(const unsigned char *in, unsigned long pos, const unsigned c
  * All three must be constants. Each tree has its own part below.
  */
 static inline __attribute__((always_inline))
-long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more, int level, int bmatch, int auto_tree)
+long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more, int level, int bmatch, int auto_tree, const int hbits)
 {
        long rem = ilen;
        unsigned long pos = 0;
@@ -1267,7 +1271,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
 
        uint32_t plit = 0;
        uint32_t dist, code;
-       union ref refs[1 << SLZ_HASH_BITS];
+       union ref refs[1 << SLZ_HASH_BITS_MAX]; /* only 1 << hbits are used */
 
        if (__builtin_expect(!strm->level, 0)) {
                /* force to send as literals (eg to preserve CPU) */
@@ -1277,7 +1281,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
                goto leave;
        }
 
-       reset_refs(refs, sizeof(refs));
+       reset_refs(refs, sizeof(refs[0]) << hbits);
        strm->outbuf = out;
 
        if (auto_tree) {
@@ -1327,7 +1331,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
 #else
                        word = *(uint32_t *)&in[pos];
 #endif
-                       h = slz_hash(word);
+                       h = slz_hash_bits(word, hbits);
                        asm volatile ("" ::); // prevent gcc from trying to be smart with the prefetch
 
                        if (sizeof(long) >= 8) {
@@ -1396,7 +1400,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
                                                skip &= 63;
                                                do {
                                                        uint32_t w = read_le32(in + pos + 60);
-                                                       uint32_t hw = slz_hash(w);
+                                                       uint32_t hw = slz_hash_bits(w, hbits);
 
                                                        if (sizeof(long) >= 8) {
                                                                uint64_t old = refs[hw].by64;
@@ -1457,7 +1461,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
                                                skip &= 63;
                                                do {
                                                        uint32_t w = read_le32(in + pos + 60);
-                                                       uint32_t hw = slz_hash(w);
+                                                       uint32_t hw = slz_hash_bits(w, hbits);
 
                                                        if (sizeof(long) >= 8) {
                                                                uint64_t old = refs[hw].by64;
@@ -1743,9 +1747,9 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
                         * that a similar sequence will be a hit next time. We only
                         * need 4 remaining bytes starting from the current one.
                         */
-                       insert_ref(refs, in, pos - mlen + 3);
-                       insert_ref(refs, in, pos - mlen + 2);
-                       insert_ref(refs, in, pos - mlen + 1);
+                       insert_ref(refs, in, pos - mlen + 3, hbits);
+                       insert_ref(refs, in, pos - mlen + 2, hbits);
+                       insert_ref(refs, in, pos - mlen + 1, hbits);
 #endif // SLZ_INSERT_SKIPPED
 
 #ifndef UNALIGNED_FASTER
@@ -1825,7 +1829,7 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
 #else
                        word = *(uint32_t *)&in[pos];
 #endif
-                       h = slz_hash(word);
+                       h = slz_hash_bits(word, hbits);
                        asm volatile ("" ::); // prevent gcc from trying to be smart with the prefetch
 
                        if (sizeof(long) >= 8) {
@@ -2049,9 +2053,9 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
 #if SLZ_INSERT_SKIPPED
                        if (rem < 4)
                                break;
-                       insert_ref(refs, in, pos - mlen + 3);
-                       insert_ref(refs, in, pos - mlen + 2);
-                       insert_ref(refs, in, pos - mlen + 1);
+                       insert_ref(refs, in, pos - mlen + 3, hbits);
+                       insert_ref(refs, in, pos - mlen + 2, hbits);
+                       insert_ref(refs, in, pos - mlen + 1, hbits);
 #endif // SLZ_INSERT_SKIPPED
 
 #ifndef UNALIGNED_FASTER
@@ -2164,36 +2168,39 @@ long _slz_rfc1951_encode(struct slz_stream *__restrict strm, unsigned char *out,
  * increase they cause, and tend to result in higher compression speed due to
  * the savings on encoding. At compression level 4, 8 bytes save 0.03% over 2
  * on silesia for 0.5% of speed.
+ * Its <hbits> argument is the size of the reference table in bits, which
+ * must be a constant (SLZ_HASH_BITS or SLZ_HASH_BITS_L4).
  * The compression levels are made of:
  *   level 1: encoder level 1, 1-byte backward match, fixed tree only;
  *   level 2: encoder level 1, 1-byte backward match, automatic tree (see
  *            SLZ_AUTO_IN);
  *   level 3: encoder level 2, 1-byte backward match, automatic tree;
- *   level 4: encoder level 3, 8-byte backward match, automatic tree.
+ *   level 4: encoder level 3, 8-byte backward match, automatic tree, table
+ *            of SLZ_HASH_BITS_L4 bits.
  * The text tree's part is the same at all levels (2-byte backward match).
  */
 static __attribute__((noinline))
 long _slz_rfc1951_encode_lev1(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more)
 {
-       return _slz_rfc1951_encode(strm, out, in, ilen, more, 1, 1, 0);
+       return _slz_rfc1951_encode(strm, out, in, ilen, more, 1, 1, 0, SLZ_HASH_BITS);
 }
 
 static __attribute__((noinline))
 long _slz_rfc1951_encode_lev2(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more)
 {
-       return _slz_rfc1951_encode(strm, out, in, ilen, more, 1, 1, 1);
+       return _slz_rfc1951_encode(strm, out, in, ilen, more, 1, 1, 1, SLZ_HASH_BITS);
 }
 
 static __attribute__((noinline))
 long _slz_rfc1951_encode_lev3(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more)
 {
-       return _slz_rfc1951_encode(strm, out, in, ilen, more, 2, 1, 1);
+       return _slz_rfc1951_encode(strm, out, in, ilen, more, 2, 1, 1, SLZ_HASH_BITS);
 }
 
 static __attribute__((noinline))
 long _slz_rfc1951_encode_lev4(struct slz_stream *__restrict strm, unsigned char *out, const unsigned char *in, long ilen, int more)
 {
-       return _slz_rfc1951_encode(strm, out, in, ilen, more, 3, 8, 1);
+       return _slz_rfc1951_encode(strm, out, in, ilen, more, 3, 8, 1, SLZ_HASH_BITS_L4);
 }
 
 /* Event encoder, used with SLZ_CT_EVENT: streams made of small events flushed
index 19ab7e39acd9784a1797c580e7821d7544499e78..3632e2f5395a5f9ec9898c0b65550879166040dd 100644 (file)
@@ -209,12 +209,20 @@ static int gen_huffman_table(unsigned int symbols,
        return 1;
 }
 
+/* Fast table entry for codes too long for the table or unused codes: a zero
+ * length, so that the readers fall back to the tree walk, and symbol 511, above
+ * all literals, so that the literal loop of dynamic blocks leaves on its
+ * literal test alone.
+ */
+#define USLZ_FAST_LONG (511U << 7)
+
 /*
  * gen_fast_table: fill the direct lookup <table> of <tbits> bits from the code
  * lengths in <lengths>, for an alphabet of <symbols> symbols. Entries have the
  * same layout as fixed_huff_dec_table[]: the symbol in the upper bits and the
- * code length in the low 4 bits. Codes longer than <tbits> are left at zero
- * and are decoded by walking the tree built by gen_huffman_table() instead.
+ * code length in the low 4 bits. Codes longer than <tbits> are left at
+ * USLZ_FAST_LONG and are decoded by walking the tree built by
+ * gen_huffman_table() instead.
  *
  * Each code is replicated over every combination of the bits above it, so
  * that a lookup performed with fewer than <tbits> bits still finds it,
@@ -232,7 +240,9 @@ static void gen_fast_table(unsigned int symbols, const unsigned char *lengths,
        unsigned short count[16];
        unsigned int i, len, code;
 
-       memset(table, 0, (1U << tbits) * sizeof(*table));
+       /* entries left unassigned carry USLZ_FAST_LONG */
+       for (i = 0; i < (1U << tbits); i++)
+               table[i] = USLZ_FAST_LONG;
        memset(count, 0, sizeof(count));
 
        for (i = 0; i < symbols; i++)
@@ -260,8 +270,10 @@ static void gen_fast_table(unsigned int symbols, const unsigned char *lengths,
                if (len > tbits)
                        continue;
 
-               /* codes appear bit-reversed in the accumulator */
-               rev = rbit9(code, len);
+               /* codes appear bit-reversed in the accumulator, and may be
+                * up to 15 bits long when the table is that wide.
+                */
+               rev = rbit16(code, len);
                step = 1U << len;
                for (; rev < (1U << tbits); rev += step)
                        table[rev] = (i << 7) | len;
@@ -553,6 +565,53 @@ static inline int gethuff_fast(const unsigned char **in_ptr, const unsigned char
                }                                                              \
        } while (0)
 
+/* Number of literals of dynamic blocks decoded per refill. The loop is only
+ * built on 64-bit platforms, where a refill brings at least 56 bits. A dynamic
+ * tree may have codes of up to 15 bits, but gen_fast_table() only places in
+ * fast_lit[] those of at most USLZ_FAST_BITS bits, and the longer ones consume
+ * nothing (see USLZ_FAST_LONG), so whatever the tree, a lookup consumes at
+ * most USLZ_FAST_BITS bits: 5 lookups per refill with 10-bit tables. It is
+ * capped to the 7 DYN_LITERAL() calls of the loop.
+ */
+#if USLZ_FAST_BITS
+#define DYN_LIT_PER_REFILL (56 / USLZ_FAST_BITS > 7 ? 7 : 56 / USLZ_FAST_BITS)
+#else
+#define DYN_LIT_PER_REFILL 0 /* no fast table, the loop is never entered */
+#endif
+
+/* same as FIXED_LITERAL() above for dynamic blocks, from fast_lit[]. A code
+ * too long for the table reads USLZ_FAST_LONG, whose zero length consumes
+ * nothing, and goes to the generic path at <dyn_slow> to be walked in the
+ * tree.
+ *
+ * As in FIXED_LITERAL(), <num_bits> is decremented by the whole entry rather
+ * than by its length (bits 0-3) to save a mask per literal: <num_bits> being
+ * 8-bit, the symbol part (sym << 7) only subtracts 0 or 128, which can only
+ * flip bit 7 since <num_bits> never exceeds 63. Bit 7 is cleared by the
+ * "num_bits &= 0x3F" after the last literal of the refill, or on exit.
+ *
+ * Any other symbol than a literal is consumed and handled at
+ * <dyn_match>.
+ */
+#define DYN_LITERAL(ofs)                                                       \
+       do {                                                                   \
+               if (ofs < DYN_LIT_PER_REFILL) {                                \
+                       unsigned int entry, sym;                               \
+                       entry = state->fast_lit[bit_accum & ((1U << USLZ_FAST_BITS) - 1)]; \
+                       bit_accum >>= entry & 0xF;                             \
+                       num_bits   -= entry;                                   \
+                       sym = entry >> 7;                                      \
+                       if (sym >= 256) {                                      \
+                               num_bits &= 0x3F;                              \
+                               if (!(entry & 0xF))                            \
+                                       goto dyn_slow;                         \
+                               state->symbol = sym;                           \
+                               goto dyn_match;                                \
+                       }                                                      \
+                       *out++ = sym;                                          \
+               }                                                              \
+       } while (0)
+
 /* Drops the bits left in the byte being read so that the accumulator holds
  * whole bytes only, and clears whatever REFILL() left above them. <num_bits>
  * must be below the width of the accumulator here, which REFILL() guarantees.
@@ -860,13 +919,6 @@ static enum uslz_decode_ret uslz_decode_block(struct uslz_stream *state)
        }  /* if (state->block_type == 0) */
 
        if (state->block_type == 2) {  /* Dynamic tables. */
-               /* codelen_order: Order of code lengths in the block header for the
-                * code length alphabet.
-                */
-               static const unsigned char codelen_order[19] = {
-                       16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
-               };
-
                /* Retrieve the three code counts from the block header. */
                state->state = USLZ_ST_LITERAL_COUNT;
 
@@ -1062,14 +1114,184 @@ static enum uslz_decode_ret uslz_decode_block(struct uslz_stream *state)
                         */
                        int ret = -1;
 
-                       if (USLZ_FAST_BITS && !state->huff_index)
+                       if (USLZ_FAST_BITS && !state->huff_index) {
+#if defined(UNALIGNED_LE_OK) && __SIZEOF_LONG__ >= 8
+                               /* Fast loop, the same as for fixed blocks
+                                * above, from the block's own fast_lit[] table:
+                                * up to 5 literals per refill, and the matches
+                                * at <dyn_match>, after which the loop goes on
+                                * with the next symbol. A code too long for the
+                                * table is walked in the tree at <dyn_slow>,
+                                * and what the loop cannot handle is left to
+                                * the generic path. Only built on 64-bit
+                                * platforms, see DYN_LIT_PER_REFILL and
+                                * <dyn_match>.
+                                */
+                               while (out_lim - out >= DYN_LIT_PER_REFILL &&
+                                      in_top - in_ptr >= __SIZEOF_LONG__) {
+                                       unsigned int dist_entry, dist_sym, dist;
+
+                                       REFILL();
+                                       DYN_LITERAL(0); DYN_LITERAL(1);
+                                       DYN_LITERAL(2); DYN_LITERAL(3);
+                                       DYN_LITERAL(4); DYN_LITERAL(5);
+                                       DYN_LITERAL(6);
+                                       num_bits &= 0x3F;
+                                       continue;
+
+ dyn_match:
+                                       /* The symbol is not a literal: it is a
+                                        * match length or the end of block. The
+                                        * match is decoded and copied here, then
+                                        * the loop goes on with the next symbol.
+                                        * The end of block, and any case the code
+                                        * below doesn't handle, is left to the
+                                        * generic path at the step reached.
+                                        *
+                                        * 258 bytes of output room are needed
+                                        * (the longest match), so that the copy
+                                        * never has to stop at <out_lim>. A
+                                        * refill's worth of input guarantees
+                                        * that after REFILL() the accumulator
+                                        * holds at least 56 bits, the loop
+                                        * being only built on 64-bit platforms:
+                                        * the length's extra bits (5 at most),
+                                        * the distance code if it is found in
+                                        * fast_dist[] (USLZ_FAST_DBITS, 15 at
+                                        * most) and the distance's extra bits
+                                        * (13 at most) take 33 at most.
+                                        */
+                                       if (state->symbol == 256 || out_lim - out < 258 ||
+                                           in_top - in_ptr < __SIZEOF_LONG__)
+                                               goto have_symbol;
+                                       REFILL();
+
+                                       /* Length codes (RFC1951 3.2.5): 257 to
+                                        * 264 are lengths 3 to 10 without extra
+                                        * bits. 265 to 284 go by groups of 4
+                                        * sharing the same number of extra bits,
+                                        * 1 for 265-268 up to 5 for 281-284, that
+                                        * is (sym - 261) / 4. Within a group, the
+                                        * base length of each code is 4, 5, 6 or 7
+                                        * (4 plus its position in the group,
+                                        * (sym - 265) & 3) shifted left by this
+                                        * number, plus 3: 265 is 11, 266 is 13,
+                                        * ..., 284 is 227.
+                                        * The extra bits, taken from the bottom
+                                        * of the accumulator, are added to the
+                                        * base. 285 is 258 without extra bits,
+                                        * and 286-287 are invalid.
+                                        */
+                                       if (state->symbol <= 264)
+                                               len = state->symbol - 254;
+                                       else if (state->symbol <= 284) {
+                                               get_bits = (state->symbol - 261) / 4;
+                                               len = bit_accum & ((1UL << get_bits) - 1);
+                                               bit_accum >>= get_bits;
+                                               num_bits -= get_bits;
+                                               len += 3 + ((4 + ((state->symbol - 265) & 3)) << get_bits);
+                                       }
+                                       else if (state->symbol == 285)
+                                               len = 258;
+                                       else
+                                               goto have_symbol;
+
+                                       /* The distance code, from fast_dist[]
+                                        * whose entries are built like those of
+                                        * fast_lit[]: the symbol above bit 7 and
+                                        * the code's length in bits 0-3, zero for
+                                        * a code too long for the table, which is
+                                        * left to the generic path at the
+                                        * distance step (the length is known).
+                                        */
+                                       dist_entry = state->fast_dist[bit_accum & ((1U << USLZ_FAST_DBITS) - 1)];
+                                       if (!USLZ_FAST_DBITS || !(dist_entry & 0xF)) {
+                                               state->repeat_length = len;
+                                               state->state = USLZ_ST_READ_DISTANCE;
+                                               goto state_READ_DISTANCE;
+                                       }
+                                       bit_accum >>= dist_entry & 0xF;
+                                       num_bits -= dist_entry & 0xF;
+                                       dist_sym = dist_entry >> 7;
+
+                                       /* Distance codes (RFC1951 3.2.5): 0 to 3
+                                        * are distances 1 to 4 without extra
+                                        * bits. 4 to 29 go by pairs sharing the
+                                        * same number of extra bits, 1 for 4-5 up
+                                        * to 13 for 28-29, that is (sym - 2) / 2.
+                                        * Within a pair, the base distance is 2
+                                        * for the even code and 3 for the odd
+                                        * one (2 + (sym & 1)), shifted left by
+                                        * this number, plus 1: 4 is 5, 5 is 7,
+                                        * ..., 29 is 24577.
+                                        * The extra bits are added to the base.
+                                        * 30 and 31 are invalid.
+                                        */
+                                       if (dist_sym <= 3)
+                                               dist = dist_sym + 1;
+                                       else if (dist_sym <= 29) {
+                                               get_bits = (dist_sym - 2) / 2;
+                                               dist = bit_accum & ((1UL << get_bits) - 1);
+                                               bit_accum >>= get_bits;
+                                               num_bits -= get_bits;
+                                               dist += 1 + ((2 + (dist_sym & 1)) << get_bits);
+                                       }
+                                       else {
+                                               err_code = USLZ_DECODE_E_INVALID_SYMBOL;
+                                               goto error_return;
+                                       }
+
+                                       /* A distance larger than what was written
+                                        * since <out_base> starts the match in the
+                                        * upper part of the ring (history before
+                                        * the last wrap), or is invalid: the
+                                        * generic copy handles both.
+                                        */
+                                       if (dist > (unsigned long)(out - out_base)) {
+                                               state->repeat_length = len;
+                                               state->distance = dist;
+                                               goto state_READ_DISTANCE_RET;
+                                       }
+
+                                       /* Same copy as the generic path: when the
+                                        * source and the destination don't
+                                        * overlap, memcpy(), otherwise the bytes
+                                        * being written are part of the source
+                                        * and are replicated one at a time.
+                                        */
+                                       if (dist >= len)
+                                               memcpy(out, out - dist, len);
+                                       else {
+                                               const unsigned char *src = out - dist;
+                                               unsigned char *dst = out;
+                                               unsigned int cnt = len;
+
+                                               do {
+                                                       *dst++ = *src++;
+                                                       __asm__("" : "+r"(dst)); // same as the generic copy
+                                               } while (--cnt);
+                                       }
+                                       out += len;
+                               }
+                               /* same as for fixed blocks above */
+                               if (out == out_lim)
+                                       continue;
+#endif
                                ret = gethuff_fast(&in_ptr, in_top, &num_bits, &bit_accum,
                                                   &state->symbol, state->fast_lit,
                                                   USLZ_FAST_BITS);
+                       }
                        if (!ret)
                                goto out_of_data;
-                       if (ret < 0)
+                       if (ret < 0) {
+#if defined(UNALIGNED_LE_OK) && __SIZEOF_LONG__ >= 8
+                               /* note that dyn_slow can jump here from above
+                                * because it leaves ret to -1.
+                                */
+                       dyn_slow:
+#endif
                                GETHUFF(state->symbol, state->literal_table);
+                       }
                }
 
 #if defined(UNALIGNED_LE_OK)