Skip to content

Conversation

@kammce
Copy link
Member

@kammce kammce commented Jan 5, 2026

Problem

Previously, each promise had to store its frame size to enable manual deallocation. This required a complex handoff:

  1. Context stores allocation size in variant state during operator new
  2. Promise copies that size to m_frame_size member in constructor
  3. Promise calls context.deallocate(m_frame_size) in self_destruct()

This handoff mechanism forced every promise type to have a non-trivial destructor and carry an extra member variable.

Solution

Store a back-pointer before each coroutine frame pointing to the context's m_stack_pointer. Now operator delete can find and update the stack pointer directly, eliminating the need for promises to track their own size.

Benefits

Removed from promises:

  • m_frame_size member variable
  • self_destruct() method
  • Non-trivial destructor logic

Simplified context:

  • State is now just blocked_by instead of variant<usize, blocked_by>
  • Removes variant discriminator, saving 1 word in context size
  • Direct state access (no variant checks)

Stack now uses uintptr_t: Changed from byte* to uptr* for proper alignment of back-pointers.

Added context::resume(): Convenience method for manual event loops.

Trade-off

Still allocates +1 word per frame (now for back-pointer instead of in promise), but memory layout is cleaner and promises are simpler.

Testing

Re-enabled all previously skipped tests - all passing.

@kammce kammce force-pushed the max-aligned-stack branch from c14e304 to 7ddb4be Compare January 6, 2026 00:41
@kammce kammce changed the title Remove frame size from promise types using back-pointer deallocation ⚡ Remove frame size from promise types using back-pointer deallocation Jan 6, 2026
@kammce kammce force-pushed the max-aligned-stack branch 2 times, most recently from f743223 to 8659852 Compare January 6, 2026 00:57
@kammce kammce force-pushed the max-aligned-stack branch from 8659852 to a35c120 Compare January 6, 2026 00:58
@kammce kammce force-pushed the max-aligned-stack branch from e34fa39 to 0596903 Compare January 6, 2026 03:58
@kammce kammce merged commit d0393de into main Jan 6, 2026
5 checks passed
@kammce kammce deleted the max-aligned-stack branch January 6, 2026 13:57
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