Skip to content

Conversation

@kammce
Copy link
Member

@kammce kammce commented Jan 6, 2026

Summary

This PR refactors the async_context library by merging the scheduler class into the context base class, improving memory efficiency, simplifying ownership semantics, and reducing indirection. It also introduces a new proxy_context creation method (from) and removes redundant context parameters in scheduling calls.


Changes Overview

  1. Merged scheduler into context:

    • The context class is now an abstract base class with pure virtual function (do_schedule) that are implemented by derived context classes.
    • This removes the need for a separate scheduler object and reduces context size down to just 64 bytes on 64-bit systems.
  2. Stack Memory Ownership:

    • Derived context classes now own their stack memory directly.
    • The context class has a new initialize_stack_memory method to set up the stack.
    • Example:
      class my_context : public async::context {
          std::array<byte, 4096> m_stack;
          my_scheduler& m_scheduler;
      
      public:
          my_context(my_scheduler& sched) : m_scheduler(sched) {
              initialize_stack_memory(m_stack);
          }
      
      protected:
          void do_schedule(blocked_by state, block_info info) override {
              m_scheduler.handle(*this, state, info);
          }
      };
  3. Proxy Context Implementation:

    • The proxy_context class now implements the proxy pattern by delegating scheduling to the original context.
    • A new static method from(context& p_parent) is added for constructing proxies.
  4. Simplified Scheduling API:

    • Removed the redundant context& p_context parameter from schedule() calls.
    • The new signature is: schedule(blocked_by state, block_info info).
  5. Memory Efficiency Gains:

    • Reduced base context size from 64 bytes to 48 bytes.
    • Eliminated pointer indirection and virtual call overhead.
  6. Updated Dependencies & Build Files:

    • Removed strong_ptr dependency
    • Updated test and benchmark code to use the new context-based design.

Benefits

Benefit Description
💾 Memory Efficiency async::context now fits within a single cache line
⚡ Performance Faster virtual calls and reduced indirection from variant
🔒 Ownership Clarity Clear separation of stack ownership
🧩 Flexibility Custom context types for various environments or scheduler types
🧠 Simplicity Cleaner API and less abstraction overhead

Example Usage

struct test_context : public async::context {
    std::array<async::uptr, 8192> m_stack{};

    test_context() {
        this->initialize_stack_memory(m_stack);
    }

protected:
    void do_schedule(blocked_by state, block_info info) noexcept override {
        // Handle scheduling logic here
    }
};

test_context ctx;

Testing & Compatibility

  • All existing tests pass with updated context construction and proxy usage.
  • Benchmarks are adjusted to use the new benchmark_context for performance measurement.
  • The proxy context creation now uses proxy_context::from(parent) instead of the previous proxy constructor.

Conclusion

This PR significantly refactors async_context to better align with embedded systems’ constraints by reducing memory overhead, simplifying ownership models, and enhancing flexibility for different scheduler implementations.

This PR message was generated by Qwen3-coder:30b using LM studio and based on a diff of this change. The content was read and edited by myself.

Proxy context still needs to be implemented.
@kammce kammce marked this pull request as draft January 6, 2026 21:21
@kammce kammce force-pushed the merge-scheduler-and-context branch 3 times, most recently from e5dc77c to 1bdfee6 Compare January 6, 2026 22:15
@kammce kammce changed the title Merge scheduler and context ⚡ Merge Scheduler and Context, Reduce Memory Overhead, and Simplify Ownership Jan 6, 2026
@kammce kammce changed the title ⚡ Merge Scheduler and Context, Reduce Memory Overhead, and Simplify Ownership ⚡ Merge Scheduler and Context Jan 6, 2026
@kammce kammce force-pushed the merge-scheduler-and-context branch from 1bdfee6 to a6c1a7d Compare January 6, 2026 22:32
@kammce kammce marked this pull request as ready for review January 6, 2026 22:35
@kammce kammce force-pushed the merge-scheduler-and-context branch from 94463d0 to 8928f51 Compare January 6, 2026 22:46
@kammce kammce force-pushed the merge-scheduler-and-context branch from 8928f51 to 1e86b35 Compare January 6, 2026 22:47
@kammce kammce merged commit 0d010e7 into main Jan 6, 2026
5 checks passed
@kammce kammce deleted the merge-scheduler-and-context branch January 6, 2026 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants