Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public abstract class NonBlockedDecompressor implements Decompressor {
// Buffer for compressed input. This buffer grows as necessary.
private ByteBuffer inputBuffer = ByteBuffer.allocateDirect(0);

// Heap scratch space used only by the direct byte[] fast path below; holds a copy of the
// (typically much smaller) compressed bytes so codecs that expose a byte[]-to-byte[] API
// (e.g. Snappy) can decompress straight into the caller's destination array. Grows as necessary.
private byte[] compressedScratch = new byte[0];

private boolean finished;

/**
Expand Down Expand Up @@ -70,6 +75,25 @@ public synchronized int decompress(byte[] buffer, int off, int len) throws IOExc
Preconditions.checkArgument(outputBuffer.position() == 0, "Invalid position of 0.");
// There is compressed input, decompress it now.
int decompressedSize = maxUncompressedLength(inputBuffer, len);

if (decompressedSize <= len && supportsDirectDecompress()) {
// Fast path: the caller's buffer can hold the entire decompressed output in one call,
// which is always true for Parquet page decompression. Decompress directly into it,
// skipping the (potentially large) off-heap outputBuffer and the copy out of it. The
// compressed bytes still need a small heap-side copy since they live in a direct
// buffer, but compressed data is typically far smaller than decompressed data.
int compressedLen = inputBuffer.remaining();
if (compressedScratch.length < compressedLen) {
compressedScratch = new byte[compressedLen];
}
inputBuffer.get(compressedScratch, 0, compressedLen);
int size = uncompressDirect(compressedScratch, 0, compressedLen, buffer, off);
inputBuffer.clear();
inputBuffer.limit(0);
finished = true;
return size;
}

if (decompressedSize > outputBuffer.capacity()) {
ByteBuffer oldBuffer = outputBuffer;
outputBuffer = ByteBuffer.allocateDirect(decompressedSize);
Expand Down Expand Up @@ -192,4 +216,31 @@ public void setDictionary(byte[] b, int off, int len) {
* @return uncompressed data size
*/
protected abstract int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) throws IOException;

/**
* @return true if this codec implements {@link #uncompressDirect}. The default is false;
* codecs whose underlying library exposes a byte[]-to-byte[] decompress API (e.g. Snappy)
* should override both this and {@link #uncompressDirect}.
*/
protected boolean supportsDirectDecompress() {
return false;
}

/**
* Optional fast path: decompress directly into the caller-supplied destination array,
* avoiding the intermediate off-heap output buffer used by {@link #uncompress(ByteBuffer, ByteBuffer)}.
* Only called when {@link #supportsDirectDecompress()} returns true.
*
* @param compressed buffer containing the compressed input
* @param compressedOffset start offset of the compressed data
* @param compressedLength length of the compressed data
* @param uncompressed destination for the uncompressed data
* @param uncompressedOffset start offset to write the uncompressed data at
* @return the number of uncompressed bytes written
*/
protected int uncompressDirect(
byte[] compressed, int compressedOffset, int compressedLength, byte[] uncompressed, int uncompressedOffset)
throws IOException {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ protected int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) throws
protected int maxUncompressedLength(ByteBuffer compressed, int maxUncompressedLength) throws IOException {
return Snappy.uncompressedLength(compressed);
}

@Override
protected boolean supportsDirectDecompress() {
return true;
}

@Override
protected int uncompressDirect(
byte[] compressed, int compressedOffset, int compressedLength, byte[] uncompressed, int uncompressedOffset)
throws IOException {
return Snappy.uncompress(compressed, compressedOffset, compressedLength, uncompressed, uncompressedOffset);
}
} // class SnappyDecompressor
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
Expand All @@ -48,6 +49,49 @@ public void TestSnappy() throws IOException {
TestSnappy(compressor, decompressor, "FooBar");
}

@Test
public void testSnappyLargeValueUsesDirectDecompressFastPath() throws IOException {
// Large enough that the whole page is decompressed in a single call, exercising
// NonBlockedDecompressor's direct byte[] fast path (and its scratch buffer growth),
// rather than the small multi-call draining path covered by TestSnappyStream.
byte[] uncompressedData = new byte[2_000_000];
new Random(42).nextBytes(uncompressedData);

SnappyCompressor compressor = new SnappyCompressor();
compressor.setInput(uncompressedData, 0, uncompressedData.length);
compressor.finish();
byte[] compressedData = new byte[Snappy.maxCompressedLength(uncompressedData.length)];
int compressedSize = compressor.compress(compressedData, 0, compressedData.length);

SnappyDecompressor decompressor = new SnappyDecompressor();
decompressor.setInput(compressedData, 0, compressedSize);
byte[] decompressedData = new byte[uncompressedData.length];
int decompressedSize = decompressor.decompress(decompressedData, 0, decompressedData.length);

assertThat(decompressor.finished()).isTrue();
assertThat(decompressedSize).isEqualTo(uncompressedData.length);
assertThat(decompressedData).isEqualTo(uncompressedData);

// Reuse across multiple pages of different sizes to exercise the scratch buffer
// shrinking/growing (it never shrinks, but must grow correctly for a bigger page).
byte[] bigger = new byte[3_000_000];
new Random(7).nextBytes(bigger);
SnappyCompressor compressor2 = new SnappyCompressor();
compressor2.setInput(bigger, 0, bigger.length);
compressor2.finish();
byte[] compressed2 = new byte[Snappy.maxCompressedLength(bigger.length)];
int compressedSize2 = compressor2.compress(compressed2, 0, compressed2.length);

decompressor.reset();
decompressor.setInput(compressed2, 0, compressedSize2);
byte[] decompressed2 = new byte[bigger.length];
int decompressedSize2 = decompressor.decompress(decompressed2, 0, decompressed2.length);

assertThat(decompressor.finished()).isTrue();
assertThat(decompressedSize2).isEqualTo(bigger.length);
assertThat(decompressed2).isEqualTo(bigger);
}

@Test
public void TestSnappyStream() throws IOException {
SnappyCodec codec = new SnappyCodec();
Expand Down