aboutsummaryrefslogtreecommitdiff
path: root/protobuf-lite
diff options
context:
space:
mode:
authorCarl Mastrangelo <notcarl@google.com>2016-09-20 17:36:21 -0700
committerCarl Mastrangelo <notcarl@google.com>2016-09-21 09:24:10 -0700
commitef4e0f4522a0da7c7c232cf6aeb344c86ffc7c76 (patch)
tree343474f72aa44cb2fc15b4a81634ffeb660be7fd /protobuf-lite
parentd74091f567a2ae251524b8a901f6b4f9f57bb4df (diff)
downloadgrpc-grpc-java-ef4e0f4522a0da7c7c232cf6aeb344c86ffc7c76.tar.gz
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
Diffstat (limited to 'protobuf-lite')
-rw-r--r--protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java27
1 files changed, 22 insertions, 5 deletions
diff --git a/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java b/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java
index 98e781885..8cf95115f 100644
--- a/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java
+++ b/protobuf-lite/src/main/java/io/grpc/protobuf/lite/ProtoLiteUtils.java
@@ -78,6 +78,18 @@ public class ProtoLiteUtils {
globalRegistry = checkNotNull(newRegistry, "newRegistry");
}
+ /**
+ * Local cache of buffers to use for parsing. ThreadLocal used a WeakReference internally, so
+ * these will not be retained.
+ */
+ private static final ThreadLocal<byte[]> bufs = new ThreadLocal<byte[]>() {
+
+ @Override
+ protected byte[] initialValue() {
+ return new byte[4096]; // Picked at random.
+ }
+ };
+
/** Create a {@code Marshaller} for protos of the same type as {@code defaultInstance}. */
public static <T extends MessageLite> Marshaller<T> marshaller(final T defaultInstance) {
@SuppressWarnings("unchecked")
@@ -129,16 +141,21 @@ public class ProtoLiteUtils {
if (stream instanceof KnownLength) {
int size = stream.available();
if (size > 0 && size <= GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE) {
- byte[] buf = new byte[size];
+ // Coded Input stream does not escape, so buf does not escape.
+ byte[] buf = bufs.get();
+ if (buf.length < size) {
+ buf = new byte[size];
+ bufs.set(buf);
+ }
int chunkSize;
int position = 0;
- while ((chunkSize = stream.read(buf, position, buf.length - position)) != -1) {
+ while ((chunkSize = stream.read(buf, position, size - position)) != -1) {
position += chunkSize;
}
- if (buf.length != position) {
- throw new RuntimeException("size inaccurate: " + buf.length + " != " + position);
+ if (size != position) {
+ throw new RuntimeException("size inaccurate: " + size + " != " + position);
}
- cis = CodedInputStream.newInstance(buf);
+ cis = CodedInputStream.newInstance(buf, 0, size);
} else if (size == 0) {
return defaultInstance;
}