diff options
author | Yang Gao <yangg@google.com> | 2015-09-25 16:49:28 -0700 |
---|---|---|
committer | Yang Gao <yangg@google.com> | 2015-09-25 16:49:28 -0700 |
commit | 0f8265a622a6b0afbde10dd6e2d4dc39e8016975 (patch) | |
tree | bef2efc848bb1dce813e6996ab40af27bc5496b8 /src | |
parent | 0a775b38c754d8d756838d1c05d6564cc8ba928e (diff) | |
parent | f031de86f2e789b7c773a5596690c94b19a923b7 (diff) |
Merge pull request #3480 from ctiller/no-alloc
Fast path for proto serialization for small protos
Diffstat (limited to 'src')
-rw-r--r-- | src/cpp/proto/proto_utils.cc | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/cpp/proto/proto_utils.cc b/src/cpp/proto/proto_utils.cc index f47acc8f8d..3c0be58919 100644 --- a/src/cpp/proto/proto_utils.cc +++ b/src/cpp/proto/proto_utils.cc @@ -158,10 +158,19 @@ namespace grpc { Status SerializeProto(const grpc::protobuf::Message& msg, grpc_byte_buffer** bp) { - GrpcBufferWriter writer(bp); - return msg.SerializeToZeroCopyStream(&writer) - ? Status::OK - : Status(StatusCode::INTERNAL, "Failed to serialize message"); + int byte_size = msg.ByteSize(); + if (byte_size <= kMaxBufferLength) { + gpr_slice slice = gpr_slice_malloc(byte_size); + GPR_ASSERT(GPR_SLICE_END_PTR(slice) == msg.SerializeWithCachedSizesToArray(GPR_SLICE_START_PTR(slice))); + *bp = grpc_raw_byte_buffer_create(&slice, 1); + gpr_slice_unref(slice); + return Status::OK; + } else { + GrpcBufferWriter writer(bp); + return msg.SerializeToZeroCopyStream(&writer) + ? Status::OK + : Status(StatusCode::INTERNAL, "Failed to serialize message"); + } } Status DeserializeProto(grpc_byte_buffer* buffer, grpc::protobuf::Message* msg, |