aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cpp
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-09-24 17:02:22 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-09-24 17:02:22 -0700
commitcc5dc5bbafd4ef3f54b655bf98afcc3b71435ef2 (patch)
treeb81568b2b546ace7240ba60eb60fa329827a1730 /src/cpp
parentfc193e1ff40d1ae557caf84d644a1b3a9bfe95b3 (diff)
Fast path for proto serialization for small protos
Diffstat (limited to 'src/cpp')
-rw-r--r--src/cpp/proto/proto_utils.cc17
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,