diff --git a/lib/go/thrift/binary_protocol.go b/lib/go/thrift/binary_protocol.go index eded931346e..e8156ea0176 100644 --- a/lib/go/thrift/binary_protocol.go +++ b/lib/go/thrift/binary_protocol.go @@ -555,6 +555,15 @@ func safeReadBytes(size int32, trans io.Reader) ([]byte, error) { return nil, nil } + // Fast path for reads smaller than 10 MiB that only allocates exactly + // what is asked for. + const readLimit = 10 * 1024 * 1024 + if size < readLimit { + b := make([]byte, size) + n, err := io.ReadFull(trans, b) + return b[:n], err + } + buf := new(bytes.Buffer) _, err := io.CopyN(buf, trans, int64(size)) return buf.Bytes(), err