[PATCH v3 17/18] lib/lz4: take lz4 PR #2217's LZ4HC_PATTERN_ANALYSIS build macro
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:35:11 EST
Pattern analysis lets lz4hc levels 9 and up jump over long runs of a
single byte instead of following the hash chain through them. It only
runs at those levels, but its code sits in the match search every
level runs.
Take the lz4hc.c change of upstream pull request #2217, "lz4hc: add
LZ4HC_PATTERN_ANALYSIS build macro", which lets a build leave it out.
The macro defaults to 1, which builds the same code as before, so this
patch on its own changes nothing.
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.
Link: https://github.com/lz4/lz4/pull/2217
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
lib/lz4/lz4_deps.h | 8 ++++----
lib/lz4/upstream/lz4hc.c | 17 ++++++++++++++++-
2 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/lib/lz4/lz4_deps.h b/lib/lz4/lz4_deps.h
index 65ed729be6158fba6a6974f35cb185e7a0a7adf9..2c6f5ecdccc6566e4433962149988e2b91fa9e48 100644
--- a/lib/lz4/lz4_deps.h
+++ b/lib/lz4/lz4_deps.h
@@ -5,10 +5,10 @@
*
* Build environment for the vendored upstream LZ4 sources.
*
- * 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.
+ * upstream/ is LZ4 v1.11.1, plus two lz4hc changes from upstream pull
+ * requests #2216 and #2217, 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 9f8985a7b166917c629b1e5673e9a409d6fb71ac..e8557ead3eeec92ccbb66d47b7306db7891b7abd 100644
--- a/lib/lz4/upstream/lz4hc.c
+++ b/lib/lz4/upstream/lz4hc.c
@@ -54,6 +54,21 @@
# endif
#endif
+/*! LZ4HC_PATTERN_ANALYSIS :
+ * Select whether levels 9+ look for runs of a repeated byte, to jump over
+ * them instead of following the chain through them (1:default).
+ * It speeds up inputs with long runs a lot. Its code sits in the match
+ * search of levels 3 and up though : at 0, it isn't compiled, the lower
+ * levels compress up to 8% faster and levels 9-10 6-29% faster on
+ * binaries and text (gcc 15 and clang 21, x86-64), while inputs with long
+ * runs compress up to 22 times slower at levels 9-12 and up to 7% larger,
+ * and level 12 compresses text up to 18% slower.
+ * Levels 1-8 produce the same output either way.
+**/
+#ifndef LZ4HC_PATTERN_ANALYSIS
+# define LZ4HC_PATTERN_ANALYSIS 1
+#endif
+
/*=== Dependency ===*/
#define LZ4_HC_STATIC_LINKING_ONLY
@@ -1146,7 +1161,7 @@ LZ4HC_InsertAndGetWiderMatch (
} }
{ U32 const distNextMatch = DELTANEXTU16(chainTable, matchIndex);
- if (patternAnalysis && distNextMatch==1 && matchChainPos==0) {
+ if (LZ4HC_PATTERN_ANALYSIS && patternAnalysis && distNextMatch==1 && matchChainPos==0) {
U32 const matchCandidateIdx = matchIndex-1;
size_t forwardPatternLength, backLength;
/* may be a repeated pattern */
--
2.34.1