Merge pull request #24494 from vjpai/server_code_size

Reduce template use in sync server unary calls
pull/24607/head
Vijay Pai 4 years ago committed by GitHub
commit 3ecb008aa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      include/grpcpp/impl/codegen/byte_buffer.h
  2. 14
      include/grpcpp/impl/codegen/completion_queue.h
  3. 78
      include/grpcpp/impl/codegen/method_handler.h
  4. 14
      include/grpcpp/impl/codegen/server_context.h
  5. 4
      src/compiler/cpp_generator.cc

@ -40,8 +40,9 @@ template <class RequestType, class ResponseType>
class CallbackUnaryHandler;
template <class RequestType, class ResponseType>
class CallbackServerStreamingHandler;
template <class ServiceType, class RequestType, class ResponseType>
class RpcMethodHandler;
template <class RequestType>
void* UnaryDeserializeHelper(grpc_call*, grpc_byte_buffer*, ::grpc::Status*,
RequestType*);
template <class ServiceType, class RequestType, class ResponseType>
class ServerStreamingHandler;
template <::grpc::StatusCode code>
@ -162,8 +163,9 @@ class ByteBuffer final {
template <class R>
friend class internal::CallOpRecvMessage;
friend class internal::CallOpGenericRecvMessage;
template <class ServiceType, class RequestType, class ResponseType>
friend class internal::RpcMethodHandler;
template <class RequestType>
friend void* internal::UnaryDeserializeHelper(grpc_call*, grpc_byte_buffer*,
::grpc::Status*, RequestType*);
template <class ServiceType, class RequestType, class ResponseType>
friend class internal::ServerStreamingHandler;
template <class RequestType, class ResponseType>

@ -38,6 +38,7 @@
#include <grpcpp/impl/codegen/completion_queue_tag.h>
#include <grpcpp/impl/codegen/core_codegen_interface.h>
#include <grpcpp/impl/codegen/grpc_library.h>
#include <grpcpp/impl/codegen/rpc_service_method.h>
#include <grpcpp/impl/codegen/status.h>
#include <grpcpp/impl/codegen/sync.h>
#include <grpcpp/impl/codegen/time.h>
@ -59,7 +60,12 @@ namespace internal {
template <class W, class R>
class ServerReaderWriterBody;
template <class ServiceType, class RequestType, class ResponseType>
template <class ResponseType>
void UnaryRunHandlerHelper(
const ::grpc::internal::MethodHandler::HandlerParameter&, ResponseType*,
::grpc::Status&);
template <class ServiceType, class RequestType, class ResponseType,
class BaseRequestType, class BaseResponseType>
class RpcMethodHandler;
template <class ServiceType, class RequestType, class ResponseType>
class ClientStreamingHandler;
@ -265,8 +271,10 @@ class CompletionQueue : private ::grpc::GrpcLibraryCodegen {
friend class ::grpc::ServerWriter;
template <class W, class R>
friend class ::grpc::internal::ServerReaderWriterBody;
template <class ServiceType, class RequestType, class ResponseType>
friend class ::grpc::internal::RpcMethodHandler;
template <class ResponseType>
friend void ::grpc::internal::UnaryRunHandlerHelper(
const ::grpc::internal::MethodHandler::HandlerParameter&, ResponseType*,
::grpc::Status&);
template <class ServiceType, class RequestType, class ResponseType>
friend class ::grpc::internal::ClientStreamingHandler;
template <class ServiceType, class RequestType, class ResponseType>

@ -49,8 +49,52 @@ template <class Callable>
#endif // GRPC_ALLOW_EXCEPTIONS
}
/// A helper function with reduced templating to do the common work needed to
/// actually send the server response. Uses non-const parameter for Status since
/// this should only ever be called from the end of the RunHandler method.
template <class ResponseType>
void UnaryRunHandlerHelper(const MethodHandler::HandlerParameter& param,
ResponseType* rsp, ::grpc::Status& status) {
GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_);
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata,
::grpc::internal::CallOpSendMessage,
::grpc::internal::CallOpServerSendStatus>
ops;
ops.SendInitialMetadata(&param.server_context->initial_metadata_,
param.server_context->initial_metadata_flags());
if (param.server_context->compression_level_set()) {
ops.set_compression_level(param.server_context->compression_level());
}
if (status.ok()) {
status = ops.SendMessagePtr(rsp);
}
ops.ServerSendStatus(&param.server_context->trailing_metadata_, status);
param.call->PerformOps(&ops);
param.call->cq()->Pluck(&ops);
}
/// A helper function with reduced templating to do deserializing.
template <class RequestType>
void* UnaryDeserializeHelper(grpc_call* call, grpc_byte_buffer* req,
::grpc::Status* status, RequestType* request) {
::grpc::ByteBuffer buf;
buf.set_buffer(req);
*status = ::grpc::SerializationTraits<RequestType>::Deserialize(
&buf, static_cast<RequestType*>(request));
buf.Release();
if (status->ok()) {
return request;
}
request->~RequestType();
return nullptr;
}
/// A wrapper class of an application provided rpc method handler.
template <class ServiceType, class RequestType, class ResponseType>
template <class ServiceType, class RequestType, class ResponseType,
class BaseRequestType = RequestType,
class BaseResponseType = ResponseType>
class RpcMethodHandler : public ::grpc::internal::MethodHandler {
public:
RpcMethodHandler(
@ -71,40 +115,16 @@ class RpcMethodHandler : public ::grpc::internal::MethodHandler {
});
static_cast<RequestType*>(param.request)->~RequestType();
}
GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_);
::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata,
::grpc::internal::CallOpSendMessage,
::grpc::internal::CallOpServerSendStatus>
ops;
ops.SendInitialMetadata(&param.server_context->initial_metadata_,
param.server_context->initial_metadata_flags());
if (param.server_context->compression_level_set()) {
ops.set_compression_level(param.server_context->compression_level());
}
if (status.ok()) {
status = ops.SendMessagePtr(&rsp);
}
ops.ServerSendStatus(&param.server_context->trailing_metadata_, status);
param.call->PerformOps(&ops);
param.call->cq()->Pluck(&ops);
UnaryRunHandlerHelper(param, static_cast<BaseResponseType*>(&rsp), status);
}
void* Deserialize(grpc_call* call, grpc_byte_buffer* req,
::grpc::Status* status, void** /*handler_data*/) final {
::grpc::ByteBuffer buf;
buf.set_buffer(req);
auto* request =
new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc(
call, sizeof(RequestType))) RequestType();
*status =
::grpc::SerializationTraits<RequestType>::Deserialize(&buf, request);
buf.Release();
if (status->ok()) {
return request;
}
request->~RequestType();
return nullptr;
call, sizeof(RequestType))) RequestType;
return UnaryDeserializeHelper(call, req, status,
static_cast<BaseRequestType*>(request));
}
private:

