-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(rpc): use gogoproto codec (#236)
### What this PR does Official grpc-go has not been compatible with gogoproto since grpc/grpc-go#3958. To use gogoproto's marshaler and unmarshaler, a custom codec should be registered. A simple benchmark show it worth: ``` name old time/op new time/op delta StreamRPC-16 7.49µs ± 3% 6.98µs ± 5% -6.85% (p=0.000 n=20+20) name old alloc/op new alloc/op delta StreamRPC-16 16.4kB ± 0% 16.4kB ± 0% -0.24% (p=0.000 n=20+20) name old allocs/op new allocs/op delta StreamRPC-16 51.0 ± 0% 49.0 ± 0% -3.92% (p=0.000 n=19+17) ``` ### Which issue(s) this PR resolves Resolves #235 ### Anything else Links: - grpc/grpc-go#3958 - grpc/grpc-go#4466
- Loading branch information
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package rpc | ||
|
||
import ( | ||
gogoproto "github.com/gogo/protobuf/proto" | ||
"github.com/golang/protobuf/proto" //nolint:staticcheck | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
const name = "proto" | ||
|
||
type codec struct{} | ||
|
||
var _ encoding.Codec = codec{} | ||
|
||
func init() { | ||
encoding.RegisterCodec(codec{}) | ||
} | ||
|
||
func (codec) Marshal(v interface{}) ([]byte, error) { | ||
if m, ok := v.(gogoproto.Marshaler); ok { | ||
return m.Marshal() | ||
} | ||
return proto.Marshal(v.(proto.Message)) | ||
} | ||
|
||
func (codec) Unmarshal(data []byte, v interface{}) error { | ||
if m, ok := v.(gogoproto.Unmarshaler); ok { | ||
return m.Unmarshal(data) | ||
} | ||
return proto.Unmarshal(data, v.(proto.Message)) | ||
} | ||
|
||
func (codec) Name() string { | ||
return name | ||
} |