Skip to content

Commit

Permalink
perf: Reduce size of initial request message when no custom factory i…
Browse files Browse the repository at this point in the history
…s provided
  • Loading branch information
Tradias committed Dec 11, 2024
1 parent 00984a1 commit 80582c6
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 148 deletions.
15 changes: 8 additions & 7 deletions src/agrpc/detail/register_callback_rpc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ struct RegisterCallbackRPCHandlerOperation
using typename Base::Service;
using ServerRPCWithRequest = detail::ServerRPCWithRequest<ServerRPC>;
using ServerRPCPtr = agrpc::ServerRPCPtr<ServerRPC>;
using Starter = detail::ServerRPCStarter<>;

struct ServerRPCAllocation
: detail::RequestMessageFactoryServerRPCMixinT<ServerRPCWithRequest, ServerRPC, RPCHandler>
struct ServerRPCAllocation : detail::ServerRPCPtrRequestMessageFactoryT<ServerRPC, RPCHandler>
{
ServerRPCAllocation(const ServerRPCExecutor& executor, RegisterCallbackRPCHandlerOperation& self)
: ServerRPCAllocation::RequestMessageFactoryMixin(self.rpc_handler(), executor), self_(self)
: detail::ServerRPCPtrRequestMessageFactoryT<ServerRPC, RPCHandler>(self.rpc_handler(), executor),
self_(self)
{
}

Expand All @@ -61,8 +62,8 @@ struct RegisterCallbackRPCHandlerOperation
AGRPC_TRY
{
self_.initiate_next();
auto& starter = *static_cast<ServerRPCAllocation*>(ptr_.server_rpc_);
starter.invoke(self_.rpc_handler(), static_cast<ServerRPCPtr&&>(ptr_));
auto& rpc = *static_cast<ServerRPCAllocation*>(ptr_.server_rpc_);
Starter::invoke(self_.rpc_handler(), static_cast<ServerRPCPtr&&>(ptr_), rpc);
}
AGRPC_CATCH(...)
{
Expand Down Expand Up @@ -152,8 +153,8 @@ struct RegisterCallbackRPCHandlerOperation

void perform_request_and_repeat(ServerRPCPtr&& ptr)
{
auto& rpc = *ptr.server_rpc_;
rpc.start(rpc.rpc_, this->service(), StartCallback{*this, static_cast<ServerRPCPtr&&>(ptr)});
auto& rpc = *static_cast<ServerRPCAllocation*>(ptr.server_rpc_);
Starter::start(rpc.rpc_, this->service(), rpc, StartCallback{*this, static_cast<ServerRPCPtr&&>(ptr)});
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/agrpc/detail/register_coroutine_rpc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,19 @@ struct RegisterCoroutineRPCHandlerOperation
template <class... Args>
static Awaitable perform_request_and_repeat(RefCountGuard g, Args... args)
{
using Starter = detail::ServerRPCStarter<Args...>;
auto& self = static_cast<Type&>(g.get().self_);
auto rpc = detail::ServerRPCContextBaseAccess::construct<ServerRPC>(self.get_executor());
detail::RequestMessageFactoryServerRPCStarter<ServerRPC, RPCHandler, Args...> starter{self.rpc_handler()};
if (!co_await starter.start(rpc, self.service(), self.completion_token()))
detail::ServerRPCRequestMessageFactoryT<ServerRPC, RPCHandler> factory{self.rpc_handler()};
if (!co_await Starter::start(rpc, self.service(), factory, self.completion_token()))
{
co_return;
}
self.notify_when_done_work_started();
AGRPC_TRY
{
self.initiate_next();
co_await starter.invoke(self.rpc_handler(), static_cast<Args&&>(args)..., rpc);
co_await Starter::invoke(self.rpc_handler(), static_cast<Args&&>(args)..., rpc, factory);
}
AGRPC_CATCH(...) { self.set_error(std::current_exception()); }
if (!detail::ServerRPCContextBaseAccess::is_finished(rpc))
Expand Down
14 changes: 7 additions & 7 deletions src/agrpc/detail/register_sender_rpc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ struct RPCHandlerOperation
{
using Service = detail::ServerRPCServiceT<ServerRPC>;
using Traits = typename ServerRPC::Traits;
using Starter = detail::RequestMessageFactoryServerRPCStarter<ServerRPC, RPCHandler>;
using RPCHandlerInvokeResult = detail::RPCHandlerInvokeResultT<Starter&, RPCHandler&, ServerRPC&>;
using RequestMessageFactory = detail::ServerRPCRequestMessageFactoryT<ServerRPC, RPCHandler>;
using RPCHandlerInvokeResult = detail::RPCHandlerInvokeResultT<ServerRPC&, RPCHandler&, RequestMessageFactory&>;
using RegisterRPCHandlerSenderOperationBase =
detail::RegisterRPCHandlerSenderOperationBase<ServerRPC, RPCHandler, StopToken>;

Expand Down Expand Up @@ -206,10 +206,10 @@ struct RPCHandlerOperation
#endif
};

using StartOperationState = detail::InplaceWithFunctionWrapper<
exec::connect_result_t<decltype(std::declval<Starter>().start(std::declval<ServerRPC&>(),
std::declval<Service&>(), agrpc::use_sender)),
StartReceiver>>;
using StartOperationState = detail::InplaceWithFunctionWrapper<exec::connect_result_t<
decltype(detail::ServerRPCStarter<>::start(std::declval<ServerRPC&>(), std::declval<Service&>(),
std::declval<RequestMessageFactory&>(), agrpc::use_sender)),
StartReceiver>>;

template <class Action>
struct Receiver
Expand Down Expand Up @@ -337,7 +337,7 @@ struct RPCHandlerOperation

auto& get_allocator() noexcept { return impl2_.second(); }

detail::CompressedPair<RegisterRPCHandlerSenderOperationBase&, Starter> impl1_;
detail::CompressedPair<RegisterRPCHandlerSenderOperationBase&, RequestMessageFactory> impl1_;
ServerRPC rpc_;
detail::CompressedPair<OperationState, Allocator> impl2_;
};
Expand Down
7 changes: 4 additions & 3 deletions src/agrpc/detail/register_yield_rpc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct RegisterYieldRPCHandlerOperation
using typename Base::RefCountGuard;
using typename Base::ServerRPCExecutor;
using typename Base::Service;
using Starter = detail::ServerRPCStarter<>;

template <class Ch>
RegisterYieldRPCHandlerOperation(const ServerRPCExecutor& executor, Service& service, RPCHandler&& rpc_handler,
Expand Down Expand Up @@ -86,16 +87,16 @@ struct RegisterYieldRPCHandlerOperation
void perform_request_and_repeat(const Yield& yield)
{
auto rpc = detail::ServerRPCContextBaseAccess::construct<ServerRPC>(this->get_executor());
detail::RequestMessageFactoryServerRPCStarter<ServerRPC, RPCHandler> starter{this->rpc_handler()};
if (!starter.start(rpc, this->service(), use_yield(yield)))
detail::ServerRPCRequestMessageFactoryT<ServerRPC, RPCHandler> factory{this->rpc_handler()};
if (!Starter::start(rpc, this->service(), factory, use_yield(yield)))
{
return;
}
this->notify_when_done_work_started();
AGRPC_TRY
{
initiate_next();
starter.invoke(this->rpc_handler(), rpc, yield);
Starter::invoke(this->rpc_handler(), rpc, factory, yield);
}
AGRPC_CATCH(...) { this->set_error(std::current_exception()); }
if (!detail::ServerRPCContextBaseAccess::is_finished(rpc))
Expand Down
48 changes: 48 additions & 0 deletions src/agrpc/detail/server_rpc_request_message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AGRPC_DETAIL_SERVER_RPC_REQUEST_MESSAGE_HPP
#define AGRPC_DETAIL_SERVER_RPC_REQUEST_MESSAGE_HPP

#include <agrpc/rpc_type.hpp>

#include <agrpc/detail/config.hpp>

AGRPC_NAMESPACE_BEGIN()

namespace detail
{
template <class Request, bool NeedsRequestPtr>
struct ServerRPCRequestMessage
{
static constexpr bool HAS_REQUEST_PTR = true;

Request* request_;
};

template <class Request>
struct ServerRPCRequestMessage<Request, false>
{
static constexpr bool HAS_REQUEST_PTR = false;
};

constexpr bool has_initial_request(agrpc::ServerRPCType type) noexcept
{
return type == agrpc::ServerRPCType::SERVER_STREAMING || type == agrpc::ServerRPCType::UNARY;
}
}

AGRPC_NAMESPACE_END

#endif // AGRPC_DETAIL_SERVER_RPC_REQUEST_MESSAGE_HPP
Loading

0 comments on commit 80582c6

Please sign in to comment.