Skip to content

Commit ef4e0f4

Browse files
protobuf: cache temp buffers
Before: TransportBenchmark.unaryCall1024 true NETTY sample 4564 2188854.745 ± 71456.423 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.00 true NETTY sample 1875968.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.50 true NETTY sample 2105344.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.90 true NETTY sample 2396160.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.95 true NETTY sample 2535424.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.99 true NETTY sample 3011993.600 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.999 true NETTY sample 7471595.520 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.9999 true NETTY sample 99090432.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p1.00 true NETTY sample 99090432.000 ns/op TransportBenchmark.unaryCall1024:·gc.alloc.rate true NETTY sample 10 2787.784 ± 169.945 MB/sec TransportBenchmark.unaryCall1024:·gc.alloc.rate.norm true NETTY sample 10 6415272.837 ± 262.046 B/op TransportBenchmark.unaryCall1024:·gc.churn.PS_Eden_Space true NETTY sample 10 2815.863 ± 429.465 MB/sec TransportBenchmark.unaryCall1024:·gc.churn.PS_Eden_Space.norm true NETTY sample 10 6483440.294 ± 947355.959 B/op TransportBenchmark.unaryCall1024:·gc.churn.PS_Survivor_Space true NETTY sample 10 2.143 ± 1.623 MB/sec TransportBenchmark.unaryCall1024:·gc.churn.PS_Survivor_Space.norm true NETTY sample 10 4873.798 ± 3679.598 B/op TransportBenchmark.unaryCall1024:·gc.count true NETTY sample 10 42.000 counts TransportBenchmark.unaryCall1024:·gc.time true NETTY sample 10 155.000 ms After: Benchmark (direct) (transport) Mode Cnt Score Error Units TransportBenchmark.unaryCall1024 true NETTY sample 5037 1982881.569 ± 16738.841 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.00 true NETTY sample 1683456.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.50 true NETTY sample 1918976.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.90 true NETTY sample 2232320.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.95 true NETTY sample 2330624.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.99 true NETTY sample 2729574.400 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.999 true NETTY sample 6127304.704 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p0.9999 true NETTY sample 15515648.000 ns/op TransportBenchmark.unaryCall1024:unaryCall1024·p1.00 true NETTY sample 15515648.000 ns/op TransportBenchmark.unaryCall1024:·gc.alloc.rate true NETTY sample 10 2071.435 ± 141.669 MB/sec TransportBenchmark.unaryCall1024:·gc.alloc.rate.norm true NETTY sample 10 4318096.849 ± 269.655 B/op TransportBenchmark.unaryCall1024:·gc.churn.PS_Eden_Space true NETTY sample 10 2076.282 ± 323.504 MB/sec TransportBenchmark.unaryCall1024:·gc.churn.PS_Eden_Space.norm true NETTY sample 10 4335884.918 ± 729189.378 B/op TransportBenchmark.unaryCall1024:·gc.churn.PS_Survivor_Space true NETTY sample 10 1.567 ± 1.238 MB/sec TransportBenchmark.unaryCall1024:·gc.churn.PS_Survivor_Space.norm true NETTY sample 10 3274.883 ± 2640.345 B/op TransportBenchmark.unaryCall1024:·gc.count true NETTY sample 10 31.000 counts TransportBenchmark.unaryCall1024:·gc.time true NETTY sample 10 51.000 ms
1 parent d74091f commit ef4e0f4

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ public static void setExtensionRegistry(ExtensionRegistryLite newRegistry) {
7878
globalRegistry = checkNotNull(newRegistry, "newRegistry");
7979
}
8080

81+
/**
82+
* Local cache of buffers to use for parsing. ThreadLocal used a WeakReference internally, so
83+
* these will not be retained.
84+
*/
85+
private static final ThreadLocal<byte[]> bufs = new ThreadLocal<byte[]>() {
86+
87+
@Override
88+
protected byte[] initialValue() {
89+
return new byte[4096]; // Picked at random.
90+
}
91+
};
92+
8193
/** Create a {@code Marshaller} for protos of the same type as {@code defaultInstance}. */
8294
public static <T extends MessageLite> Marshaller<T> marshaller(final T defaultInstance) {
8395
@SuppressWarnings("unchecked")
@@ -129,16 +141,21 @@ public T parse(InputStream stream) {
129141
if (stream instanceof KnownLength) {
130142
int size = stream.available();
131143
if (size > 0 && size <= GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE) {
132-
byte[] buf = new byte[size];
144+
// Coded Input stream does not escape, so buf does not escape.
145+
byte[] buf = bufs.get();
146+
if (buf.length < size) {
147+
buf = new byte[size];
148+
bufs.set(buf);
149+
}
133150
int chunkSize;
134151
int position = 0;
135-
while ((chunkSize = stream.read(buf, position, buf.length - position)) != -1) {
152+
while ((chunkSize = stream.read(buf, position, size - position)) != -1) {
136153
position += chunkSize;
137154
}
138-
if (buf.length != position) {
139-
throw new RuntimeException("size inaccurate: " + buf.length + " != " + position);
155+
if (size != position) {
156+
throw new RuntimeException("size inaccurate: " + size + " != " + position);
140157
}
141-
cis = CodedInputStream.newInstance(buf);
158+
cis = CodedInputStream.newInstance(buf, 0, size);
142159
} else if (size == 0) {
143160
return defaultInstance;
144161
}

0 commit comments

Comments
 (0)