cek-pykinos
Safe HaskellNone
LanguageHaskell2010

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 unboxed MutableByteArray spanning exactly 5 cache lines:
  • Bytes [ 0 .. 31]: Block header (Int32[0] = cursor, Int32[1] = frozenCursor).
  • Bytes [ 32 .. 63]: Lane 0 — 32 x 1-byte FrameTag opcodes.
  • Bytes [ 64 .. 191]: Lane 1 — 32 x 4-byte Int32 integer fields (intA).
  • Bytes [192 .. 319]: Lane 2 — 32 x 4-byte Int32 integer fields (intB).
  • kbPtrA: A 32-slot SmallMutableArray holding primary boxed term or record pointers.
  • kbPtrB: A 32-slot SmallMutableArray holding 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:

  1. captureKontSoA records the current block and cursor in CapturedKont and freezes the block by updating its frozenCursor.
  2. Future pushes onto a frozen block perform a lazy copy-on-write clone of the active prefix (cloneBlockPrefix) only when modifying frozen slots.
  3. Unfrozen pushes and pops operate in place with zero heap allocations.
Synopsis

Continuation Frame Opcodes

newtype FrameTag Source #

Unboxed 1-byte continuation frame opcode stored in Lane 0 of KontBlock.

Constructors

FrameTag 

Fields

Instances

Instances details
Eq FrameTag Source # 
Instance details

Defined in Cek.Pykinos.Kont

Methods

(==) :: FrameTag -> FrameTag -> Bool

(/=) :: FrameTag -> FrameTag -> Bool

Ord FrameTag Source # 
Instance details

Defined in Cek.Pykinos.Kont

Methods

compare :: FrameTag -> FrameTag -> Ordering

(<) :: FrameTag -> FrameTag -> Bool

(<=) :: FrameTag -> FrameTag -> Bool

(>) :: FrameTag -> FrameTag -> Bool

(>=) :: FrameTag -> FrameTag -> Bool

max :: FrameTag -> FrameTag -> FrameTag

min :: FrameTag -> FrameTag -> FrameTag

Show FrameTag Source # 
Instance details

Defined in Cek.Pykinos.Kont

Methods

showsPrec :: Int -> FrameTag -> ShowS

show :: FrameTag -> String

showList :: [FrameTag] -> ShowS

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

data KontBlock s Source #

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

newtype KontStack s Source #

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.