Skip to content

Avoid extra buffer copy when decompressing with NonBlockedDecompressor (Snappy) #3987

Done

Description

@wangyum

Describe the enhancement requested

NonBlockedDecompressor#decompress(byte[], int, int) (used by SnappyCodec/LZ4-raw via NonBlockedDecompressorStream) always decompresses into an intermediate off-heap outputBuffer, then copies the result out into the caller-supplied destination array via outputBuffer.get(buffer, off, numBytes):

outputBuffer.clear();
int size = uncompress(inputBuffer, outputBuffer);
outputBuffer.limit(size);
...
int numBytes = Math.min(len, outputBuffer.remaining());
outputBuffer.get(buffer, off, numBytes);

For Parquet's own read path, the caller (BytesInput$StreamBytesInput#toByteArray) always requests the entire decompressed page in a single call, so this copy is never avoided — for large pages/values it shows up clearly in profiling (confirmed with async-profiler: _platform_bzero + the outputBuffer.get copy accounted for a large share of CPU time when decompressing pages holding large binary/string values).

Snappy's Java binding (org.xerial.snappy.Snappy) already exposes a byte[]-to-byte[] decompress API (Snappy.uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)) that doesn't need the intermediate off-heap buffer at all.

I'd like to propose adding an opt-in fast path to NonBlockedDecompressor: when the caller's destination buffer can hold the whole decompressed page in one call (always true for Parquet page decompression) and the codec supports it, decompress directly into the caller's array, skipping outputBuffer entirely. The codec still needs a small heap-side copy of the (typically much smaller) compressed bytes out of the existing direct inputBuffer, but that's far cheaper than copying the full decompressed page.

I have a draft patch (NonBlockedDecompressor gets two new protected hooks, supportsDirectDecompress()/uncompressDirect(...), defaulted off; SnappyDecompressor implements them) plus a new test (TestSnappyCodec#testSnappyLargeValueUsesDirectDecompressFastPath) covering a multi-MB payload through the new path, including the scratch-buffer growth. All existing codec tests (TestSnappyCodec, TestCompressionCodec, TestInteropReadLz4RawCodec, TestParquetWriter) still pass.

Measured with a microbenchmark decompressing a 1MB (36-byte-repeating) value through the real CodecFactory-produced decompressor: current path ~190us/op vs the direct path ~134us/op (~1.25x), consistent with the outputBuffer copy being the removed cost.

Happy to open a PR if this approach sounds reasonable. The same hook could later be implemented for LZ4-raw's existing byte[] API; I haven't looked at whether something equivalent is practical for Zstd (it goes through a separate, stream-based ZstdDecompressorStream today, not NonBlockedDecompressor).

Component(s)

Core

Activity

  1. wangyum commented on Nov 24, 2026

    @wangyum
    MemberAuthor

    Closing. Prototyped this in #3989 and benchmarked it against the existing path before merging - the proposed byte[]-to-byte[] fast path is actually 1.85x-2.5x slower than the current off-heap ByteBuffer path for typical page sizes (8KB-1MB), and only roughly breaks even at 8MB. See #3989 for the full benchmark numbers.

    async-profiler confirms the cause: the byte[]-to-byte[] JNI call requires pinning both arrays against GC movement (GetPrimitiveArrayCritical/Release, backed by G1's pin_object/unpin_object), while the current ByteBuffer-to-ByteBuffer call avoids that entirely via GetDirectBufferAddress, since direct buffers are never moved by the GC. The extra copy described in this issue is real, but this particular way of avoiding it costs more than it saves.

    Not pursuing this specific approach further.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions