-
Notifications
You must be signed in to change notification settings - Fork 4k
protobuf: zero copy into protobuf #7250
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c8810ad
Define an interface for retrieving a list of ByteBuffers.
voidzcy 17d6dbb
Add methods to ReadableBuffer to support returning read bytes via Byt…
voidzcy 83f0448
CompositeReadableBuffer's implementation for reading by returning Byt…
voidzcy 69a1ddf
NettyReadableBuffer's implementation for reading by directly returnin…
voidzcy 88d2b6f
Deframer gives an InputStream if the backing ReadableBuffer supports …
voidzcy 94b5124
Feed a list of ByteBuffers into protobuf if the InputStream implement…
voidzcy ce8969e
Fix readByteBuffers operation for CompositeReadableBuffer.
voidzcy a16b6ae
Improve ByteBuffer read for netty.
voidzcy 0cb6ea8
update interface for getting a list of ByteBuffers.
voidzcy f9aaffc
Improve feeding ByteBuffer to protobuf.
voidzcy 8a0c7a2
Should not close ReadableBuffer before reading the returned ByteBuffe…
voidzcy 8fb4534
Add TODO for remembering current implementation leaks buffer.
voidzcy 58165d6
Replace ByteBufferReadable interface with ManagedBytes, which takes t…
voidzcy e2b3991
Add api for reading data as ManagedBytes from ReadableBuffer.
voidzcy 8959de5
Implement reading via ManagedBytes for netty.
voidzcy 86414ad
Implement reading via ManagedBytes for composite.
voidzcy 093e435
Change stream to use ManagedBytes for data transfer.
voidzcy c9c123c
Use new ManagedBufferReadable api to transfer data into protobuf and …
voidzcy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2020 The gRPC Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.grpc; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A logical representation of bytes with the responsibility of managing the resource lifecycle | ||
| * used to hold the data. Usually used for transferring data by directly hand over the ownership | ||
| * of the backing resources. | ||
| */ | ||
| public abstract class ManagedBytes { | ||
|
|
||
| /** | ||
| * Returns the data in a list of {@link ByteBuffer}s. | ||
| */ | ||
| public abstract List<ByteBuffer> asByteBuffers(); | ||
|
|
||
| /** | ||
| * Release the usage of the data. The underlying resource used to hold the data may be released | ||
| * and no more access should be attempted. | ||
| */ | ||
| public void release() { | ||
| // no-op | ||
| } | ||
|
|
||
| /** | ||
| * A readable data source that supports the operation of reading its content into | ||
| * {@link ManagedBytes}. | ||
| */ | ||
| public interface ManagedBytesReadable { | ||
|
|
||
| /** | ||
| * Reads up to a total of {@code length} bytes as {@link ManagedBytes}. | ||
| * | ||
| * @param length the maximum number of bytes to be read. | ||
| * @return {@link ManagedBytes} that contains the bytes being read or {@code null} | ||
| * if no more bytes can be read. | ||
| */ | ||
| @Nullable | ||
| ManagedBytes readManagedBytes(int length); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,14 @@ | |
| package io.grpc.netty; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import io.grpc.ManagedBytes; | ||
| import io.grpc.internal.AbstractReadableBuffer; | ||
| import io.netty.buffer.ByteBuf; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A {@link java.nio.Buffer} implementation that is backed by a Netty {@link ByteBuf}. This class | ||
|
|
@@ -79,6 +82,26 @@ public NettyReadableBuffer readBytes(int length) { | |
| return new NettyReadableBuffer(buffer.readRetainedSlice(length)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldUseManagedBytes() { | ||
| return buffer.nioBufferCount() > 0; | ||
| } | ||
|
|
||
| @Override | ||
| public ManagedBytes readManagedBytes(int length) { | ||
| if (buffer.readableBytes() < length) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
|
Contributor
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. Return empty list here if |
||
| final List<ByteBuffer> res = Arrays.asList(buffer.nioBuffers(buffer.readerIndex(), length)); | ||
| buffer.skipBytes(length); | ||
| return new ManagedBytes() { | ||
| @Override | ||
| public List<ByteBuffer> asByteBuffers() { | ||
| return res; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasArray() { | ||
| return buffer.hasArray(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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):