@ -37,6 +37,7 @@
#include <grpcpp/impl/codegen/create_auth_context.h>
#include <grpcpp/impl/codegen/message_allocator.h>
#include <grpcpp/impl/codegen/metadata_map.h>
#include <grpcpp/impl/codegen/rpc_service_method.h>
#include <grpcpp/impl/codegen/security/auth_context.h>
#include <grpcpp/impl/codegen/server_callback.h>
#include <grpcpp/impl/codegen/server_interceptor.h>
@ -75,7 +76,11 @@ template <class RequestType, class ResponseType>
class CallbackBidiHandler;
template <class ServiceType, class RequestType, class ResponseType>
class ClientStreamingHandler;
template <class ServiceType, class RequestType, class ResponseType>
template <class ResponseType>
void UnaryRunHandlerHelper(const MethodHandler::HandlerParameter&,
ResponseType*, Status&);
template <class ServiceType, class RequestType, class ResponseType,
class BaseRequestType, class BaseResponseType>
class RpcMethodHandler;
template <class Base>
class FinishOnlyReactor;
@ -355,7 +360,12 @@ class ServerContextBase {
friend class ::grpc::ServerWriter;
template <class W, class R>
friend class ::grpc::internal::ServerReaderWriterBody;
template <class ServiceType, class RequestType, class ResponseType>
template <class ResponseType>
friend void ::grpc::internal::UnaryRunHandlerHelper(
const internal::MethodHandler::HandlerParameter& param, ResponseType* rsp,
Status& status);
template <class ServiceType, class RequestType, class ResponseType,
class BaseRequestType, class BaseResponseType>
friend class ::grpc::internal::RpcMethodHandler;
template <class ServiceType, class RequestType, class ResponseType>
friend class ::grpc::internal::ClientStreamingHandler;

@ -2216,8 +2216,8 @@ void PrintSourceService(grpc_generator::Printer* printer,
" $prefix$$Service$_method_names[$Idx$],\n"
" ::grpc::internal::RpcMethod::NORMAL_RPC,\n"
" new ::grpc::internal::RpcMethodHandler< $ns$$Service$::Service, "
"$Request$, "
"$Response$>(\n"
"$Request$, $Response$, ::grpc::protobuf::MessageLite, "
"::grpc::protobuf::MessageLite>(\n"
" []($ns$$Service$::Service* service,\n"
" ::grpc::ServerContext* ctx,\n"
" const $Request$* req,\n"

Loading…
Cancel
Save