Skip to content

Commit

Permalink
Merge pull request #23 from vimeo/grpc_no_bytes_copy
Browse files Browse the repository at this point in the history
gRPC: don't copy bytes before sending on the wire.
  • Loading branch information
dfinkel authored Mar 23, 2021
2 parents c0b1af7 + 2f6eba5 commit b7e5d71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion grpc/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (gp *serviceImpl) GetFromPeer(ctx context.Context, req *pb.GetRequest) (*pb

galaxy.Stats.ServerRequests.Add(1) // keep track of the num of req

var value gc.ByteCodec
var value unsafeByteCodec
err := galaxy.Get(ctx, req.Key, &value)
if err != nil {
return nil, status.Errorf(status.Code(err), "Failed to retrieve [%s]: %v", req, err)
Expand Down
17 changes: 17 additions & 0 deletions grpc/unsafe_byte_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package grpc

// unsafeByteCodec is a byte slice type that implements Codec
type unsafeByteCodec []byte

// MarshalBinary returns the contained byte-slice
func (c *unsafeByteCodec) MarshalBinary() ([]byte, error) {
return *c, nil
}

// UnmarshalBinary to provided data so they share the same backing array
// this is a generally unsafe performance optimization, but safe in the context
// of the gRPC server.
func (c *unsafeByteCodec) UnmarshalBinary(data []byte) error {
*c = data
return nil
}

0 comments on commit b7e5d71

Please sign in to comment.