Skip to content

Commit cb1ba5b

Browse files
core: remove extra allocation in MessageFramer
OutputStreamAdapter is a private class, and is only ever called by two places: ByteStreams.copy, which never calls the single byte method, and DrainTo, which potentially can. There are two classes that implement DrainTo, which is primarily ProtoInputStream. It calls MessageLite.writeTo(OutputStream) or back again to ByteStreams.copy. MessageLite.writeTo in turn wraps the OutputStream in a CodedOutputStream.OutputStreamEncoder, which then never calls the single byte version. Thus, all know implementations never call the single byte override. Additionally, it is well known that the single byte write is slow, and is expected to be wrapped in a BufferedOutputStream if there are many small writes.
1 parent 1d5fd1f commit cb1ba5b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,14 @@ private void verifyNotClosed() {
320320

321321
/** OutputStream whose write()s are passed to the framer. */
322322
private class OutputStreamAdapter extends OutputStream {
323-
private final byte[] singleByte = new byte[1];
324-
323+
/**
324+
* This is slow, don't call it. If you care about write overhead, use a BufferedOutputStream.
325+
* Better yet, you can use your own single byte buffer and call
326+
* {@link #write(byte[], int, int)}.
327+
*/
325328
@Override
326329
public void write(int b) {
327-
singleByte[0] = (byte) b;
330+
byte[] singleByte = new byte[]{(byte)b};
328331
write(singleByte, 0, 1);
329332
}
330333

@@ -344,7 +347,7 @@ private final class BufferChainOutputStream extends OutputStream {
344347

345348
/**
346349
* This is slow, don't call it. If you care about write overhead, use a BufferedOutputStream.
347-
* Better yet, you can use your own single byte buffer and called
350+
* Better yet, you can use your own single byte buffer and call
348351
* {@link #write(byte[], int, int)}.
349352
*/
350353
@Override

0 commit comments

Comments
 (0)