Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ project(async_context LANGUAGES CXX)
# Library
# ==============================================================================

find_package(strong_ptr REQUIRED)

add_library(async_context STATIC)
target_compile_features(async_context PUBLIC cxx_std_23)
target_link_libraries(async_context PUBLIC strong_ptr)
target_sources(async_context PUBLIC
FILE_SET CXX_MODULES
TYPE CXX_MODULES
Expand Down Expand Up @@ -63,10 +60,18 @@ install(
EXPORT_PACKAGE_DEPENDENCIES
)

# Always run this custom target by making it depend on ALL
add_custom_target(copy_compile_commands ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)

# ==============================================================================
# Unit testing
# ==============================================================================

if(TRUE)
if(CMAKE_CROSSCOMPILING)
message(STATUS "Cross compiling, skipping unit test execution")
else()
Expand Down Expand Up @@ -98,18 +103,13 @@ else()

add_custom_target(run_tests ALL DEPENDS async_unit_test COMMAND async_unit_test)
endif()

# Always run this custom target by making it depend on ALL
add_custom_target(copy_compile_commands ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)
endif()

# ==============================================================================
# Benchmarking
# ==============================================================================

if(FALSE)
if(CMAKE_CROSSCOMPILING)
message(STATUS "Cross compiling, skipping benchmarks")
else()
Expand All @@ -135,3 +135,4 @@ else()

add_custom_target(run_benchmark ALL DEPENDS async_benchmark COMMAND async_benchmark)
endif()
endif()
65 changes: 12 additions & 53 deletions benchmarks/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import async_context;

// Quick Bench: https://quick-bench.com/
// Compiler flags: -std=c++23 -O3 -DNDEBUG
//
// Include your de-moduled async_context code above this section
// ============================================================================

// ============================================================================
// BENCHMARKS
Expand Down Expand Up @@ -251,63 +248,25 @@ __attribute__((noinline)) async::future<int> sync_future_level1(
auto f = sync_future_level2(ctx, x);
return f.sync_wait() + 1;
}
struct test_scheduler
: public async::scheduler
, mem::enable_strong_from_this<test_scheduler>
struct benchmark_context : public async::context
{
int sleep_count = 0;
async::context* sync_context = nullptr;
bool io_block = false;
std::array<async::uptr, 8192> m_stack{};

test_scheduler(mem::strong_ptr_only_token)
benchmark_context()
{
this->initialize_stack_memory(m_stack);
}

private:
void do_schedule([[maybe_unused]] async::context& p_context,
[[maybe_unused]] async::blocked_by p_block_state,
[[maybe_unused]] async::scheduler::block_info
p_block_info) noexcept override
void do_schedule(async::blocked_by, async::block_info) noexcept override
{
switch (p_block_state) {
case async::blocked_by::time: {
if (std::holds_alternative<std::chrono::nanoseconds>(p_block_info)) {
sleep_count++;
}
break;
}
case async::blocked_by::sync: {
if (std::holds_alternative<async::context*>(p_block_info)) {
auto* context = std::get<async::context*>(p_block_info);
sync_context = context;
}
break;
}
case async::blocked_by::io: {
io_block = true;
break;
}
case async::blocked_by::nothing: {
break;
}
default: {
break;
}
}
}

std::pmr::memory_resource& do_get_allocator() noexcept override
{
return *strong_from_this().get_allocator();
// Do nothing for the benchmark
}
};

auto scheduler =
mem::make_strong_ptr<test_scheduler>(std::pmr::new_delete_resource());

static void bm_future_sync_return(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
for (auto _ : state) {
Expand Down Expand Up @@ -344,7 +303,7 @@ __attribute__((noinline)) async::future<int> coro_level1(async::context& ctx,

static void bm_future_coroutine(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
for (auto _ : state) {
Expand Down Expand Up @@ -385,7 +344,7 @@ __attribute__((noinline)) async::future<int> sync_in_coro_level1(

static void bm_future_sync_await(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
for (auto _ : state) {
Expand Down Expand Up @@ -425,7 +384,7 @@ __attribute__((noinline)) async::future<int> mixed_coro_level1(

static void bm_future_mixed(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
for (auto _ : state) {
Expand Down Expand Up @@ -466,7 +425,7 @@ void_coro_level1(async::context& ctx, int& out, int x)

static void bm_future_void_coroutine(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
int output = 0;
Expand All @@ -481,7 +440,7 @@ BENCHMARK(bm_future_void_coroutine);

static void bm_future_void_coroutine_context_resume(benchmark::State& state)
{
async::context ctx(scheduler, 4096);
benchmark_context ctx;

int input = 42;
int output = 0;
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def build_requirements(self):
self.test_requires("benchmark/1.9.4")

def requirements(self):
self.requires("strong_ptr/0.1.2")
pass

def layout(self):
cmake_layout(self)
Expand Down
Loading