Skip to content
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

core: reduce CompositeReadableBuffer allocation #3279

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 20 additions & 6 deletions core/src/main/java/io/grpc/internal/CompositeReadableBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class CompositeReadableBuffer extends AbstractReadableBuffer {

private int readableBytes;
private final Queue<ReadableBuffer> buffers = new ArrayDeque<ReadableBuffer>();
private final Queue<ReadableBuffer> buffers = new ArrayDeque<ReadableBuffer>(2);

/**
* Adds a new {@link ReadableBuffer} at the end of the buffer list. After a buffer is added, it is
Expand Down Expand Up @@ -133,22 +133,36 @@ public int readInternal(ReadableBuffer buffer, int length) throws IOException {
}
}

@Override
public CompositeReadableBuffer readBytes(int length) {
/**
* Reads {@code length} bytes from this buffer and writes them to the destination buffer.
* Increments the read position by {@code length}. If the required bytes are not readable, throws
* {@link IndexOutOfBoundsException}.
*
* @param dest the destination buffer to receive the bytes.
* @param length the number of bytes to be copied.
* @throws IOException thrown if any error was encountered while writing to the stream.
* @throws IndexOutOfBoundsException if required bytes are not readable
*/
public void readBytes(CompositeReadableBuffer dest, int length) {
checkReadable(length);
readableBytes -= length;

CompositeReadableBuffer newBuffer = new CompositeReadableBuffer();
while (length > 0) {
ReadableBuffer buffer = buffers.peek();
if (buffer.readableBytes() > length) {
newBuffer.addBuffer(buffer.readBytes(length));
dest.addBuffer(buffer.readBytes(length));
length = 0;
} else {
newBuffer.addBuffer(buffers.poll());
dest.addBuffer(buffers.poll());
length -= buffer.readableBytes();
}
}
}

@Override
public CompositeReadableBuffer readBytes(int length) {
CompositeReadableBuffer newBuffer = new CompositeReadableBuffer();
readBytes(newBuffer, length);
return newBuffer;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/grpc/internal/MessageDeframer.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private boolean readRequiredBytes() {
}
int toRead = Math.min(missingBytes, unprocessed.readableBytes());
totalBytesRead += toRead;
nextFrame.addBuffer(unprocessed.readBytes(toRead));
unprocessed.readBytes(nextFrame, toRead);
}
return true;
} finally {
Expand Down