-
Notifications
You must be signed in to change notification settings - Fork 34
Avoid composite buffers for small messages #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import io.netty.buffer.Unpooled; | ||
| import io.vertx.core.buffer.Buffer; | ||
| import io.vertx.core.buffer.impl.BufferInternal; | ||
| import io.vertx.core.buffer.impl.VertxByteBufAllocator; | ||
| import io.vertx.grpc.common.GrpcMessage; | ||
|
|
||
| public class GrpcMessageImpl implements GrpcMessage { | ||
|
|
@@ -38,15 +39,31 @@ public Buffer payload() { | |
| } | ||
|
|
||
| public static Buffer encode(GrpcMessage message) { | ||
| ByteBuf bbuf = ((BufferInternal)message.payload()).getByteBuf(); | ||
| ByteBuf bbuf = ((BufferInternal) message.payload()).getByteBuf(); | ||
| int len = bbuf.readableBytes(); | ||
| boolean compressed = !message.encoding().equals("identity"); | ||
| // it is worthy to just copy here, 'cause composite (heap) buffers are slower to be sent to the wire or copied | ||
| if (len <= 128) { | ||
| int totalBytes = 5 + len; | ||
| // let's use vertx buffer here because it doesn't have any atomic release, if unpooled | ||
| ByteBuf fullMsg = VertxByteBufAllocator.DEFAULT.heapBuffer(totalBytes, totalBytes); | ||
| fullMsg.setByte(0, compressed ? 1 : 0); // Compression flag | ||
| fullMsg.setInt(1, len); // Length | ||
| fullMsg.setBytes(5, bbuf, bbuf.readerIndex(), len); | ||
| fullMsg.writerIndex(totalBytes); | ||
| try { | ||
| return BufferInternal.buffer(fullMsg); | ||
| } finally { | ||
| bbuf.release(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vertx buffers are unreleasable and not pooled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, but the type is still |
||
| } | ||
| } | ||
| // slow-path | ||
| ByteBuf prefix = Unpooled.buffer(5, 5); | ||
| prefix.writeByte(compressed ? 1 : 0); // Compression flag | ||
| prefix.writeInt(len); // Length | ||
| CompositeByteBuf composite = Unpooled.compositeBuffer(); | ||
| composite.addComponent(true, prefix); | ||
| composite.addComponent(true, bbuf); | ||
| CompositeByteBuf composite = Unpooled.compositeBuffer(2); | ||
| composite.addComponent(true, 0, prefix); | ||
| composite.addComponent(true, 1, bbuf); | ||
| return BufferInternal.buffer(composite); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we would like to use pooled direct buffers here instead, wdyt @vietj ?
It would save the transport later to copy it into a direct one :/