From 164d4c63c83aaee5d501fb58a803758f127ab722 Mon Sep 17 00:00:00 2001 From: Chris Kennelly Date: Fri, 26 Jul 2024 14:46:41 -0700 Subject: [PATCH] Avoid redundant ByteSizeLong() call. Given we have called ByteSizeLog() at line 52, we can use WithCachedSizes here. PiperOrigin-RevId: 656534835 --- include/grpcpp/impl/codegen/config_protobuf.h | 2 ++ include/grpcpp/impl/proto_utils.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/grpcpp/impl/codegen/config_protobuf.h b/include/grpcpp/impl/codegen/config_protobuf.h index 06561cf121047..b4095ff08f599 100644 --- a/include/grpcpp/impl/codegen/config_protobuf.h +++ b/include/grpcpp/impl/codegen/config_protobuf.h @@ -71,6 +71,7 @@ #define GRPC_CUSTOM_ZEROCOPYINPUTSTREAM \ ::google::protobuf::io::ZeroCopyInputStream #define GRPC_CUSTOM_CODEDINPUTSTREAM ::google::protobuf::io::CodedInputStream +#define GRPC_CUSTOM_CODEDOUTPUTSTREAM ::google::protobuf::io::CodedOutputStream #endif #ifndef GRPC_CUSTOM_JSONUTIL @@ -113,6 +114,7 @@ namespace io { typedef GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream; typedef GRPC_CUSTOM_ZEROCOPYINPUTSTREAM ZeroCopyInputStream; typedef GRPC_CUSTOM_CODEDINPUTSTREAM CodedInputStream; +typedef GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream; } // namespace io } // namespace protobuf diff --git a/include/grpcpp/impl/proto_utils.h b/include/grpcpp/impl/proto_utils.h index 20d1435842621..f2cd8d21b76c4 100644 --- a/include/grpcpp/impl/proto_utils.h +++ b/include/grpcpp/impl/proto_utils.h @@ -61,7 +61,9 @@ Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, return grpc::Status::OK; } ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size); - return msg.SerializeToZeroCopyStream(&writer) + protobuf::io::CodedOutputStream cs(&writer); + msg.SerializeWithCachedSizes(&cs); + return !cs.HadError() ? grpc::Status::OK : Status(StatusCode::INTERNAL, "Failed to serialize message"); }