aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar yang-g <yangg@google.com>2017-11-09 09:51:29 -0800
committerGravatar yang-g <yangg@google.com>2017-11-09 09:54:54 -0800
commitb90f0e66e503a8fc675c1cdf3a4301b28a27d62e (patch)
treeca069c65d0920245851d9031273b2c31ef55788a
parentc88185900d5deb54fe02c3cdde3fc9359e185287 (diff)
relax for inlined bytes to avoid allocation
-rw-r--r--include/grpc++/impl/codegen/proto_utils.h11
1 files changed, 8 insertions, 3 deletions
diff --git a/include/grpc++/impl/codegen/proto_utils.h b/include/grpc++/impl/codegen/proto_utils.h
index 799d236e0d..b7636034d4 100644
--- a/include/grpc++/impl/codegen/proto_utils.h
+++ b/include/grpc++/impl/codegen/proto_utils.h
@@ -188,9 +188,14 @@ Status GenericSerialize(const grpc::protobuf::Message& msg,
"BufferWriter must be a subclass of io::ZeroCopyOutputStream");
*own_buffer = true;
int byte_size = msg.ByteSize();
- if (byte_size == 0) {
- grpc_slice empty_slice = g_core_codegen_interface->grpc_empty_slice();
- *bp = g_core_codegen_interface->grpc_raw_byte_buffer_create(&empty_slice, 1);
+ if ((size_t)byte_size <= GRPC_SLICE_INLINED_SIZE) {
+ grpc_slice slice = g_core_codegen_interface->grpc_slice_malloc(byte_size);
+ GPR_CODEGEN_ASSERT(
+ GRPC_SLICE_END_PTR(slice) ==
+ msg.SerializeWithCachedSizesToArray(GRPC_SLICE_START_PTR(slice)));
+ *bp = g_core_codegen_interface->grpc_raw_byte_buffer_create(&slice, 1);
+ g_core_codegen_interface->grpc_slice_unref(slice);
+
return g_core_codegen_interface->ok();
}
BufferWriter writer(bp, kGrpcBufferWriterMaxBufferLength, byte_size);