Repository navigation
Commit 2ab1b02
authored
## What's Changed
`Lz4CompressionCodec` uses commons-compress's pure-Java LZ4, which is
slow enough that compressing an 8 MiB buffer of sequential `int64`
values takes several minutes (#192). This changes the codec to use
[lz4-java](https://github.com/yawkat/lz4-java), the JNI bindings to
liblz4 that Spark, Kafka and others use, while keeping every frame it
could read before readable.
**Compression.** Each buffer is written as one LZ4 frame of independent
blocks of up to 4 MiB, without checksums, compressed with lz4-java
directly from the buffer's memory. Every LZ4 frame decoder reads frames
with independent blocks: liblz4 (Arrow C++, pyarrow), `lz4_flex`
(arrow-rs), lz4-java, and commons-compress.
**Decompression.** A frame with independent blocks is decompressed with
lz4-java directly into the output buffer. That covers frames from this
codec, from arrow-rs, and from the previous version of this codec. The
new decoder accepts any block size, block and content checksums (which
it verifies), and a content size. It checks the frame's block lengths,
and that they can hold the recorded decompressed length, before
allocating for it.
A frame whose blocks are linked, or that uses a dictionary, still goes
through commons-compress. lz4-java cannot decode a block that refers to
earlier blocks, and Arrow C++ writes linked blocks: liblz4's default
`LZ4F_preferences_t` is `LZ4F_blockLinked`, so pyarrow 26 writes every
buffer larger than 64 KiB as linked 64 KiB blocks. The LZ4 files in
arrow-testing are small enough that liblz4 writes them as single,
independent blocks, so the integration tests would not catch this.
`TestLz4CompressionCodec` covers linked frames written by
commons-compress instead.
**Dependency.** This adds `at.yawk.lz4:lz4-java` 1.12.1, the maintained
fork of `org.lz4:lz4-java`. The original artifact's last code release,
1.8.0, has several published advisories (for example CVE-2025-12183 and
CVE-2025-66566), and its 1.8.1 is a relocation POM that points to the
fork. Spark moved to the fork in 4.2. Both artifacts use the
`net.jpountz` packages and the `org.lz4.java` module name, and the codec
uses only APIs present in both, so an application that pins
`org.lz4:lz4-java` instead still works. The codec asks for lz4-java's
JNI implementation and falls back to its Java one only when the native
library cannot load, because `LZ4Factory.fastestInstance()` settles for
Java whenever lz4-java is not loaded by the system class loader.
### Performance
`CompressionCodecBenchmarks` (new, in `performance`) on an Apple M3
Ultra, JDK 17, 8 MiB buffers:
| Benchmark | LZ4_FRAME | ZSTD |
| --- | ---: | ---: |
| compress, sequential `int64` | 8.5 ms | 12.5 ms |
| compress, random decimal text | 9.9 ms | 44.6 ms |
| decompress, sequential `int64` | 2.1 ms | 4.8 ms |
| decompress, random decimal text | 1.9 ms | 8.5 ms |
The previous codec is too slow for the same JMH run. From a simple
timing harness on the same machine:
| Input | Previous compress | New compress | Previous decompress | New
decompress |
| --- | ---: | ---: | ---: | ---: |
| 8 MiB sequential `int64` | 278 s | 7.8 ms | 93.5 ms | 2.2 ms |
| 8 MiB of `i % 1000` as text | 17.9 ms | 0.3 ms | 3.3 ms | 0.3 ms |
| 8 MiB random bytes | 243 ms | 0.5 ms | (stored) | (stored) |
| 1 MiB random decimal text | 38.5 s | 1.4 ms | 33.3 ms | 0.3 ms |
### Compression ratio
On the inputs above, the ratio is unchanged except for random decimal
text, where commons-compress's more thorough match search reached 1.61
and lz4-java's fast compressor reaches 1.10. The new output matches what
Arrow C++ writes by default: pyarrow's LZ4 codec also reaches 1.10 on
the same kind of data, and 1.63 at compression level 9.
`CommonsCompressionFactory.createCodec(LZ4_FRAME, level)` ignores the
level today. Mapping it to lz4-java's high-compression mode would give
users who want the better ratio a way to ask for it; I left that out to
keep this change focused, and can add it here or in a follow-up.
### Testing
- The existing `TestCompressionCodec` and
`TestArrowReaderWriterWithCompression` pass unchanged, including the
decompressed-length check from #1207.
- `TestLz4CompressionCodec` (new):
- round-trips buffers across block boundaries, including one whose first
block is incompressible and stored, and checks that commons-compress and
lz4-java's `LZ4FrameInputStream` both read the frames this codec writes;
- decompresses frames written by commons-compress for every block size,
with linked and independent blocks, with and without checksums, and a
frame written by lz4-java with a content size;
- rejects corrupt frames (descriptor, block and content checksums, a
missing end mark, blocks that run past the frame or exceed the maximum
block size, undecodable blocks, a wrong content size, and recorded
lengths that are too long or too short), and releases everything it
allocated.
- I also decoded pyarrow 26 output (buffers over 64 KiB, linked blocks)
by hand to confirm that lz4-java rejects linked frames and
commons-compress reads them, which is why the fallback stays.
### AI usage
The code, tests and benchmark in this PR were written with an AI coding
assistant. The frame behavior described above (pyarrow's linked blocks,
lz4-java's rejection of them) and the benchmark numbers come from
running them.
Closes #192.
1 parent 3c9e49b commit 2ab1b02
7 files changed
Lines changed: 775 additions & 32 deletions
File tree
- compression
- src
- main/java
- org/apache/arrow/compression
- test/java/org/apache/arrow/compression
- performance
- src/main/java/org/apache/arrow/compression
- tools/src/shade
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
60 | 65 | | |
61 | 66 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
| 31 | + | |
26 | 32 | | |
27 | 33 | | |
28 | 34 | | |
29 | 35 | | |
30 | | - | |
31 | 36 | | |
32 | 37 | | |
33 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
34 | 47 | | |
35 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
36 | 76 | | |
37 | 77 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
42 | 124 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
51 | 143 | | |
| 144 | + | |
| 145 | + | |
52 | 146 | | |
53 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
54 | 175 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
60 | 272 | | |
61 | 273 | | |
62 | | - | |
63 | | - | |
| 274 | + | |
| 275 | + | |
64 | 276 | | |
65 | 277 | | |
66 | 278 | | |
67 | 279 | | |
68 | 280 | | |
69 | 281 | | |
70 | 282 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 283 | + | |
| 284 | + | |
75 | 285 | | |
76 | 286 | | |
77 | 287 | | |
| |||
81 | 291 | | |
82 | 292 | | |
83 | 293 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
| 294 | + | |
89 | 295 | | |
90 | 296 | | |
91 | 297 | | |
| |||
97 | 303 | | |
98 | 304 | | |
99 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
100 | 359 | | |
0 commit comments