Elffuss BraidHub

kikocis/java-driver public

Eliminate memory copies in SnappyFrameCompressor and use netty's allocator for allocations

Motiviation:

Snappy allows to use ByteBuffer for compress/decompress when direct buffers are used. At the moment a memory copy to heap buffers is done, which is wasteful.

Modification:

Use ByteBuffer for compress/decompress when possible and allocate the buffer from the ByteBufAllocator of netty which may be a pooled implementation.

Result:

Less memory copies == Better performance.
Changesetb37b671958a5a8c2f65f5cf4991a076dcee6648fcbeb5e84762f2e2698f16584
AuthorNorman Maurer <norman_maurer@apple.com> · 2016-07-12 07:04 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-09-20 16:19 -07:00
Parents9945f5df57d4

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/FrameCompressor.java +109 −11
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.exceptions.DriverInternalError;
19+import io.netty.buffer.ByteBuf;
1920 import io.netty.buffer.Unpooled;
2021 import net.jpountz.lz4.LZ4Factory;
2122 import org.slf4j.Logger;
@@ -23,6 +24,7 @@
2324 import org.xerial.snappy.Snappy;
2425
2526 import java.io.IOException;
27+import java.nio.ByteBuffer;
2628
2729 abstract class FrameCompressor {
2830
@@ -58,23 +60,119 @@
5860
5961 @Override
6062 public Frame compress(Frame frame) throws IOException {
61- byte[] input = CBUtil.readRawBytes(frame.body);
62- byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
63-
64- int written = Snappy.compress(input, 0, input.length, output, 0);
65- return frame.with(Unpooled.wrappedBuffer(output, 0, written));
63+ ByteBuf input = frame.body;
64+ int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes());
65+
66+ final ByteBuf frameBody;
67+ if (input.isDirect()) {
68+ // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
69+ // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
70+ ByteBuf output = input.alloc().directBuffer(maxCompressedLength);
71+ try {
72+ // Using internalNioBuffer(...) as we only hold the reference in this method and so can
73+ // reduce Object allocations.
74+ ByteBuffer in = input.internalNioBuffer(input.readerIndex(), input.readableBytes());
75+ // Increase reader index.
76+ input.readerIndex(input.writerIndex());
77+
78+ ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes());
79+ int written = Snappy.compress(in, out);
80+ // Set the writer index so the amount of written bytes is reflected
81+ output.writerIndex(output.writerIndex() + written);
82+ frameBody = output;
83+ } catch (IOException e) {
84+ // release output buffer so we not leak and rethrow exception.
85+ output.release();
86+ throw e;
87+ }
88+ } else {
89+ int inOffset = input.arrayOffset() + input.readerIndex();
90+ byte[] in = input.array();
91+ int len = input.readableBytes();
92+ // Increase reader index.
93+ input.readerIndex(input.writerIndex());
94+
95+ // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
96+ // can eliminate the overhead of allocate a new byte[].
97+ ByteBuf output = input.alloc().heapBuffer(maxCompressedLength);
98+ try {
99+ // Calculate the correct offset.
100+ int offset = output.arrayOffset() + output.writerIndex();
101+ byte[] out = output.array();
102+ int written = Snappy.compress(in, inOffset, len, out, offset);
103+
104+ // Increase the writerIndex with the written bytes.
105+ output.writerIndex(output.writerIndex() + written);
106+ frameBody = output;
107+ } catch (IOException e) {
108+ // release output buffer so we not leak and rethrow exception.
109+ output.release();
110+ throw e;
111+ }
112+ }
113+ return frame.with(frameBody);
66114 }
67115
68116 @Override
69117 public Frame decompress(Frame frame) throws IOException {
70- byte[] input = CBUtil.readRawBytes(frame.body);
118+ ByteBuf input = frame.body;
119+ final ByteBuf frameBody;
71120
72- if (!Snappy.isValidCompressedBuffer(input, 0, input.length))
73- throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");
121+ if (input.isDirect()) {
122+ // Using internalNioBuffer(...) as we only hold the reference in this method and so can
123+ // reduce Object allocations.
124+ ByteBuffer in = input.internalNioBuffer(input.readerIndex(), input.readableBytes());
125+ // Increase reader index.
126+ input.readerIndex(input.writerIndex());
74127
75- byte[] output = new byte[Snappy.uncompressedLength(input)];
76- int size = Snappy.uncompress(input, 0, input.length, output, 0);
77- return frame.with(Unpooled.wrappedBuffer(output, 0, size));
128+ if (!Snappy.isValidCompressedBuffer(in))
129+ throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");
130+
131+ // If the input is direct we will allocate a direct output buffer as well as this will allow us to use
132+ // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies.
133+ ByteBuf output = frame.body.alloc().directBuffer(Snappy.uncompressedLength(in));
134+ try {
135+ ByteBuffer out = output.internalNioBuffer(output.writerIndex(), output.writableBytes());
136+
137+ int size = Snappy.uncompress(in, out);
138+ // Set the writer index so the amount of written bytes is reflected
139+ output.writerIndex(output.writerIndex() + size);
140+ frameBody = output;
141+ } catch (IOException e) {
142+ // release output buffer so we not leak and rethrow exception.
143+ output.release();
144+ throw e;
145+ }
146+ } else {
147+ // Not a direct buffer so use byte arrays...
148+ int inOffset = input.arrayOffset() + input.readerIndex();
149+ byte[] in = input.array();
150+ int len = input.readableBytes();
151+ // Increase reader index.
152+ input.readerIndex(input.writerIndex());
153+
154+ if (!Snappy.isValidCompressedBuffer(in, inOffset, len))
155+ throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");
156+
157+ // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so
158+ // can eliminate the overhead of allocate a new byte[].
159+ ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len));
160+ try {
161+ // Calculate the correct offset.
162+ int offset = output.arrayOffset() + output.writerIndex();
163+ byte[] out = output.array();
164+ int written = Snappy.uncompress(in, inOffset, len, out, offset);
165+
166+ // Increase the writerIndex with the written bytes.
167+ output.writerIndex(output.writerIndex() + written);
168+ frameBody = output;
169+ } catch (IOException e) {
170+ // release output buffer so we not leak and rethrow exception.
171+ output.release();
172+ throw e;
173+ }
174+ }
175+ return frame.with(frameBody);
78176 }
79177 }
80178