Skip to content

Commit 2084099

Browse files
committed
without allocateKnownLength()
1 parent 0a3284e commit 2084099

File tree

5 files changed

+5
-41
lines changed

5 files changed

+5
-41
lines changed

core/src/main/java/io/grpc/internal/MessageFramer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private int writeKnownLengthUncompressed(InputStream message, int messageLength)
239239
* that.
240240
*/
241241
private WritableBuffer allocateKnownLength() {
242-
WritableBuffer newBuffer = bufferAllocator.allocateKnownLength(knownLengthPendingAllocation);
242+
WritableBuffer newBuffer = bufferAllocator.allocate(knownLengthPendingAllocation);
243243
knownLengthPendingAllocation -= Math.min(knownLengthPendingAllocation,
244244
newBuffer.writableBytes());
245245
return newBuffer;
@@ -259,7 +259,7 @@ private void writeBufferChain(BufferChainOutputStream bufferChain, boolean compr
259259
}
260260
headerScratch.clear();
261261
headerScratch.put(compressed ? COMPRESSED : UNCOMPRESSED).putInt(messageLength);
262-
WritableBuffer writeableHeader = bufferAllocator.allocateKnownLength(HEADER_LENGTH);
262+
WritableBuffer writeableHeader = bufferAllocator.allocate(HEADER_LENGTH);
263263
writeableHeader.write(headerScratch.array(), 0, headerScratch.position());
264264
if (messageLength == 0) {
265265
// the payload had 0 length so make the header the current buffer.

core/src/main/java/io/grpc/internal/WritableBufferAllocator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,4 @@ public interface WritableBufferAllocator {
2727
* free to return a buffer with a greater or lesser capacity.
2828
*/
2929
WritableBuffer allocate(int capacityHint);
30-
31-
/**
32-
* Request a new {@link WritableBuffer} with the given {@code capacityHint}. This method is
33-
* similar to {@link #allocate(int)}, but there is no need to allocate greater capacity.
34-
*/
35-
default WritableBuffer allocateKnownLength(int capacityHint) {
36-
return allocate(capacityHint);
37-
}
3830
}

netty/src/main/java/io/grpc/netty/NettyWritableBufferAllocator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
*/
3434
class NettyWritableBufferAllocator implements WritableBufferAllocator {
3535

36-
// Use 4k as our minimum buffer size.
37-
private static final int MIN_BUFFER = 4 * 1024;
38-
3936
// Set the maximum buffer size to 1MB.
4037
private static final int MAX_BUFFER = 1024 * 1024;
4138

@@ -47,11 +44,6 @@ class NettyWritableBufferAllocator implements WritableBufferAllocator {
4744

4845
@Override
4946
public WritableBuffer allocate(int capacityHint) {
50-
return allocateKnownLength(Math.max(MIN_BUFFER, capacityHint));
51-
}
52-
53-
@Override
54-
public WritableBuffer allocateKnownLength(int capacityHint) {
5547
capacityHint = Math.min(MAX_BUFFER, capacityHint);
5648
return new NettyWritableBuffer(allocator.buffer(capacityHint, capacityHint));
5749
}

okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBufferAllocator.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator {
4444
@Override
4545
public WritableBuffer allocate(int capacityHint) {
4646
// okio buffer uses fixed size Segments, round capacityHint up
47-
return allocateKnownLength((capacityHint + Segment.SIZE - 1) / Segment.SIZE * Segment.SIZE);
48-
}
49-
50-
@Override
51-
public WritableBuffer allocateKnownLength(int capacityHint) {
52-
capacityHint = Math.min(MAX_BUFFER, capacityHint);
53-
return new OkHttpWritableBuffer(new Buffer(), capacityHint);
47+
return new OkHttpWritableBuffer(new Buffer(),
48+
Math.min(MAX_BUFFER, (capacityHint + Segment.SIZE - 1) / Segment.SIZE * Segment.SIZE));
5449
}
5550
}

servlet/src/main/java/io/grpc/servlet/ServletServerStream.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import io.grpc.internal.TransportFrameUtil;
3939
import io.grpc.internal.TransportTracer;
4040
import io.grpc.internal.WritableBuffer;
41-
import io.grpc.internal.WritableBufferAllocator;
4241
import java.io.IOException;
4342
import java.nio.charset.StandardCharsets;
4443
import java.util.Collections;
@@ -73,7 +72,7 @@ final class ServletServerStream extends AbstractServerStream {
7372
Attributes attributes,
7473
String authority,
7574
InternalLogId logId) throws IOException {
76-
super(ALLOCATOR, statsTraceCtx);
75+
super(ByteArrayWritableBuffer::new, statsTraceCtx);
7776
transportState =
7877
new ServletTransportState(maxInboundMessageSize, statsTraceCtx, new TransportTracer());
7978
this.attributes = attributes;
@@ -162,20 +161,6 @@ public void deframeFailed(Throwable cause) {
162161
}
163162
}
164163

165-
private static final WritableBufferAllocator ALLOCATOR = new WritableBufferAllocator() {
166-
private static final int MIN_BUFFER = 4096;
167-
168-
@Override
169-
public WritableBuffer allocate(int capacityHint) {
170-
return new ByteArrayWritableBuffer(max(MIN_BUFFER, capacityHint));
171-
}
172-
173-
@Override
174-
public WritableBuffer allocateKnownLength(int capacityHint) {
175-
return new ByteArrayWritableBuffer(capacityHint);
176-
}
177-
};
178-
179164
private static final class ByteArrayWritableBuffer implements WritableBuffer {
180165

181166
private final int capacity;

0 commit comments

Comments
 (0)