-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpcdaemon: extract awaitable async task run by executor (#1953)
- Loading branch information
Showing
12 changed files
with
707 additions
and
564 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright 2024 The Silkworm Authors | ||
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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <exception> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include <silkworm/infra/concurrency/task.hpp> | ||
|
||
#include <boost/asio/compose.hpp> | ||
#include <boost/asio/post.hpp> | ||
#include <boost/asio/this_coro.hpp> | ||
#include <boost/asio/use_awaitable.hpp> | ||
|
||
namespace silkworm::rpc { | ||
|
||
//! Helper trait for any completion handler signature | ||
template <typename R, typename F, typename... Args> | ||
struct CompletionHandler { | ||
using type = void(std::exception_ptr, R); | ||
}; | ||
|
||
//! Partial specialization for \code void return type | ||
template <typename F, typename... Args> | ||
struct CompletionHandler<void, F, Args...> { | ||
using type = void(std::exception_ptr); | ||
}; | ||
|
||
//! Alias helper trait for the completion handler signature of any task | ||
template <typename F, typename... Args> | ||
using TaskCompletionHandler = typename CompletionHandler<std::invoke_result_t<F, Args...>, F, Args...>::type; | ||
|
||
//! Asynchronous \code co_await-able task executing function \code fn with arguments \code args in \code runner executor | ||
template <typename Executor, typename F, typename... Args> | ||
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) because of https://github.com/llvm/llvm-project/issues/68105 | ||
Task<std::invoke_result_t<F, Args...>> async_task(Executor runner, F&& fn, Args&&... args) { | ||
auto this_executor = co_await ThisTask::executor; | ||
co_return co_await boost::asio::async_compose<decltype(boost::asio::use_awaitable), TaskCompletionHandler<F, Args...>>( | ||
[&this_executor, &runner, fn = std::forward<F>(fn), ... args = std::forward<Args>(args)](auto& self) mutable { | ||
boost::asio::post(runner, [&, fn = std::forward<decltype(fn)>(fn), ... args = std::forward<Args>(args), self = std::move(self)]() mutable { | ||
try { | ||
if constexpr (std::is_void_v<std::invoke_result_t<F, Args...>>) { | ||
std::invoke(fn, args...); | ||
boost::asio::post(this_executor, [self = std::move(self)]() mutable { | ||
self.complete({}); | ||
}); | ||
} else { | ||
auto result = std::invoke(fn, args...); | ||
boost::asio::post(this_executor, [result = std::move(result), self = std::move(self)]() mutable { | ||
self.complete({}, result); | ||
}); | ||
} | ||
} catch (...) { | ||
std::exception_ptr eptr = std::current_exception(); | ||
boost::asio::post(this_executor, [eptr, self = std::move(self)]() mutable { | ||
if constexpr (std::is_void_v<std::invoke_result_t<F, Args...>>) | ||
self.complete(eptr); | ||
else | ||
self.complete(eptr, {}); | ||
}); | ||
} | ||
}); | ||
}, | ||
boost::asio::use_awaitable); | ||
} | ||
|
||
} // namespace silkworm::rpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2024 The Silkworm Authors | ||
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. | ||
*/ | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <boost/asio/thread_pool.hpp> | ||
|
||
#include <silkworm/rpc/test/context_test_base.hpp> | ||
|
||
#include "async_task.hpp" | ||
|
||
namespace silkworm::rpc { | ||
|
||
std::size_t recursive_factorial(std::size_t n) { | ||
return n == 0 ? 1 : n * recursive_factorial(n - 1); | ||
} | ||
|
||
struct AsyncTaskBenchTest : test::ContextTestBase { | ||
}; | ||
|
||
template <typename Executor> | ||
Task<std::size_t> async_compose_factorial(const Executor runner, const std::size_t number) { | ||
const auto this_executor = co_await ThisTask::executor; | ||
co_return co_await boost::asio::async_compose<decltype(boost::asio::use_awaitable), void(std::exception_ptr, std::size_t)>( | ||
[&](auto& self) { | ||
boost::asio::post(runner, [&, self = std::move(self)]() mutable { | ||
try { | ||
const auto result = recursive_factorial(number); | ||
boost::asio::post(this_executor, [result, self = std::move(self)]() mutable { | ||
self.complete({}, result); | ||
}); | ||
} catch (...) { | ||
std::exception_ptr eptr = std::current_exception(); | ||
boost::asio::post(this_executor, [eptr, self = std::move(self)]() mutable { | ||
self.complete(eptr, {}); | ||
}); | ||
} | ||
}); | ||
}, | ||
boost::asio::use_awaitable); | ||
} | ||
|
||
static void benchmark_async_compose(benchmark::State& state) { | ||
const auto n = static_cast<std::size_t>(state.range(0)); | ||
|
||
boost::asio::thread_pool workers{}; | ||
AsyncTaskBenchTest test; | ||
for ([[maybe_unused]] auto _ : state) { | ||
const auto result = test.spawn_and_wait(async_compose_factorial(workers.get_executor(), n)); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
|
||
BENCHMARK(benchmark_async_compose)->Arg(10); | ||
BENCHMARK(benchmark_async_compose)->Arg(100); | ||
BENCHMARK(benchmark_async_compose)->Arg(1'000); | ||
BENCHMARK(benchmark_async_compose)->Arg(10'000); | ||
|
||
template <typename Executor> | ||
Task<std::size_t> async_task_factorial(Executor runner, std::size_t number) { | ||
co_return co_await async_task(runner, recursive_factorial, number); | ||
} | ||
|
||
static void benchmark_async_task(benchmark::State& state) { | ||
const auto n = static_cast<std::size_t>(state.range(0)); | ||
|
||
boost::asio::thread_pool workers{}; | ||
AsyncTaskBenchTest test; | ||
for ([[maybe_unused]] auto _ : state) { | ||
const auto result = test.spawn_and_wait(async_task_factorial(workers.get_executor(), n)); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
|
||
BENCHMARK(benchmark_async_task)->Arg(10); | ||
BENCHMARK(benchmark_async_task)->Arg(100); | ||
BENCHMARK(benchmark_async_task)->Arg(1'000); | ||
BENCHMARK(benchmark_async_task)->Arg(10'000); | ||
|
||
} // namespace silkworm::rpc |
Oops, something went wrong.