Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
02d6711
stack concept
Feb 5, 2026
d9feced
refined concepts
Feb 5, 2026
93f4fb6
context concepts
Feb 5, 2026
c6723e2
thoughts
Feb 5, 2026
876e835
wip
Feb 5, 2026
c5b2693
invocability
Feb 5, 2026
cd5c526
wip
Feb 5, 2026
82202c2
handle const contexts/stacks
Feb 6, 2026
4dc2856
.switch API for stack allocator
Feb 6, 2026
720b5c9
rm no format bits
Feb 6, 2026
d03de33
make frame_handle templated on Context
Feb 6, 2026
e047749
todo
Feb 6, 2026
b33289f
rename
Feb 6, 2026
6d45b09
rename context -> worker_context
Feb 6, 2026
d986119
tweak
Feb 6, 2026
0d5a2c2
add comment
Feb 6, 2026
ac511db
fixes
Feb 8, 2026
4b35191
comment out bits
Feb 8, 2026
a6ab424
ops in struct
Feb 8, 2026
d426fc3
compiling
Feb 8, 2026
3588a0a
just compiling
Feb 8, 2026
3eceea7
type-check promise
Feb 8, 2026
629afce
polymorphic + global alocator
Feb 8, 2026
d064fbf
no await
Feb 8, 2026
86c5e60
linear alloc (unused)
Feb 8, 2026
cd91e9c
break circular dep
Feb 8, 2026
12e6b6f
first arg but no destroy
Feb 8, 2026
3f4b71a
running with global again
Feb 8, 2026
6584f18
stash in allocation
Feb 8, 2026
35647fc
some tail code
Feb 8, 2026
0b0dde8
no tls downpropagate call
Feb 8, 2026
bf805e1
set ctx call return
Feb 8, 2026
13ae9f6
fix spell
Feb 8, 2026
bcb6bc7
rename
Feb 9, 2026
7e208e0
revert context/first arg in concepts
Feb 9, 2026
82fb3c4
Revert "ops in struct"
Feb 9, 2026
e9d1da6
re-apply promise tls usage
Feb 9, 2026
70bbe37
fix benchmarks
Feb 9, 2026
91c5b45
more parts
Feb 9, 2026
abfc950
restore fork path
Feb 9, 2026
2bde92f
poly vector context
Feb 9, 2026
9bd1cbb
add final
Feb 9, 2026
a404d69
tidy ups
Feb 9, 2026
707c08d
drop dead function
Feb 9, 2026
b4cb0a9
store checkpoints
Feb 9, 2026
371f295
rm todo
Feb 9, 2026
4e81407
rm dead includes
Feb 9, 2026
0e4e85b
rm dead comments
Feb 9, 2026
1bea4da
fixup comments
Feb 9, 2026
55534e8
comments
Feb 9, 2026
6053d83
move static asserts to test file
Feb 9, 2026
769ef3f
rm dead benchmark code
Feb 9, 2026
defc931
constructor for polymorphic context
Feb 10, 2026
2a95dac
remove definition from constify
Feb 10, 2026
8f355ec
header
Feb 10, 2026
b8e45c7
reduce buffer size
Feb 10, 2026
192685f
clean up of thread context
Feb 10, 2026
a9493d6
default constructor
Feb 10, 2026
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
39 changes: 24 additions & 15 deletions benchmark/src/libfork_benchmark/fib/fib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,41 @@ struct tls_bump {
}
};

constinit inline std::byte *bump_ptr = nullptr;
// === Shared Context Logic ===

