From e40307c567557e9a531ead1fe96525b2e3e1f369 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 4 Jul 2019 14:06:29 +0300 Subject: [PATCH] codec grpc: fix extra allocations on message unmarshal Signed-off-by: Vasiliy Tolstov --- client/grpc/codec.go | 6 ++++-- server/grpc/codec.go | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/client/grpc/codec.go b/client/grpc/codec.go index fe59c361..f223a55c 100644 --- a/client/grpc/codec.go +++ b/client/grpc/codec.go @@ -4,9 +4,11 @@ import ( "fmt" "strings" + b "bytes" + "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" - "github.com/json-iterator/go" + jsoniter "github.com/json-iterator/go" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/bytes" "github.com/micro/go-micro/codec/jsonrpc" @@ -116,7 +118,7 @@ func (jsonCodec) Marshal(v interface{}) ([]byte, error) { func (jsonCodec) Unmarshal(data []byte, v interface{}) error { if pb, ok := v.(proto.Message); ok { - return jsonpb.UnmarshalString(string(data), pb) + return jsonpb.Unmarshal(b.NewReader(data), pb) } return json.Unmarshal(data, v) diff --git a/server/grpc/codec.go b/server/grpc/codec.go index 7562a044..7362f49c 100644 --- a/server/grpc/codec.go +++ b/server/grpc/codec.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" + b "bytes" + "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" "github.com/micro/go-micro/codec" @@ -81,7 +83,7 @@ func (jsonCodec) Marshal(v interface{}) ([]byte, error) { func (jsonCodec) Unmarshal(data []byte, v interface{}) error { if pb, ok := v.(proto.Message); ok { - return jsonpb.UnmarshalString(string(data), pb) + return jsonpb.Unmarshal(b.NewReader(data), pb) } return json.Unmarshal(data, v)