Skip to content

Commit

Permalink
perf: Merge some if-conditions and remove unnecessary assignments in …
Browse files Browse the repository at this point in the history
…GrpcContextImplementation::do_one related to multithreaded GrpcContext
  • Loading branch information
Tradias committed Sep 13, 2024
1 parent 633ed51 commit 7df30c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 60 deletions.
20 changes: 4 additions & 16 deletions src/agrpc/detail/grpc_context_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ AGRPC_NAMESPACE_BEGIN()
namespace detail
{
template <class Function>
struct GrpcContextLoopFunction
struct GrpcContextLoopFunction : Function
{
Function function_;

template <class Context>
auto operator()(Context& context) const
{
return function_(context);
}
using Function::operator();

[[nodiscard]] bool has_processed(detail::DoOneResult result) const noexcept { return bool{result}; }
};
Expand All @@ -49,15 +43,9 @@ template <class Function>
GrpcContextLoopFunction(Function) -> GrpcContextLoopFunction<Function>;

template <class Function>
struct GrpcContextCompletionQueueLoopFunction
struct GrpcContextCompletionQueueLoopFunction : Function
{
Function function_;

template <class Context>
auto operator()(Context& context) const
{
return function_(context);
}
using Function::operator();

[[nodiscard]] bool has_processed(detail::DoOneResult result) const noexcept
{
Expand Down
65 changes: 21 additions & 44 deletions src/agrpc/detail/grpc_context_implementation_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,10 @@ inline bool GrpcContextImplementation::move_remote_work_to_local_queue(
inline bool GrpcContextImplementation::distribute_all_local_work_to_other_threads_but_one(
detail::GrpcContextThreadContext& context) noexcept
{
auto& local_work_queue = context.local_work_queue_;
if (!local_work_queue.empty())
if (auto& local_work_queue = context.local_work_queue_; !local_work_queue.empty())
{
const auto first = local_work_queue.pop_front();
bool needs_trigger = !local_work_queue.empty();
if (GrpcContextImplementation::move_local_queue_to_remote_work(context))
{
needs_trigger = false;
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
}
const bool needs_trigger = GrpcContextImplementation::move_local_queue_to_remote_work(context);
local_work_queue.push_back(first);
return needs_trigger;
}
Expand Down Expand Up @@ -215,53 +209,36 @@ inline DoOneResult GrpcContextImplementation::do_one(detail::GrpcContextThreadCo
::gpr_timespec deadline, detail::InvokeHandler invoke)
{
const agrpc::GrpcContext& grpc_context = context.grpc_context_;
auto& local_work_queue = context.local_work_queue_;
const auto& local_work_queue = context.local_work_queue_;
bool check_remote_work = context.check_remote_work_;
if (check_remote_work)
if constexpr (IsMultithreaded)
{
if constexpr (IsMultithreaded)
if (local_work_queue.empty() && check_remote_work)
{
if (local_work_queue.empty())
{
check_remote_work = GrpcContextImplementation::move_remote_work_to_local_queue(context);
if (check_remote_work)
{
check_remote_work = GrpcContextImplementation::move_remote_work_to_local_queue(context);
}
if (check_remote_work)
{
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
check_remote_work = false;
}
}
else if (local_work_queue.has_exactly_one_element())
{
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
check_remote_work = false;
}
check_remote_work = GrpcContextImplementation::move_remote_work_to_local_queue(context);
}
else
if (GrpcContextImplementation::distribute_all_local_work_to_other_threads_but_one(context) || check_remote_work)
{
check_remote_work = GrpcContextImplementation::move_remote_work_to_local_queue(context);
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
}
context.check_remote_work_ = check_remote_work;
check_remote_work = false;
}
const auto distribute_local_work = [&]
else
{
if constexpr (IsMultithreaded)
if (check_remote_work)
{
if (GrpcContextImplementation::distribute_all_local_work_to_other_threads_but_one(context) &&
check_remote_work)
{
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
check_remote_work = false;
context.check_remote_work_ = check_remote_work;
}
check_remote_work = GrpcContextImplementation::move_remote_work_to_local_queue(context);
}
};
distribute_local_work();
}
context.check_remote_work_ = check_remote_work;
const bool processed_local_work = GrpcContextImplementation::process_local_queue(context, invoke);
distribute_local_work();
if constexpr (IsMultithreaded)
{
if (GrpcContextImplementation::distribute_all_local_work_to_other_threads_but_one(context))
{
GrpcContextImplementation::trigger_work_alarm(context.grpc_context_);
}
}
const bool is_more_completed_work_pending = check_remote_work || !local_work_queue.empty();
if (!is_more_completed_work_pending && grpc_context.is_stopped())
{
Expand Down

0 comments on commit 7df30c3

Please sign in to comment.