Skip to content

Commit

Permalink
codec grpc: fix extra allocations on message unmarshal
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Jul 4, 2019
1 parent a412486 commit e40307c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions client/grpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion server/grpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e40307c

Please sign in to comment.