-
Notifications
You must be signed in to change notification settings - Fork 325
Avoid memory copy in MessagePacker.writePayload #87
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
Changes from 2 commits
d50670b
9f92b90
8fdf1e6
88f9dc3
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 |
|---|---|---|
|
|
@@ -359,27 +359,58 @@ public MessagePacker packRawStringHeader(int len) throws IOException { | |
| return this; | ||
| } | ||
|
|
||
| public MessagePacker writePayload(ByteBuffer bb) throws IOException { | ||
| while(bb.remaining() > 0) { | ||
| if(position >= buffer.size()) | ||
| flush(); | ||
| int writeLen = Math.min(buffer.size() - position, bb.remaining()); | ||
| buffer.putByteBuffer(position, bb, writeLen); | ||
| position += writeLen; | ||
| bb.position(bb.position() + writeLen); | ||
| private final int FLUSH_THRESHOLD = 512; | ||
|
|
||
| public MessagePacker writePayload(ByteBuffer src) throws IOException { | ||
| if(src.remaining() >= FLUSH_THRESHOLD) { | ||
| // Use the source ByteBuffet directly to avoid memory copy | ||
|
|
||
| // First, flush the current buffer contents | ||
| out.flush(buffer, 0, position); | ||
|
Member
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. You can call |
||
| position = 0; | ||
|
|
||
| // Wrap the input source as a MessageBuffer | ||
| MessageBuffer wrapped = MessageBuffer.wrap(src); | ||
| // Then, dump the source data to the output | ||
| out.flush(wrapped, src.position(), src.remaining()); | ||
|
Member
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. Looks good. Just another idea is to add
Member
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. You need to call |
||
| } | ||
| else { | ||
| // If the input source is small, simply copy the contents to the buffer | ||
| while(src.remaining() > 0) { | ||
| if(position >= buffer.size()) | ||
| flush(); | ||
| int writeLen = Math.min(buffer.size() - position, src.remaining()); | ||
| buffer.putByteBuffer(position, src, writeLen); | ||
| position += writeLen; | ||
| src.position(src.position() + writeLen); | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| public MessagePacker writePayload(byte[] o, int off, int len) throws IOException { | ||
| int cursor = 0; | ||
| while(cursor < len) { | ||
| if(position >= buffer.size()) | ||
| flush(); | ||
| int writeLen = Math.min(buffer.size() - position, len - cursor); | ||
| buffer.putBytes(position, o, off + cursor, writeLen); | ||
| position += writeLen; | ||
| cursor += writeLen; | ||
| public MessagePacker writePayload(byte[] src, int off, int len) throws IOException { | ||
| if(len >= FLUSH_THRESHOLD) { | ||
| // Use the input array directory to avoid memory copy | ||
|
|
||
| // Flush the current buffer contents | ||
| out.flush(buffer, 0, position); | ||
| position = 0; | ||
|
|
||
| // Wrap the input array as a MessageBuffer | ||
| MessageBuffer wrapped = MessageBuffer.wrap(src); | ||
| // Dump the source data to the output | ||
| out.flush(wrapped, off, len); | ||
| } | ||
| else { | ||
| int cursor = 0; | ||
| while(cursor < len) { | ||
| if(position >= buffer.size()) | ||
| flush(); | ||
| int writeLen = Math.min(buffer.size() - position, len - cursor); | ||
| buffer.putBytes(position, src, off + cursor, writeLen); | ||
| position += writeLen; | ||
| cursor += writeLen; | ||
| } | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
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.
(trivial) ByteBuffet -> ByteBuffer