Skip to content

GH-3987: Add direct byte[] decompress fast path to NonBlockedDecompressor - #3989

Closed
wangyum wants to merge 1 commit into
apache:masterfrom
wangyum:fix-snappy-direct-decompress
Closed

wangyum wants to merge 1 commit into
apache:masterfrom
wangyum:fix-snappy-direct-decompress

Conversation

@wangyum

@wangyum wangyum commented Nov 23, 2026

Copy link
Copy Markdown
Member

Rationale

See #3987 for the full writeup. Summary: NonBlockedDecompressor#decompress(byte[], int, int) always routes through an intermediate off-heap outputBuffer, copying the decompressed bytes out into the caller's array afterwards. Parquet's own callers always request the whole decompressed page in one call, so that copy is never avoided — for pages holding large binary/string values it shows up clearly in profiling.

Changes

  • NonBlockedDecompressor: add an opt-in fast path used when the caller's buffer can hold the whole decompressed page in one call. The codec copies the (typically much smaller) compressed bytes into a reusable heap scratch array and decompresses straight into the caller's destination array, skipping outputBuffer entirely. Gated behind a new supportsDirectDecompress() hook (defaults to false, so behavior is unchanged for codecs that don't opt in).
  • SnappyDecompressor: implements the new hooks via org.xerial.snappy.Snappy's existing byte[]-to-byte[] API.
  • TestSnappyCodec: new test covering a multi-MB payload through the fast path (and reuse across differently-sized pages, to exercise the scratch buffer growth), since the existing tests only use a few bytes.

Testing

Ran TestSnappyCodec, TestCompressionCodec, TestInteropReadLz4RawCodec (confirms LZ4-raw, which doesn't opt in, is unaffected), and TestParquetWriter (full Snappy round trip) — all pass. spotless:check passes.

Microbenchmark decompressing a 1MB value through the real CodecFactory-produced decompressor: ~190us/op before, ~134us/op after (~1.25x), consistent with the removed outputBuffer copy.

Note: I verified this change applies cleanly to current master and re-ran the above tests against a very recent commit before rebasing, but could not do a full local reactor build against the exact current tip because my local thrift (0.22.0) is older than what parquet-format-structures currently requires (0.25.0) — the change itself doesn't touch anything thrift-generated, so I don't expect this to matter, but flagging it so CI is the source of truth here.

Fixes #3987

…compressor

NonBlockedDecompressor#decompress(byte[], int, int) always routed
through an intermediate off-heap outputBuffer, even though Parquet's
own callers always request the entire decompressed page in one call.
For large pages this extra buffer-to-buffer copy showed up clearly in
profiling.

Add an opt-in fast path: when the destination buffer can hold the
whole decompressed page and the codec exposes a byte[]-to-byte[] API
(Snappy does), decompress straight into the caller's array, skipping
the off-heap outputBuffer and the copy out of it.

Fixes apache#3987
@wangyum

wangyum commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Closing this PR. While following up on a regression this fast path caused (dictionary pages silently falling back to the slow path, which took several more commits to track down), I benchmarked the new byte[]-to-byte[] fast path against the original off-heap ByteBuffer path it replaces (JMH, 5 forks x 10 iterations):

Page size byte[] fast path (this PR) ByteBuffer path (original)
8 KB 431,149 ± 138,404 ops/s 1,093,792 ± 112,247 ops/s (2.5x faster)
64 KB 89,835 ± 28,686 ops/s 165,744 ± 7,533 ops/s (1.85x faster)
1 MB 6,685 ± 2,004 ops/s 13,984 ± 517 ops/s (2.1x faster)
8 MB 1,063 ± 139 ops/s 1,210 ± 54 ops/s (~12% faster)

The original path is faster at every size tested. Profiling with async-profiler explains why: the byte[]-to-byte[] JNI call (xerial snappy-java) requires GetPrimitiveArrayCritical/ReleasePrimitiveArrayCritical to pin both arrays against GC movement (plus G1's pin_object/unpin_object underneath), while the existing ByteBuffer-to-ByteBuffer call only needs GetDirectBufferAddress - no pinning at all, since direct buffers are never relocated by the GC. That per-call JNI pinning overhead outweighs the single ByteBuffer.get() copy this fast path was meant to avoid, especially at small/medium page sizes where it dominates the call cost.

Not pursuing this approach further - closing in favor of re-investigating with an approach that doesn't hit the array-pinning cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid extra buffer copy when decompressing with NonBlockedDecompressor (Snappy)

1 participant