cek-pykinos
Safe HaskellNone
LanguageHaskell2010

Cek.Pykinos.Store

Description

Separates unboxed cell phase tags, memoized normal-form DAG pointers (NfAddr), and auxiliary 32-bit integers from boxed payload pointers to minimize GC scanning.

Structure-of-Arrays (SoA) Memory Layout

In traditional pointer-heavy graph reduction engines, every lazy evaluation cell (thunk or indirection) is a heap closure containing an info pointer, mutable fields, and payload pointers. As the heap grows, garbage collection pause times degrade proportionally with the total number of cells.

SoAStore eliminates per-cell heap headers by grouping cells into 4096-cell contiguous memory chunks (StoreChunk):

  • scUnboxed: A 36,864-byte unboxed MutableByteArray holding raw bytes and integers in three segregated contiguous lanes:
  • Lane 0 (Bytes 0..4095): 4096 x 1-byte CellTag values.
  • Lane 1 (Bytes 4096..20479): 4096 x 4-byte Int32 memoized normal-form DAG addresses.
  • Lane 2 (Bytes 20480..36863): 4096 x 4-byte Int32 auxiliary integers (literals, symbols).
  • scPtrA: A 4096-slot SmallMutableArray holding primary boxed payload pointers.
  • scPtrB: A 4096-slot SmallMutableArray holding secondary boxed payload pointers.

Cell Lifecycle Transitions

Cells progress through strict evaluation phases tracked in Lane 0:

  1. CT_Thunk: Unevaluated closure (term, env) stored across scPtrA and scPtrB.
  2. CT_Blackhole: Currently under active evaluation. Cycles trigger runtime errors.
  3. CT_WLit: Integer / word literal stored unboxed in Lane 2 with zero heap allocations.
  4. CT_WBinder: Evaluated lambda or function abstraction.
  5. CT_WRecord: Evaluated record with interned SchemaId.
  6. CT_WNeu: Neutral application or variable spine pending normalization.
Synopsis

Cell Phase Tags

newtype CellTag Source #

1-byte cell phase opcode stored in Lane 0 of StoreChunk.

Constructors

CellTag 

Fields

Instances

Instances details
Eq CellTag Source # 
Instance details

Defined in Cek.Pykinos.Store

Methods

(==) :: CellTag -> CellTag -> Bool

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

Ord CellTag Source # 
Instance details

Defined in Cek.Pykinos.Store

Methods

compare :: CellTag -> CellTag -> Ordering

(<) :: CellTag -> CellTag -> Bool

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

(>) :: CellTag -> CellTag -> Bool

(>=) :: CellTag -> CellTag -> Bool

max :: CellTag -> CellTag -> CellTag

min :: CellTag -> CellTag -> CellTag

Show CellTag Source # 
Instance details

Defined in Cek.Pykinos.Store

Methods

showsPrec :: Int -> CellTag -> ShowS

show :: CellTag -> String

showList :: [CellTag] -> ShowS

pattern CT_Thunk :: CellTag Source #

Unevaluated lazy thunk closure (term, env).

pattern CT_Blackhole :: CellTag Source #

In-progress evaluation sentinel; detects infinite recursion loops.

pattern CT_WLit :: CellTag Source #

Evaluated unboxed integer/boolean literal.

pattern CT_WBinder :: CellTag Source #

Evaluated lambda abstraction or binder closure.

pattern CT_WRecord :: CellTag Source #

Evaluated flat record mapped by a SchemaId.

pattern CT_WNeu :: CellTag Source #

Neutral term spine waiting on an unbound variable or argument.

pattern CT_Unbound :: CellTag Source #

Unbound / unallocated cell slot marker.

Store Arena Structures

data StoreChunk s Source #

A 4096-cell Structure-of-Arrays Store chunk.

Layout of the 36,864-byte scUnboxed MutableByteArray: * Bytes [ 0 .. 4095]: Lane 0 — 4096 x Word8 CellTag (1 byte per cell) * Bytes [ 4096 .. 20479]: Lane 1 — 4096 x Int32 Memoized NfAddr (4 bytes per cell) * Bytes [20480 .. 36863]: Lane 2 — 4096 x Int32 AuxInt (4 bytes per cell)

Constructors

StoreChunk 

Fields

data SoAStore s Source #

Chunked SoA Store arena supporting up to 4,194,304 cells across 1024 chunks.

Constructors

SoAStore 

Fields

Store Allocation and Initialization

newSoAStore :: ST s (SoAStore s) Source #

Create a new SoAStore with chunk 0 pre-allocated.

acquireSlot :: SoAStore s -> ST s (Int32, StoreChunk s, Int) Source #

Acquire the next monotonic cell Int32 address, allocating a new chunk on boundary crossings.

Returns the global address, the enclosing StoreChunk, and the local intra-chunk slot index (0..4095).

allocThunkSoA :: SoAStore s -> a -> b -> ST s Int32 Source #

Allocate a lazy thunk (term, env) cell in the Store.

Initializes the cell phase to CT_Thunk and saves the term and environment pointers.

allocWLitSoA :: SoAStore s -> Int32 -> ST s Int32 Source #

Allocate an unboxed integer literal cell with zero pointer writes.

Stores the value directly in Lane 2 of scUnboxed, completely bypassing the GC write barrier.

allocWNeuSoA :: SoAStore s -> Int32 -> ST s Int32 Source #

Allocate a fresh neutral binder symbol WNeu sym [] in the Store.

Records the symbol ID in Lane 2 and initializes an empty elimination spine.

Cell Phase and Memoization Accessors

readCellPhaseSoA :: SoAStore s -> Int32 -> ST s (CellTag, Int32, StoreChunk s, Int) Source #

Read the unboxed CellTag, AuxInt, StoreChunk, and slot offset for addr.

readMemoNfSoA :: SoAStore s -> Int32 -> ST s Int32 Source #

Check in O(1) whether addr already has a memoized normal-form DAG pointer (NfAddr >= 0).

writeMemoNfSoA :: SoAStore s -> Int32 -> Int32 -> ST s () Source #

Record a memoized normal-form DAG pointer nfAddr for cell addr.