protobuf: make buffer cache even weaker

This commit is contained in:
Carl Mastrangelo 2016-09-21 10:25:50 -07:00
parent ef4e0f4522
commit 017f5f8808
1 changed files with 9 additions and 11 deletions

View File

@ -49,6 +49,8 @@ import io.grpc.internal.GrpcUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
/** /**
* Utility methods for using protobuf with grpc. * Utility methods for using protobuf with grpc.
@ -78,15 +80,11 @@ public class ProtoLiteUtils {
globalRegistry = checkNotNull(newRegistry, "newRegistry"); globalRegistry = checkNotNull(newRegistry, "newRegistry");
} }
/** private static final ThreadLocal<Reference<byte[]>> bufs = new ThreadLocal<Reference<byte[]>>() {
* 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 @Override
protected byte[] initialValue() { protected Reference<byte[]> initialValue() {
return new byte[4096]; // Picked at random. return new WeakReference<byte[]>(new byte[4096]); // Picked at random.
} }
}; };
@ -141,11 +139,11 @@ public class ProtoLiteUtils {
if (stream instanceof KnownLength) { if (stream instanceof KnownLength) {
int size = stream.available(); int size = stream.available();
if (size > 0 && size <= GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE) { if (size > 0 && size <= GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE) {
// Coded Input stream does not escape, so buf does not escape. // buf should not be used after this method has returned.
byte[] buf = bufs.get(); byte[] buf = bufs.get().get();
if (buf.length < size) { if (buf == null || buf.length < size) {
buf = new byte[size]; buf = new byte[size];
bufs.set(buf); bufs.set(new WeakReference<byte[]>(buf));
} }
int chunkSize; int chunkSize;
int position = 0; int position = 0;