| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Cek.Pykinos.Kont
Description
Eliminates per-step heap closure allocation by packing 32 continuation frames
into a 320-byte (5 cache-line) MutableByteArray paired with two pointer lanes.
Structure-of-Arrays Continuation Design
In conventional CEK and Krivine machine implementations, every evaluation step
pushes a frame onto a singly-linked list of heap closures (e.g. data Frame = ...).
This incurs significant allocation overhead, GC pressure, and cache misses during
high-frequency interpreter loops.
KontStack replaces the linked list of heap frames with an unrolled Structure-of-Arrays
(SoA) chain of 32-frame blocks (KontBlock):
kbUnboxed: A 320-byte unboxedMutableByteArrayspanning exactly 5 cache lines:- Bytes [ 0 .. 31]: Block header (Int32[0] = cursor, Int32[1] = frozenCursor).
- Bytes [ 32 .. 63]: Lane 0 — 32 x 1-byte
FrameTagopcodes. - Bytes [ 64 .. 191]: Lane 1 — 32 x 4-byte
Int32integer fields (intA). - Bytes [192 .. 319]: Lane 2 — 32 x 4-byte
Int32integer fields (intB). kbPtrA: A 32-slotSmallMutableArrayholding primary boxed term or record pointers.kbPtrB: A 32-slotSmallMutableArrayholding secondary boxed environment or argument pointers.
O(1) Copy-on-Write Continuation Capture
First-class continuation capture (such as Scheme's call/cc and Mercury's choice-point
backtracking) is achieved in strictly $O(1)$ time without copying the stack:
captureKontSoArecords the current block and cursor inCapturedKontand freezes the block by updating itsfrozenCursor.- Future pushes onto a frozen block perform a lazy copy-on-write clone of the active
prefix (
cloneBlockPrefix) only when modifying frozen slots. - Unfrozen pushes and pops operate in place with zero heap allocations.
Synopsis
- newtype FrameTag = FrameTag {
- unFrameTag :: Word8
- pattern FT_UpdateWHNF :: FrameTag
- pattern FT_App :: FrameTag
- pattern FT_Proj :: FrameTag
- pattern FT_MergeL :: FrameTag
- pattern FT_MergeR :: FrameTag
- pattern FT_StartQuote :: FrameTag
- pattern FT_UpdateNF :: FrameTag
- pattern FT_QuoteBinder :: FrameTag
- pattern FT_BuildBinder :: FrameTag
- pattern FT_QuoteRecord :: FrameTag
- pattern FT_QuoteSpine :: FrameTag
- pattern FT_BuildElim :: FrameTag
- pattern FT_PureDissect :: FrameTag
- data KontBlock s = KontBlock {
- kbUnboxed :: !(MutableByteArray s)
- kbPtrA :: !(SmallMutableArray s (Any :: Type))
- kbPtrB :: !(SmallMutableArray s (Any :: Type))
- kbParent :: !(Maybe (KontBlock s))
- kbFreeChild :: !(SmallMutableArray s (Maybe (KontBlock s)))
- newtype KontStack s = KontStack (SmallMutableArray s (KontBlock s))
- data CapturedKont s = CapturedKont {
- ckTopBlock :: !(KontBlock s)
- ckCursor :: !Int
- newKontStack :: ST s (KontStack s)
- pushFrameSoA :: KontStack s -> FrameTag -> Int32 -> Int32 -> (Any :: Type) -> (Any :: Type) -> ST s ()
- popFrameSoA :: KontStack s -> ST s (Maybe (FrameTag, Int32, Int32, Any :: Type, Any :: Type))
- captureKontSoA :: KontStack s -> ST s (CapturedKont s)
- restoreKontSoA :: KontStack s -> CapturedKont s -> ST s ()
Continuation Frame Opcodes
Unboxed 1-byte continuation frame opcode stored in Lane 0 of KontBlock.
Constructors
| FrameTag | |
Fields
| |
pattern FT_UpdateWHNF :: FrameTag Source #
Frame: Overwrite a lazy thunk address in the store with the evaluated WHNF value.
pattern FT_App :: FrameTag Source #
Frame: Apply evaluated function to pending unevaluated argument address.
pattern FT_Proj :: FrameTag Source #
Frame: Project a field from an evaluated record using its slot offset.
pattern FT_MergeL :: FrameTag Source #
Frame: Merge left operand of a record union (//) while waiting on the right operand.
pattern FT_MergeR :: FrameTag Source #
Frame: Merge right operand of a record union (//) with evaluated left operand.
pattern FT_StartQuote :: FrameTag Source #
Frame: Enter NbE quote phase to read back a value into a normal-form DAG.
pattern FT_UpdateNF :: FrameTag Source #
Frame: Update a thunk's memoized normal form in Lane 1 of the store arena.
pattern FT_QuoteBinder :: FrameTag Source #
Frame: Read back a function abstraction by extending the neutral environment.
pattern FT_BuildBinder :: FrameTag Source #
Frame: Reconstruct a binder term after its body has been normalized.
pattern FT_QuoteRecord :: FrameTag Source #
Frame: Normalize fields of a flat record in sequence.
pattern FT_QuoteSpine :: FrameTag Source #
Frame: Read back arguments in an elimination spine.
pattern FT_BuildElim :: FrameTag Source #
Frame: Reconstruct an applied neutral term from normalized arguments.
pattern FT_PureDissect :: FrameTag Source #
Frame: Generic polynomial dissection frame driving generic AST traversal.
Block and Stack Structures
A 32-frame unrolled Structure-of-Arrays block.
Layout inside the 320-byte kbUnboxed MutableByteArray:
* Bytes [ 0 .. 31]: Header (Int32[0] = cursor, Int32[1] = frozenCursor)
* Bytes [ 32 .. 63]: Lane 0 — 32 x Word8 FrameTag opcodes
* Bytes [ 64 .. 191]: Lane 1 — 32 x Int32 IntA (Int32 indices 16 .. 47)
* Bytes [192 .. 319]: Lane 2 — 32 x Int32 IntB (Int32 indices 48 .. 79)
Constructors
| KontBlock | |
Fields
| |
Mutable reference to the active top KontBlock of the continuation stack.
Constructors
| KontStack (SmallMutableArray s (KontBlock s)) |
data CapturedKont s Source #
First-class O(1) continuation snapshot for call/cc and backtracking choice points.
Constructors
| CapturedKont | |
Fields
| |
Stack Lifecycle and Operations
newKontStack :: ST s (KontStack s) Source #
Create a fresh KontStack with one pre-allocated 32-frame block.
pushFrameSoA :: KontStack s -> FrameTag -> Int32 -> Int32 -> (Any :: Type) -> (Any :: Type) -> ST s () Source #
Push a continuation frame onto the stack in O(1) time.
Encodes tag, two unboxed integers, and two pointer lanes with zero heap allocations.
popFrameSoA :: KontStack s -> ST s (Maybe (FrameTag, Int32, Int32, Any :: Type, Any :: Type)) Source #
Pop the topmost continuation frame from the stack in O(1) time.
Clears pointer lanes on pop to avoid memory leaks across deep recursions.
captureKontSoA :: KontStack s -> ST s (CapturedKont s) Source #
Capture the current continuation in O(1) by marking the active block chain frozen.
restoreKontSoA :: KontStack s -> CapturedKont s -> ST s () Source #
Restore a previously captured continuation snapshot in O(1) time.