struct global_bump {
template <lf::stack_allocator Alloc>
struct vector_ctx {

static auto operator new(std::size_t sz) -> void * {
auto *prev = bump_ptr;
bump_ptr += fib_align_size(sz);
return prev;
}
using handle_type = lf::frame_handle<vector_ctx>;

static auto operator delete(void *p, [[maybe_unused]] std::size_t sz) noexcept -> void {
bump_ptr = std::bit_cast<std::byte *>(p);
std::vector<handle_type> work;
Alloc allocator;

vector_ctx() { work.reserve(1024); }

auto alloc() noexcept -> Alloc & { return allocator; }

void push(handle_type handle) { work.push_back(handle); }

auto pop() noexcept -> handle_type {
auto handle = work.back();
work.pop_back();
return handle;
}
};

// === Shared Context Logic ===
template <lf::stack_allocator Alloc>
struct poly_vector_ctx final : lf::polymorphic_context<Alloc> {

struct vector_ctx final : lf::polymorphic_context {
using handle_type = lf::frame_handle<lf::polymorphic_context<Alloc>>;

std::vector<lf::work_handle> work;
std::vector<handle_type> work;

vector_ctx() { work.reserve(1024); }
poly_vector_ctx() { work.reserve(1024); }

void push(lf::work_handle handle) override { work.push_back(handle); }
void push(handle_type handle) override { work.push_back(handle); }

auto pop() noexcept -> lf::work_handle override {
auto pop() noexcept -> handle_type override {
auto handle = work.back();
work.pop_back();
return handle;
Expand Down
123 changes: 70 additions & 53 deletions benchmark/src/libfork_benchmark/fib/lf_parts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,41 @@ import libfork.core;

namespace {

struct stack_on_heap {
static constexpr auto operator new(std::size_t sz) -> void * { return ::operator new(sz); }
static constexpr auto operator delete(void *p, [[maybe_unused]] std::size_t sz) noexcept -> void {
::operator delete(p, sz);
struct global_allocator {

struct empty {};

constexpr static auto push(std::size_t sz) -> void * { return ::operator new(sz); }
constexpr static auto pop(void *p, std::size_t sz) noexcept -> void { ::operator delete(p, sz); }
constexpr static auto checkpoint() noexcept -> empty { return {}; }
constexpr static auto switch_to(empty) noexcept -> void {}
};

static_assert(lf::stack_allocator<global_allocator>);

struct linear_allocator {

std::unique_ptr<std::byte[]> data = std::make_unique<std::byte[]>(1024 * 1024);
std::byte *ptr = data.get();

constexpr auto push(std::size_t sz) -> void * {
auto *prev = ptr;
ptr += fib_align_size(sz);
return prev;
}
constexpr auto pop(void *p, std::size_t) noexcept -> void { ptr = static_cast<std::byte *>(p); }

constexpr auto checkpoint() noexcept -> std::byte * { return data.get(); }

constexpr static auto switch_to(std::byte *) noexcept -> void {}
};

template <lf::alloc_mixin Stack>
constexpr auto no_await = [](this auto fib, std::int64_t *ret, std::int64_t n) -> lf::task<void, Stack> {
static_assert(lf::stack_allocator<linear_allocator>);

using lf::task;

template <lf::worker_context T>
constexpr auto no_await = [](this auto fib, std::int64_t *ret, std::int64_t n) -> task<void, T> {
if (n < 2) {
*ret = n;
co_return;
Expand All @@ -40,8 +66,8 @@ constexpr auto no_await = [](this auto fib, std::int64_t *ret, std::int64_t n) -
*ret = lhs + rhs;
};

template <lf::alloc_mixin Stack>
constexpr auto await = [](this auto fib, std::int64_t *ret, std::int64_t n) -> lf::task<void, Stack> {
template <lf::worker_context T>
constexpr auto await = [](this auto fib, std::int64_t *ret, std::int64_t n) -> lf::task<void, T> {
if (n < 2) {
*ret = n;
co_return;
Expand All @@ -56,7 +82,8 @@ constexpr auto await = [](this auto fib, std::int64_t *ret, std::int64_t n) -> l
*ret = lhs + rhs;
};

constexpr auto ret = [](this auto fib, std::int64_t n) -> lf::task<std::int64_t, tls_bump> {
template <lf::worker_context T>
constexpr auto ret = [](this auto fib, std::int64_t n) -> lf::task<std::int64_t, T> {
if (n < 2) {
co_return n;
}
Expand All @@ -70,8 +97,8 @@ constexpr auto ret = [](this auto fib, std::int64_t n) -> lf::task<std::int64_t,
co_return lhs + rhs;
};

template <typename Ctx, typename A = tls_bump>
constexpr auto fork_call = [](this auto fib, std::int64_t n) -> lf::task<std::int64_t, A, Ctx> {
template <typename T>
constexpr auto fork_call = [](this auto fib, std::int64_t n) -> lf::task<std::int64_t, T> {
if (n < 2) {
co_return n;
}
Expand All @@ -85,23 +112,24 @@ constexpr auto fork_call = [](this auto fib, std::int64_t n) -> lf::task<std::in
co_return lhs + rhs;
};

template <auto Fn>
using global_alloc = vector_ctx<global_allocator>;
using linear_alloc = vector_ctx<linear_allocator>;

template <auto Fn, typename T, typename U = T>
void fib(benchmark::State &state) {

std::int64_t n = state.range(0);
std::int64_t expect = fib_ref(n);

state.counters["n"] = static_cast<double>(n);

// Set bump allocator buffer
std::unique_ptr buf = std::make_unique<std::byte[]>(1024 * 1024);
tls_bump_ptr = buf.get();
bump_ptr = buf.get();
T context;

lf::thread_context<U> = static_cast<U *>(&context);

// Set both context and poly context
std::unique_ptr ctx = std::make_unique<vector_ctx>();
lf::thread_context<vector_ctx> = ctx.get();
lf::thread_context<lf::polymorphic_context> = ctx.get();
lf::defer _ = [] static noexcept {
lf::thread_context<U> = nullptr;
};

for (auto _ : state) {
benchmark::DoNotOptimize(n);
Expand All @@ -121,55 +149,44 @@ void fib(benchmark::State &state) {
CHECK_RESULT(result, expect);
benchmark::DoNotOptimize(result);
}

if (tls_bump_ptr != buf.get() || bump_ptr != buf.get()) {
LF_TERMINATE("Stack leak detected");
}

tls_bump_ptr = nullptr;
bump_ptr = nullptr;
lf::thread_context<vector_ctx> = nullptr;
lf::thread_context<lf::polymorphic_context> = nullptr;
}

} // namespace

// Return by ref-arg, test direct root, no co-await, direct resumes, uses new/delete for alloc
BENCHMARK(fib<no_await<stack_on_heap>>)->Name("test/libfork/fib/heap/no_await")->Arg(fib_test);
BENCHMARK(fib<no_await<stack_on_heap>>)->Name("base/libfork/fib/heap/no_await")->Arg(fib_base);
static_assert(lf::worker_context<global_alloc>);

// Same as above but uses tls bump allocator
BENCHMARK(fib<no_await<tls_bump>>)->Name("test/libfork/fib/tls_bump/no_await")->Arg(fib_test);
BENCHMARK(fib<no_await<tls_bump>>)->Name("base/libfork/fib/tls_bump/no_await")->Arg(fib_base);
// Return by ref-arg, test direct root, no co-await, direct resumes, uses new/delete for alloc
BENCHMARK(fib<no_await<global_alloc>, global_alloc>)->Name("test/libfork/fib/heap/no_await")->Arg(fib_test);
BENCHMARK(fib<no_await<global_alloc>, global_alloc>)->Name("base/libfork/fib/heap/no_await")->Arg(fib_base);

// Same as above but with global bump allocator
BENCHMARK(fib<no_await<global_bump>>)->Name("test/libfork/fib/global_bump/no_await")->Arg(fib_test);
BENCHMARK(fib<no_await<global_bump>>)->Name("base/libfork/fib/global_bump/no_await")->Arg(fib_base);
// Same as above but uses bump allocator
BENCHMARK(fib<no_await<linear_alloc>, linear_alloc>)->Name("test/libfork/fib/bump/no_await")->Arg(fib_test);
BENCHMARK(fib<no_await<linear_alloc>, linear_alloc>)->Name("base/libfork/fib/bump/no_await")->Arg(fib_base);

// TODO: no_await with segmented stack allocator?

// Return by ref-arg, libfork call/call with co-await, uses new/delete for alloc
BENCHMARK(fib<await<stack_on_heap>>)->Name("test/libfork/fib/heap/await")->Arg(fib_test);
BENCHMARK(fib<await<stack_on_heap>>)->Name("base/libfork/fib/heap/await")->Arg(fib_base);
BENCHMARK(fib<await<global_alloc>, global_alloc>)->Name("test/libfork/fib/heap/await")->Arg(fib_test);
BENCHMARK(fib<await<global_alloc>, global_alloc>)->Name("base/libfork/fib/heap/await")->Arg(fib_base);

// Same as above but uses tls bump allocator
BENCHMARK(fib<await<tls_bump>>)->Name("test/libfork/fib/tls_bump/await")->Arg(fib_test);
BENCHMARK(fib<await<tls_bump>>)->Name("base/libfork/fib/tls_bump/await")->Arg(fib_base);

// Same as above but with global bump allocator
BENCHMARK(fib<await<global_bump>>)->Name("test/libfork/fib/global_bump/await")->Arg(fib_test);
BENCHMARK(fib<await<global_bump>>)->Name("base/libfork/fib/global_bump/await")->Arg(fib_base);
// // Same as above but uses bump allocator
BENCHMARK(fib<await<linear_alloc>, linear_alloc>)->Name("test/libfork/fib/bump/await")->Arg(fib_test);
BENCHMARK(fib<await<linear_alloc>, linear_alloc>)->Name("base/libfork/fib/bump/await")->Arg(fib_base);

// Return by value
// libfork call/call with co-await
BENCHMARK(fib<ret>)->Name("test/libfork/fib/tls_bump/return")->Arg(fib_test);
BENCHMARK(fib<ret>)->Name("base/libfork/fib/tls_bump/return")->Arg(fib_base);
BENCHMARK(fib<ret<linear_alloc>, linear_alloc>)->Name("test/libfork/fib/bump/return")->Arg(fib_test);
BENCHMARK(fib<ret<linear_alloc>, linear_alloc>)->Name("base/libfork/fib/bump/return")->Arg(fib_base);

// Return by value
// libfork call/fork (no join)
// Non-polymorphic vector-backed context
BENCHMARK(fib<fork_call<vector_ctx>>)->Name("test/libfork/fib/vector_ctx")->Arg(fib_test);
BENCHMARK(fib<fork_call<vector_ctx>>)->Name("base/libfork/fib/vector_ctx")->Arg(fib_base);
BENCHMARK(fib<fork_call<linear_alloc>, linear_alloc>)->Name("test/libfork/fib/vector_ctx")->Arg(fib_test);
BENCHMARK(fib<fork_call<linear_alloc>, linear_alloc>)->Name("base/libfork/fib/vector_ctx")->Arg(fib_base);

using A = poly_vector_ctx<linear_allocator>;
using B = lf::polymorphic_context<linear_allocator>;

BENCHMARK(fib<fork_call<lf::polymorphic_context>>)->Name("test/libfork/fib/poly_vector_ctx")->Arg(fib_test);
BENCHMARK(fib<fork_call<lf::polymorphic_context>>)->Name("base/libfork/fib/poly_vector_ctx")->Arg(fib_base);
// Same as above but with polymorphic contexts.
BENCHMARK(fib<fork_call<B>, A, B>)->Name("test/libfork/fib/poly_vector_ctx")->Arg(fib_test);
BENCHMARK(fib<fork_call<B>, A, B>)->Name("base/libfork/fib/poly_vector_ctx")->Arg(fib_base);
100 changes: 84 additions & 16 deletions src/core/concepts.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import std;

namespace lf {

// ========== Specialization ========== //

template <typename T, template <typename...> typename Template>
struct is_specialization_of : std::false_type {};

template <template <typename...> typename Template, typename... Args>
struct is_specialization_of<Template<Args...>, Template> : std::true_type {};

/**
* @brief Test if `T` is a specialization of the template `Template`.
*/
export template <typename T, template <typename...> typename Template>
concept specialization_of = is_specialization_of<std::remove_cvref_t<T>, Template>::value;

// ========== Task constraint related concepts ========== //

// ==== Returnable

/**
* @brief A type returnable from libfork's async functions/coroutines.
*
Expand All @@ -13,31 +31,78 @@ namespace lf {
template <typename T>
concept returnable = std::is_void_v<T> || std::movable<T>;

// ==== Stack

template <typename T>
concept mixinable = std::is_empty_v<T> && !std::is_final_v<T>;
requires std::is_object_v<T>
consteval auto constify(T &&x) noexcept -> std::add_const_t<T> &;

/**
* @brief Defines the API for a libfork compatible stack allocator.
*
* - After construction push is valid.
* - Pop is valid provided the FILO order is respected.
* - Destruction is expected to only occur when the stack is empty.
* - Result of `.checkpoint()` is expected to be "cheap to copy".
* - Switch releases the current stack and resumes from the checkpoint:
* - This is a noop if the checkpoint is from this stack.
* - If the checkpoint is default-constructed it is expected to switch to a new stack.
*
* Fast-path operations: empty, push, pop, checkpoint
* Slow-path operations: switch
*/
export template <typename T>
concept alloc_mixin = mixinable<T> && requires (std::size_t n, T *ptr) {
{ T::operator new(n) } -> std::same_as<void *>;
{ T::operator delete(ptr, n) } noexcept -> std::same_as<void>;
concept stack_allocator = std::is_object_v<T> && requires (T alloc, std::size_t n, void *ptr) {
// { alloc.empty() } noexcept -> std::same_as<bool>;
{ alloc.push(n) } -> std::same_as<void *>;
{ alloc.pop(ptr, n) } noexcept -> std::same_as<void>;
{ alloc.checkpoint() } noexcept -> std::semiregular;
{ alloc.switch_to({}) } noexcept -> std::same_as<void>;
{ alloc.switch_to(constify(alloc.checkpoint())) } noexcept -> std::same_as<void>;
};

template <typename T, template <typename...> typename Template>
struct is_specialization_of : std::false_type {};
/**
* @brief Fetch the checkpoint type of a stack allocator `T`.
*/
template <stack_allocator T>
using checkpoint_t = decltype(std::declval<T &>().checkpoint());

template <template <typename...> typename Template, typename... Args>
struct is_specialization_of<Template<Args...>, Template> : std::true_type {};
// ==== Context

export template <typename T>
class frame_handle;

template <typename T>
concept ref_to_stack_allocator = std::is_lvalue_reference_v<T> && stack_allocator<std::remove_reference_t<T>>;

/**
* @brief Test if `T` is a specialization of the template `Template`.
* @brief Defines the API for a libfork compatible worker context.
*
* This requires that `T` is an object type and supports the following operations:
*
* - Push/pop a frame handle onto the context in a LIFO manner.
* - Have a `stack_allocator` that can be accessed via `alloc()`.
*/
template <typename T, template <typename...> typename Template>
concept specialization_of = is_specialization_of<std::remove_cvref_t<T>, Template>::value;
export template <typename T>
concept worker_context = std::is_object_v<T> && requires (T ctx, frame_handle<T> handle) {
{ ctx.alloc() } noexcept -> ref_to_stack_allocator;
{ ctx.push(handle) } -> std::same_as<void>;
{ ctx.pop() } noexcept -> std::same_as<frame_handle<T>>;
};

/**
* @brief Fetch the allocator type of a worker context `T`.
*/
template <worker_context T>
using allocator_t = std::remove_reference_t<decltype(std::declval<T &>().alloc())>;

// ==== Forward-decl

// Forward-decl
export template <returnable T, alloc_mixin Stack, typename Context>
export template <returnable T, worker_context Context>
struct task;

// ========== Invocability ========== //

/**
* @brief Test if a callable `Fn` when invoked with `Args...` returns an `lf::task`.
*/
Expand All @@ -50,9 +115,12 @@ concept async_invocable =
*/
export template <typename Fn, typename... Args>
requires async_invocable<Fn, Args...>
using async_result_t = std::invoke_result_t<Fn, Args...>::type;
using async_result_t = std::invoke_result_t<Fn, Args...>::value_type;

template <typename Fn, typename R, typename... Args>
concept async_invocable_to = async_invocable<Fn, Args...> && std::same_as<async_result_t<Fn, Args...>, R>;
/**
* @brief Subsumes `async_invocable` and checks the result type is `R`.
*/
export template <typename Fn, typename R, typename... Args>
concept async_invocable_to = async_invocable<Fn, Args...> && std::same_as<R, async_result_t<Fn, Args...>>;

} // namespace lf
Loading
Loading