Skip to content

Conversation

@voidzcy
Copy link
Contributor

@voidzcy voidzcy commented Jul 24, 2020

TODO: tests and Javadoc enhancement.

@voidzcy voidzcy force-pushed the impl/zero_copy_to_protobuf branch from fe6c132 to 94b5124 Compare July 24, 2020 21:18
Copy link
Contributor

@njhill njhill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@voidzcy thanks, I'm quite interested in this change! I hope you don't mind the flyby comments

* @throws UnsupportedOperationException the buffer does not support this method
* @throws IndexOutOfBoundsException if required bytes are not readable
*/
List<ByteBuffer> readByteBuffers(int length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just returning null here instead of separate boolean method? This would also be more efficient for composite case since it means not having to iterate over the buffers an additional time.

if (buffer.readableBytes() < length) {
throw new IndexOutOfBoundsException();
}
List<ByteBuffer> res = Arrays.asList(buffer.nioBuffers(buffer.readerIndex(), length));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here would be preferable to do:

List<ByteBuffer> res = buffer.nioBufferCount() == 1
    ? Collections.singletonList(buffer.nioBuffer(buffer.readableIndex(), length)
    : Arrays.asList(buffer.nioBuffers(buffer.readerIndex(), length));

public List<ByteBuffer> readByteBuffers(int length) {
if (buffer.readableBytes() < length) {
throw new IndexOutOfBoundsException();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return empty list here if length == 0?

// messages.
if (stream instanceof ByteBufferReadable) {
cis = CodedInputStream.newInstance(
((ByteBufferReadable) stream).readByteBuffers(size));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the protobuf code doesn't do this itself so would be better to handle (most common) singleton case separately:

List<ByteBuffer> bufs = ((ByteBufferReadable) stream).readByteBuffers(size);
cis = bufs.size() == 1 ? CodedInputStream.newInstance(bufs.get(0)) : CodedInputStream.newInstance(bufs);

* bytes can be read.
*/
@Nullable
Iterable<ByteBuffer> readByteBuffers(int length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return Collections.emptyList() instead of null?

int readLength = length;
if (buffer.readableBytes() <= length) {
readLength = buffer.readableBytes();
buffers.poll();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffers.remove()?

@Override
public List<ByteBuffer> readByteBuffers(int length) {
checkReadable(length);
readableBytes -= length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to decrement this within the loop so that the buffer state remains consistent in case of runtime exception?

Also how about a special case (should be common):

if (buffers.size() == 1) {
  return (readableBytes == 0 ? buffers.poll() : buffers.peek()).readByteBuffers(length);
}

@voidzcy
Copy link
Contributor Author

voidzcy commented Aug 17, 2020

Close in favor of #7330.

@voidzcy voidzcy closed this Aug 17, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants