Elffuss BraidHub

kikocis/java-driver public

Eliminate memory copies in LZ4Compressor

Motivation:

Use ByteBuffer for compress / decompress to reduce memory copies / object allocations.

Modifications:

- Use ByteBuffer and so not need to do any extra memory copies for compress / decompress.

Result:

Faster compression / decompression when using lz4.
Changesetea1eb34ee4403b839af9c403c37266b3f9af98515a9da121121efc4634379bc0
AuthorNorman Maurer <norman_maurer@apple.com> · 2016-07-12 16:38 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-09-20 16:19 -07:00
Parentsb37b671958a5

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +37 −24
@@ -207,44 +207,57 @@
207207
208208 @Override
209209 public Frame compress(Frame frame) throws IOException {
210- byte[] input = CBUtil.readRawBytes(frame.body);
211-
212- int maxCompressedLength = compressor.maxCompressedLength(input.length);
213- byte[] output = new byte[INTEGER_BYTES + maxCompressedLength];
214-
215- output[0] = (byte) (input.length >>> 24);
216- output[1] = (byte) (input.length >>> 16);
217- output[2] = (byte) (input.length >>> 8);
218- output[3] = (byte) (input.length);
219-
210+ ByteBuf input = frame.body;
211+ // Using internalNioBuffer(...) as we only hold the reference in this method and so can
212+ // reduce Object allocations.
213+ ByteBuffer in = inputNioBuffer(input, input.readerIndex(), input.readableBytes());
214+
215+ int maxCompressedLength = compressor.maxCompressedLength(in.remaining());
216+ ByteBuf output = input.alloc().directBuffer(INTEGER_BYTES + maxCompressedLength);
220217 try {
221- int written = compressor.compress(input, 0, input.length, output, INTEGER_BYTES, maxCompressedLength);
222- return frame.with(Unpooled.wrappedBuffer(output, 0, INTEGER_BYTES + written));
218+ output.writeInt(in.remaining());
219+ // Using internalNioBuffer(...) as we only hold the reference in this method and so can
220+ // reduce Object allocations.
221+ ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes());
222+ int written = compressor.compress(in, in.position(), in.remaining(), out, out.position(), out.remaining());
223+ // Set the writer index so the amount of written bytes is reflected
224+ output.writerIndex(output.writerIndex() + written);
225+ return frame.with(output);
223226 } catch (Exception e) {
227+ // release output buffer so we not leak and rethrow exception.
228+ output.release();
224229 throw new IOException(e);
225230 }
226231 }
227232
228233 @Override
229234 public Frame decompress(Frame frame) throws IOException {
230- byte[] input = CBUtil.readRawBytes(frame.body);
231-
232- int uncompressedLength = ((input[0] & 0xFF) << 24)
233- | ((input[1] & 0xFF) << 16)
234- | ((input[2] & 0xFF) << 8)
235- | ((input[3] & 0xFF));
236-
237- byte[] output = new byte[uncompressedLength];
238-
235+ ByteBuf input = frame.body;
236+ int readable = input.readableBytes();
237+ int uncompressedLength = input.readInt();
238+ ByteBuffer in = inputNioBuffer(input, input.readerIndex(), input.readableBytes());
239+ input.readerIndex(input.writerIndex());
240+ ByteBuf output = input.alloc().directBuffer(uncompressedLength);
239241 try {
240- int read = decompressor.decompress(input, INTEGER_BYTES, output, 0, uncompressedLength);
241- if (read != input.length - INTEGER_BYTES)
242+ ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes());
243+ int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining());
244+ if (read != readable - INTEGER_BYTES)
242245 throw new IOException("Compressed lengths mismatch");
243246
244- return frame.with(Unpooled.wrappedBuffer(output));
247+ // Set the writer index so the amount of written bytes is reflected
248+ output.writerIndex(output.writerIndex() + uncompressedLength);
249+ return frame.with(output);
245250 } catch (Exception e) {
251+ // release output buffer so we not leak and rethrow exception.
252+ output.release();
246253 throw new IOException(e);
247254 }
248255 }
256+
257+ private static ByteBuffer inputNioBuffer(ByteBuf buf, int index, int len) {
258+ // Using internalNioBuffer(...) as we only hold the reference in this method and so can
259+ // reduce Object allocations.
260+ return buf.nioBufferCount() == 1 ? buf.internalNioBuffer(index, len) : buf.nioBuffer(index, len);
261+ }
249262 }
250263 }