[PATCH v3 16/18] lib/lz4: take lz4 PR #2216's lz4hc hash chain search speedup
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:35:23 EST
lz4hc levels 3 to 9 spend most of their time in the hash chain loop of
LZ4HC_InsertAndGetWiderMatch(). In v1.11.1 that loop reads more values
than x86_64 has general-purpose registers, and tests for an external
dictionary on every candidate, even when none is loaded. On the 4K
pages zram compresses, v1.11.1's lz4hc is slower than the fork it
replaces.
Take the lz4hc.c change of upstream pull request #2216, "lz4hc: speed
up the hash chain search without a dictionary": input without a
dictionary gets its own copy of the search, without the external
dictionary branch, which checks the maximum match length on pointers
it already holds. Output is unchanged.
The pull request is not merged yet. If upstream changes it, this patch
follows; once a release has it, the next re-sync replaces this patch.
Note the change in lz4_deps.h's description of upstream/.
Link: https://github.com/lz4/lz4/pull/2216
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
lib/lz4/lz4_deps.h | 7 ++++---
lib/lz4/upstream/lz4hc.c | 43 +++++++++++++++++++++++++++++++++++++------
2 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/lib/lz4/lz4_deps.h b/lib/lz4/lz4_deps.h
index dc5112a146ac91f7a06189f7baec8629f83e4b59..65ed729be6158fba6a6974f35cb185e7a0a7adf9 100644
--- a/lib/lz4/lz4_deps.h
+++ b/lib/lz4/lz4_deps.h
@@ -5,9 +5,10 @@
*
* Build environment for the vendored upstream LZ4 sources.
*
- * upstream/ is LZ4 v1.11.1, unmodified. Replace it with a later release
- * rather than patch it. The three entry point files include this header
- * first.
+ * upstream/ is LZ4 v1.11.1, plus the lz4hc change of upstream pull
+ * request #2216, not yet merged. Replace it with a later release rather
+ * than patch it further. The three entry point files include this
+ * header first.
*/
#ifndef __LZ4_DEPS_H__
diff --git a/lib/lz4/upstream/lz4hc.c b/lib/lz4/upstream/lz4hc.c
index b394c3961d35d80a8e89a4b0838ff8eea42adfe6..9f8985a7b166917c629b1e5673e9a409d6fb71ac 100644
--- a/lib/lz4/upstream/lz4hc.c
+++ b/lib/lz4/upstream/lz4hc.c
@@ -75,7 +75,9 @@
/*=== Enums ===*/
-typedef enum { noDictCtx, usingDictCtxHc } dictCtx_directive;
+/* noExtDictHc : no dictCtx, and no external dictionary either (lowLimit == dictLimit) :
+ * every candidate is within the prefix, so the match search leaves out the external dictionary code. */
+typedef enum { noDictCtx, usingDictCtxHc, noExtDictHc } dictCtx_directive;
/*=== Constants ===*/
@@ -1085,15 +1087,19 @@ LZ4HC_InsertAndGetWiderMatch (
ipIndex, matchIndex, lowestMatchIndex);
/* Candidates only replace the current match if strictly longer,
- * so stopping once @longest reaches @maxLength doesn't change the result. */
- while ((matchIndex>=lowestMatchIndex) && (nbAttempts>0) && (longest<maxLength)) {
+ * so stopping once @longest reaches @maxLength doesn't change the result.
+ * Without a dictionary, the same test on pointers keeps one variable less
+ * live in the hash chain search. */
+ while ((matchIndex>=lowestMatchIndex) && (nbAttempts>0)
+ && (dict == noExtDictHc ? iLowLimit + longest < iHighLimit : longest < maxLength)) {
int matchLength=0;
nbAttempts--;
assert(matchIndex < ipIndex);
+ assert(dict != noExtDictHc || matchIndex >= prefixIdx);
if (favorDecSpeed && (ipIndex - matchIndex < 8)) {
/* do nothing:
* favorDecSpeed intentionally skips matches with offset < 8 */
- } else if (matchIndex >= prefixIdx) { /* within current Prefix */
+ } else if (dict == noExtDictHc || matchIndex >= prefixIdx) { /* within current Prefix */
const BYTE* const matchPtr = prefixPtr + (matchIndex - prefixIdx);
assert(matchPtr < ip);
assert(longest >= 1);
@@ -1109,7 +1115,7 @@ LZ4HC_InsertAndGetWiderMatch (
DEBUGLOG(7, "Found match of len=%i within prefix, offset=%i, back=%i", longest, offset, -back);
HEX_CMP(7, ip + back, ip + back - offset, (size_t)matchLength);
} } }
- } else { /* lowestMatchIndex <= matchIndex < dictLimit : within Ext Dict */
+ } else if (dict != noExtDictHc) { /* lowestMatchIndex <= matchIndex < dictLimit : within Ext Dict */
const BYTE* const matchPtr = dictStart + (matchIndex - dictIdx);
assert(matchIndex >= dictIdx);
if ( likely(matchIndex <= prefixIdx - 4)
@@ -1440,6 +1446,26 @@ LZ4_FORCE_INLINE int LZ4HC_compress_hashChain (
}
+/* The hash chain compressor, once without an external dictionary and once for the
+ * other cases, each out of line : inlined into LZ4HC_compress_generic_internal(),
+ * compilers instantiate it per call site and directive (clang : 2 to 3 times), and
+ * its search loop's register allocation then varies with the surrounding code. */
+LZ4_FORCE_NOINLINE int
+LZ4HC_compress_hashChain_noExtDict(LZ4HC_CCtx_internal* const ctx,
+ const char* const src, char* const dst, int* const srcSizePtr, int const maxOutputSize,
+ int const maxNbAttempts, const limitedOutput_directive limit)
+{
+ return LZ4HC_compress_hashChain(ctx, src, dst, srcSizePtr, maxOutputSize, maxNbAttempts, limit, noExtDictHc);
+}
+
+LZ4_FORCE_NOINLINE int
+LZ4HC_compress_hashChain_anyDict(LZ4HC_CCtx_internal* const ctx,
+ const char* const src, char* const dst, int* const srcSizePtr, int const maxOutputSize,
+ int const maxNbAttempts, const limitedOutput_directive limit, const dictCtx_directive dict)
+{
+ return LZ4HC_compress_hashChain(ctx, src, dst, srcSizePtr, maxOutputSize, maxNbAttempts, limit, dict);
+}
+
static int LZ4HC_compress_optimal( LZ4HC_CCtx_internal* ctx,
const char* const source, char* dst,
int* srcSizePtr, int dstCapacity,
@@ -1481,7 +1507,12 @@ LZ4HC_compress_generic_internal (
src, dst, srcSizePtr, dstCapacity,
limit, dict);
} else if (cParam.strat == lz4hc) {
- result = LZ4HC_compress_hashChain(ctx,
+ if (dict == noDictCtx && ctx->lowLimit == ctx->dictLimit)
+ result = LZ4HC_compress_hashChain_noExtDict(ctx,
+ src, dst, srcSizePtr, dstCapacity,
+ cParam.nbSearches, limit);
+ else
+ result = LZ4HC_compress_hashChain_anyDict(ctx,
src, dst, srcSizePtr, dstCapacity,
cParam.nbSearches, limit, dict);
} else {
--
2.34.1