Skip to content

Commit 9a0efa0

Browse files
committed
address michajlo feedback
1 parent 9843a5e commit 9a0efa0

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import com.google.devtools.build.lib.packages.Target;
1818
import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback;
1919
import com.google.devtools.build.lib.query2.proto.proto2api.Build;
20-
import java.io.ByteArrayOutputStream;
20+
import com.google.protobuf.CodedOutputStream;
21+
2122
import java.io.IOException;
2223
import java.io.OutputStream;
2324
import java.util.stream.StreamSupport;
@@ -29,18 +30,6 @@
2930
*/
3031
public class StreamedProtoOutputFormatter extends ProtoOutputFormatter {
3132

32-
/**
33-
* The most bytes that protobuf delimited proto format will prepend to a proto message. See <a
34-
* href="https://github.com/protocolbuffers/protobuf/blob/c11033dc27c3e9c1913e45b62fb5d4c5b5644b3e/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java#L72">
35-
* <code>writeDelimitedTo</code></a> and <a
36-
* href="https://github.com/protocolbuffers/protobuf/blob/c11033dc27c3e9c1913e45b62fb5d4c5b5644b3e/java/core/src/main/java/com/google/protobuf/WireFormat.java#L28">
37-
* <code>MAX_VARINT32_SIZE</code></a>.
38-
*
39-
* <p>The value for int32 (used by {@code writeDelimitedTo} is actually 5, but we pick 10 just to
40-
* be safe.
41-
*/
42-
private static final int MAX_BYTES_FOR_VARINT32_ENCODING = 10;
43-
4433
@Override
4534
public String getName() {
4635
return "streamed_proto";
@@ -79,23 +68,23 @@ private Build.Target toProto(Target target) {
7968
}
8069
}
8170

82-
private synchronized void writeToOutputStreamThreadSafe(ByteArrayOutputStream bout) {
71+
private synchronized void writeToOutputStreamThreadSafe(byte[] data) {
8372
try {
84-
bout.writeTo(out);
73+
out.write(data);
8574
} catch (IOException e) {
8675
throw new WrappedIOException(e);
8776
}
8877
}
8978
};
9079
}
9180

92-
private static ByteArrayOutputStream writeDelimited(Build.Target targetProtoBuffer) {
81+
private static byte[] writeDelimited(Build.Target targetProtoBuffer) {
9382
try {
94-
var bout =
95-
new ByteArrayOutputStream(
96-
targetProtoBuffer.getSerializedSize() + MAX_BYTES_FOR_VARINT32_ENCODING);
97-
targetProtoBuffer.writeDelimitedTo(bout);
98-
return bout;
83+
var serializedSize = targetProtoBuffer.getSerializedSize();
84+
var headerSize = CodedOutputStream.computeUInt32SizeNoTag(serializedSize);
85+
var output = new byte[headerSize + serializedSize];
86+
targetProtoBuffer.writeTo(CodedOutputStream.newInstance(output, headerSize, output.length - headerSize));
87+
return output;
9988
} catch (IOException e) {
10089
throw new WrappedIOException(e);
10190
}

0 commit comments

Comments
 (0)