Merge pull request #566 from unistack-org/speed
codec grpc: fix extra allocations on message unmarshal
This commit is contained in:
		@@ -4,9 +4,11 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						b "bytes"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/golang/protobuf/jsonpb"
 | 
						"github.com/golang/protobuf/jsonpb"
 | 
				
			||||||
	"github.com/golang/protobuf/proto"
 | 
						"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"
 | 
				
			||||||
	"github.com/micro/go-micro/codec/bytes"
 | 
						"github.com/micro/go-micro/codec/bytes"
 | 
				
			||||||
	"github.com/micro/go-micro/codec/jsonrpc"
 | 
						"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 {
 | 
					func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
 | 
				
			||||||
	if pb, ok := v.(proto.Message); ok {
 | 
						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)
 | 
						return json.Unmarshal(data, v)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						b "bytes"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/golang/protobuf/jsonpb"
 | 
						"github.com/golang/protobuf/jsonpb"
 | 
				
			||||||
	"github.com/golang/protobuf/proto"
 | 
						"github.com/golang/protobuf/proto"
 | 
				
			||||||
	"github.com/micro/go-micro/codec"
 | 
						"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 {
 | 
					func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
 | 
				
			||||||
	if pb, ok := v.(proto.Message); ok {
 | 
						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)
 | 
						return json.Unmarshal(data, v)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user