New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/proto: simplify & optimize proto codec #3958
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,6 @@ | |
package proto | ||
|
||
import ( | ||
"math" | ||
"sync" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
@@ -38,73 +35,14 @@ func init() { | |
// codec is a Codec implementation with protobuf. It is the default codec for gRPC. | ||
type codec struct{} | ||
|
||
type cachedProtoBuffer struct { | ||
lastMarshaledSize uint32 | ||
proto.Buffer | ||
} | ||
|
||
func capToMaxInt32(val int) uint32 { | ||
if val > math.MaxInt32 { | ||
return uint32(math.MaxInt32) | ||
} | ||
return uint32(val) | ||
} | ||
|
||
func marshal(v interface{}, cb *cachedProtoBuffer) ([]byte, error) { | ||
protoMsg := v.(proto.Message) | ||
newSlice := make([]byte, 0, cb.lastMarshaledSize) | ||
|
||
cb.SetBuf(newSlice) | ||
cb.Reset() | ||
if err := cb.Marshal(protoMsg); err != nil { | ||
return nil, err | ||
} | ||
out := cb.Bytes() | ||
cb.lastMarshaledSize = capToMaxInt32(len(out)) | ||
return out, nil | ||
} | ||
|
||
func (codec) Marshal(v interface{}) ([]byte, error) { | ||
if pm, ok := v.(proto.Marshaler); ok { | ||
// object can marshal itself, no need for buffer | ||
return pm.Marshal() | ||
} | ||
|
||
cb := protoBufferPool.Get().(*cachedProtoBuffer) | ||
out, err := marshal(v, cb) | ||
|
||
// put back buffer and lose the ref to the slice | ||
cb.SetBuf(nil) | ||
protoBufferPool.Put(cb) | ||
return out, err | ||
return proto.Marshal(v.(proto.Message)) | ||
} | ||
|
||
func (codec) Unmarshal(data []byte, v interface{}) error { | ||
protoMsg := v.(proto.Message) | ||
protoMsg.Reset() | ||
|
||
if pu, ok := protoMsg.(proto.Unmarshaler); ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same problem, removing this will break use case of |
||
// object can unmarshal itself, no need for buffer | ||
return pu.Unmarshal(data) | ||
} | ||
|
||
cb := protoBufferPool.Get().(*cachedProtoBuffer) | ||
cb.SetBuf(data) | ||
err := cb.Unmarshal(protoMsg) | ||
cb.SetBuf(nil) | ||
protoBufferPool.Put(cb) | ||
return err | ||
return proto.Unmarshal(data, v.(proto.Message)) | ||
} | ||
|
||
func (codec) Name() string { | ||
return Name | ||
} | ||
|
||
var protoBufferPool = &sync.Pool{ | ||
New: func() interface{} { | ||
return &cachedProtoBuffer{ | ||
Buffer: proto.Buffer{}, | ||
lastMarshaledSize: 16, | ||
} | ||
}, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking existing code generate with
gogoproto.customtype = "MyType"
, where a user can write custom marshaling/unmarshaling code to have more control. IMHO should not be removed.https://github.com/gogo/protobuf/blob/master/custom_types.md#custom-type-method-signatures
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gogoproto
is not officially supported by gRPC-Go.If you want a codec that suits your needs better, it's easy to install your own. Just copy/paste this code into another package, update the parts that are important, and
import
it in yourmain
package after anygrpc
imports. (If you don't controlmain
, you can give it a new name and use theCodec
Call
-/Dial
-/Server
-Option
s.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok thanks, I will give it a try. I have also created an issue about this. Please feel free to close it #4192 I will also comment the solution there after testing it.