[PATCH v3 18/18] lib/lz4: leave out lz4hc pattern analysis
From: Michal Wilczynski
Date: Wed Dec 02 2026 - 12:35:24 EST
Pattern analysis lets lz4hc levels 9 and up jump over long runs of a
single byte. The fork never had it. In v1.11.1 its code sits in the
match search every level runs. It causes most of lz4hc's slowdown at
level 9, zram's default: with a dictionary, v1.11.1 is a third slower
than the fork there on the memory of real programs.
zram stores pages that hold a single repeated value without
compressing them, so pattern analysis seldom pays off there. Set
LZ4HC_PATTERN_ANALYSIS to 0 in lz4_deps.h.
With this commit and the two before it, zram's write speed on the
anonymous memory of four programs (headless Firefox and Chrome, cc1
compiling kernel/sched/core.c, Python running scripts/kernel-doc), in
a KVM guest on one core, median of 5 boots of 20 passes each, against
today's kernel, before and after these three commits:
lz4hc:3 lz4hc:6 lz4hc:9 9, 16K dict
Firefox -6.0 -1.3 -4.2 +2.3 -1.0 +10.9 -32.2 +10.4
Chrome -6.1 -0.7 -3.1 +3.0 +2.5 +10.8 -31.4 +8.2
gcc (cc1) -6.1 -1.1 -2.5 +4.1 -2.5 +14.4 -33.8 +12.5
Python -5.0 -0.5 -2.2 +4.5 -5.8 +12.1 -40.4 +9.0
in percent. Compressed size stays within 0.05% of today's.
The other lz4hc users are f2fs and the crypto API's lz4hc. They lose
the speedup on long runs of one byte too: at levels 9 to 12 such input
compresses up to 22 times slower and up to 7% larger in upstream's own
benchmark, and level 12 on source text is 8-18% slower. Levels 9 and
10 on binaries and text get 6-29% faster. On other input, output at
levels 9 to 12 grows slightly: by under 1% in most cases tested, up to
1.8% at level 10 on a 4K block with a dictionary. Levels below 9
compress the same.
Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
lib/lz4/lz4_deps.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/lib/lz4/lz4_deps.h b/lib/lz4/lz4_deps.h
index 2c6f5ecdccc6566e4433962149988e2b91fa9e48..baa9a75fd22e8f4f2d44a2e916f77b62ad952532 100644
--- a/lib/lz4/lz4_deps.h
+++ b/lib/lz4/lz4_deps.h
@@ -45,6 +45,12 @@
#define LZ4_HEAPMODE 1
#define LZ4HC_HEAPMODE 1
+/* Pattern analysis only helps lz4hc levels 9 and up on long runs of one
+ * byte, and its code slows the match search of every level. zram stores
+ * pages of a single value without compressing them, so leave it out.
+ */
+#define LZ4HC_PATTERN_ANALYSIS 0
+
/* Failing stubs; nothing in the kernel reaches these. */
static void *LZ4_malloc(size_t s) { return NULL; }
static void *LZ4_calloc(size_t n, size_t s) { return NULL; }
--
2.34.1