From f7a5769187a11d4d15ed685abf55338eaeafdc12 Mon Sep 17 00:00:00 2001 From: Michajlo Matijkiw Date: Wed, 2 Nov 2016 17:45:31 +0000 Subject: Store and use commandId and cookie as ByteStrings We wind up doing String -> UTF8 bytes conversion for every message serialized (this happens in protocol buffer land). Do the conversion once and reuse the immutable value instead of doing it for every chunk of output written. Keep this optimization local to RpcOutputStream where we see a lot of repitition - using ByteStrings in place of Strings can get confusing when it comes to logging, so only apply this optimization where it could count. -- MOS_MIGRATED_REVID=137964305 --- .../devtools/build/lib/server/GrpcServerImpl.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java index 03af9d78a6..0c8791990e 100644 --- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java +++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java @@ -384,14 +384,17 @@ public class GrpcServerImpl implements RPCServer { static class RpcOutputStream extends OutputStream { private static final int CHUNK_SIZE = 8192; - private final String commandId; - private final String responseCookie; + // Store commandId and responseCookie as ByteStrings to avoid String -> UTF8 bytes conversion + // for each serialized chunk of output. + private final ByteString commandIdBytes; + private final ByteString responseCookieBytes; + private final StreamType type; private final GrpcSink sink; RpcOutputStream(String commandId, String responseCookie, StreamType type, GrpcSink sink) { - this.commandId = commandId; - this.responseCookie = responseCookie; + this.commandIdBytes = ByteString.copyFromUtf8(commandId); + this.responseCookieBytes = ByteString.copyFromUtf8(responseCookie); this.type = type; this.sink = sink; } @@ -402,8 +405,8 @@ public class GrpcServerImpl implements RPCServer { ByteString input = ByteString.copyFrom(b, off + i, Math.min(CHUNK_SIZE, inlen - i)); RunResponse.Builder response = RunResponse .newBuilder() - .setCookie(responseCookie) - .setCommandId(commandId); + .setCookieBytes(responseCookieBytes) + .setCommandIdBytes(commandIdBytes); switch (type) { case STDOUT: response.setStandardOutput(input); break; @@ -421,7 +424,7 @@ public class GrpcServerImpl implements RPCServer { log.info( String.format( "Client disconnected received for command %s on thread %s", - commandId, Thread.currentThread().getName())); + commandIdBytes.toStringUtf8(), Thread.currentThread().getName())); throw new IOException("Client disconnected"); } } -- cgit v1.2.3