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

api, core: support zero copy into protobuf #8102

Merged
merged 48 commits into from
May 14, 2021
Merged
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9936df1
Add mark&reset methods and canUseByteBuffer&getByteBuffer methods to …
voidzcy Aug 17, 2020
aa93e1d
Default implementations.
voidzcy Aug 17, 2020
454e224
Wire new methods for forwarder
voidzcy Aug 17, 2020
3f47d5e
Support mark&reset and retrieving content via ByteBuffer for netty.
voidzcy Aug 17, 2020
b3c9974
Implementation for composite.
voidzcy Aug 17, 2020
2ede525
Define interface for accessing readable content via ByteBuffers.
voidzcy Aug 17, 2020
3dca312
Implement mark&reset for simple readable buffers.
voidzcy Aug 17, 2020
6249353
Use HasByteBuffer interface for accesing input stream's backing ByteB…
voidzcy Aug 17, 2020
29dce67
Eliminate the length argument for retrieving the ByteBuffer.
voidzcy Aug 17, 2020
22ea6c3
Do no require netty buffer to be direct from API's perspective.
voidzcy Aug 17, 2020
53e347c
Use Deque operations to avoid unncessary moves.
voidzcy Aug 17, 2020
27801fe
Make a list of ByteBuffers up-front instead of a running iterator.
voidzcy Aug 17, 2020
eb71a68
Add getByteBufferSupported method for HasByteBuffer so that it can be…
voidzcy Aug 17, 2020
5d3c657
It's not necessary to implement getByteBuffer for ByteReadbaleBufferW…
voidzcy Aug 17, 2020
9fd8d3c
Add test coverage for mark&reset and getByteBuffer for generic ByteBu…
voidzcy Aug 17, 2020
e3afe50
Add test coverage for netty's special get NIO bytebuffer operation.
voidzcy Aug 17, 2020
e2fdd07
Skip test for operations not supported by okhttp.
voidzcy Aug 17, 2020
033270b
Add test coverage for BufferInputStream with getByteBuffer operation.
voidzcy Aug 17, 2020
0622d51
Add test using a known-length input stream with getByteBuffer operati…
voidzcy Aug 17, 2020
0e8caee
Modify test method name.
voidzcy Aug 17, 2020
ba4e91b
Add test coverage for mark&reset and getByteBuffer for CompositeReada…
voidzcy Aug 17, 2020
69618b2
Add getByteBuffer support for ByteReadableBufferWrapper.
voidzcy Aug 20, 2020
437857d
Only pull ByteBuffers when message is large.
voidzcy Aug 21, 2020
1363505
Run ByteBuffer codepath only in Java 9+.
voidzcy Aug 28, 2020
c46eb73
Slight improvement for avoiding array creation if not necessary.
voidzcy Aug 28, 2020
7b4e070
Merge branch 'master' of github.com:grpc/grpc-java into impl/zero_cop…
voidzcy Aug 28, 2020
772b3ba
Change ReadableBuffer#canUseByteBuffer to hasByteBuffer.
voidzcy Sep 1, 2020
b1c99e5
Removed unnecessary reset.
voidzcy Sep 1, 2020
692076c
Simplify checking runtime java version.
voidzcy Sep 1, 2020
10c13b8
Add ExperimentalApi annotation.
voidzcy Sep 1, 2020
f13c165
Rename ReadableBuffer#hasByteBuffer to getByteBufferSupported.
voidzcy Sep 2, 2020
5c562b3
Merge branch 'master' of github.com:grpc/grpc-java into impl/zero_cop…
voidzcy Apr 20, 2021
0f804b7
Revert changes for MessageMarshaller.
voidzcy Apr 20, 2021
ad64a1d
Add Retainable interface that allows taking over resource ownership a…
voidzcy Apr 20, 2021
aa902aa
Make BufferInputStream implements Retainable. Its close() method beco…
voidzcy Apr 20, 2021
6cf0739
Remove no longer needed constructors.
voidzcy Apr 22, 2021
bfe1d7d
Change return type to be more specific.
voidzcy Apr 22, 2021
4d90800
Restore optimizations for avoid allocating new buffer wrappers.
voidzcy Apr 24, 2021
d8d030a
Optimize by allocating rewindable buffer deque lazily.
voidzcy Apr 27, 2021
f1f1c95
Change to Detachable API, which makes the original InputStream behave…
voidzcy May 6, 2021
a2f3a9e
Change naming for getByteBufferSupported().
voidzcy May 6, 2021
61c3447
Eliminate generics, use Object and casts instead.
voidzcy May 6, 2021
15a0314
Return Detachable instead of Object.
voidzcy May 6, 2021
f8afce5
Hook BufferInputStream's markSupported() with underlying buffers.
voidzcy May 6, 2021
2a6cbb6
Update Detachable interface definition, make it more specific to Inpu…
voidzcy May 12, 2021
e534f4a
Replace the internal buffer of BufferInputStream with an empty buffer…
voidzcy May 12, 2021
ad8940f
Add ExperimentalApi link.
voidzcy May 14, 2021
a6c1b13
Update Javadoc for Detachable.
voidzcy May 14, 2021
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
Prev Previous commit
Next Next commit
Restore optimizations for avoid allocating new buffer wrappers.
voidzcy committed Apr 24, 2021
commit 4d908003ea9721494fa6f3d2efd7beb076b75a59
78 changes: 57 additions & 21 deletions core/src/main/java/io/grpc/internal/CompositeReadableBuffer.java
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@ public class CompositeReadableBuffer extends AbstractReadableBuffer {
private int readableBytes;
private boolean marked;

public CompositeReadableBuffer(int initialCapacity) {
readableBuffers = new ArrayDeque<>(initialCapacity);
rewindableBuffers = new ArrayDeque<>(initialCapacity);
}

public CompositeReadableBuffer() {
readableBuffers = new ArrayDeque<>();
rewindableBuffers = new ArrayDeque<>();
@@ -156,20 +161,44 @@ public void readBytes(OutputStream dest, int length) throws IOException {
execute(STREAM_OP, length, dest, 0);
}

private static final NoThrowReadOperation<CompositeReadableBuffer> COMPOSITE_OP =
new NoThrowReadOperation<CompositeReadableBuffer>() {
@Override
public int read(ReadableBuffer buffer, int length, CompositeReadableBuffer dest,
int unused) {
dest.addBuffer(buffer.readBytes(length));
return 0;
}
};

@Override
public CompositeReadableBuffer readBytes(int length) {
final CompositeReadableBuffer newBuffer = new CompositeReadableBuffer();
executeNoThrow(COMPOSITE_OP, length, newBuffer, 0);
public ReadableBuffer readBytes(int length) {
if (length <= 0) {
return ReadableBuffers.empty();
}
checkReadable(length);
readableBytes -= length;

ReadableBuffer newBuffer = null;
CompositeReadableBuffer newComposite = null;
do {
ReadableBuffer buffer = readableBuffers.peek();
int readable = buffer.readableBytes();
ReadableBuffer readBuffer;
if (readable > length) {
readBuffer = buffer.readBytes(length);
length = 0;
} else {
if (marked) {
readBuffer = buffer.readBytes(readable);
advanceBuffer();
} else {
readBuffer = readableBuffers.poll();
}
length -= readable;
}
if (newBuffer == null) {
newBuffer = readBuffer;
} else {
if (newComposite == null) {
newComposite = new CompositeReadableBuffer(
length == 0 ? 2 : Math.min(readableBuffers.size() + 2, 16));
newComposite.addBuffer(newBuffer);
newBuffer = newComposite;
}
newComposite.addBuffer(readBuffer);
}
} while (length > 0);
return newBuffer;
}

@@ -276,15 +305,22 @@ private <T> int executeNoThrow(NoThrowReadOperation<T> op, int length, T dest, i
private void advanceBufferIfNecessary() {
ReadableBuffer buffer = readableBuffers.peek();
if (buffer.readableBytes() == 0) {
if (marked) {
rewindableBuffers.add(readableBuffers.remove());
ReadableBuffer next = readableBuffers.peek();
if (next != null) {
next.mark();
}
} else {
readableBuffers.remove().close();
advanceBuffer();
}
}

/**
* Removes one buffer from the front and closes it.
*/
private void advanceBuffer() {
if (marked) {
rewindableBuffers.add(readableBuffers.remove());
ReadableBuffer next = readableBuffers.peek();
if (next != null) {
next.mark();
}
} else {
readableBuffers.remove().close();
}
}