Hylix stable engine
cdk-rsx-engine-hylix implements Hylix, the stable engine. It supplies stable64
allocation, fixed positional codecs and synchronous buffered byte access.
Store owns the shared module tree and one arena, lending its capability while
an actor scope is active. Select Hylix explicitly with memory(..., hylix) for
a complete Data record. Its collection facades live in
cdk_rsx_collections::hylix.
The release bundle's examples/hylix-quickstart shows a typed Store root
with one stable-memory provider and a private Hylix counter. Use
Arena::format only during install. On KEEP, Arena::open checks the same
arena contract before the root rebinds its services. The generated-client
example verifies the populated stable image and a fresh write after restore.
Hylix Quickstart
Arena<M> exclusively owns a platform Memory provider. Its RSXBUF02 anchor
holds the compiled 32-byte contract, logical frontier, extent/descriptor lists,
optional root and segment geometry. Protected 64-byte slab descriptors occupy
permanent metadata segments separate from payloads. Opening checks the anchor,
segment geometry and optional root without scanning allocations or reconstructing
schemas. Cache capacity is transient policy, not stored geometry.
Every allocation has a 16-byte packed header. A nonowning Block stores its
stable64 offset and generation in another 16 bytes. Requests use power-of-two size
classes starting at 32 total bytes. Partially occupied slabs reuse slots within
their class. Final release returns an empty extent for reuse across classes;
larger free extents split, and adjacent free extents coalesce within the chains
separated by protected metadata segments. A request needs one suitable contiguous
extent. Releasing a free tail can retreat the logical frontier; physical stable
memory does not shrink. Small headers and payloads fit in one segment. Resize
keeps the identity within capacity and otherwise copies existing bytes through a
bounded 4-KiB buffer. New or newly exposed bytes require initialization before
publication.
Block generation is opaque: its high 40 bits identify the protected descriptor in 16-byte units, and its low 24 bits are a descriptor-local counter. It has no global ordering. Descriptor admission checks the active lifetime and slot before reading the payload header, so old header bytes inside reused payloads cannot revive a stale identity. Counters never wrap; an exhausted descriptor cannot issue another allocation identity, while its existing live blocks remain valid.
The default byte cache holds at most 256 initialized, aligned 4-KiB segments: 1 MiB of segment backing plus table/FIFO/owner metadata. Allocator metadata and payloads share this cache. Within a frame, misses read a complete segment; header checks and payload access then reuse its coherent bytes. Cold validation can additionally read a protected metadata segment. A disposable decoded allocator and bounded slab cache avoid repeated metadata decoding. Hot descriptor admission borrows instead of copying it, releasing the loan before payload access. Slab changes accumulate until mutable eviction or frame completion; an immutable miss never evicts a dirty entry. Only clean entries survive commit.
A bounded header cache also reuses admission for a complete Block identity.
Only the arena can change allocation headers: successful header writes update
the cache, and deallocation invalidates the exact token before release. Misses
validate both descriptor and header; every requested span keeps its bounds
check. Frame discard clears all decoded caches. FIFO byte-cache eviction skips pinned immutable slices, with
scoped uncached scratch if every entry is pinned.
read_with lends a checked segment slice for a range within one segment;
crossing ranges assemble transient scratch. No borrowed slice can escape.
Storage::copy_bytes validates both complete live-generation ranges before
mutation, then transfers initialized encodings with overlap-safe ordering.
Same-segment moves use the resident bytes directly; crossing transfers reuse a
bounded stack workspace and release source loans before loading destination
segments. No move decodes values or creates ownership edges. Writes that fail
after mutation starts trap. The default implementation uses checked provider
reads and writes; the arena specializes transfer to avoid repeated admission.
Payload slots share exact 64-KiB backing allocations with control metadata
outside; first use initializes only the leased slot. Eviction reuses an unpinned
victim's unique Rc<Segment> and initialized payload after dirty flush/removal,
avoiding a new control allocation or payload zeroing. Refill resets dirty tracking
and publishes the new cache key only after a successful read; failed refills
release the victim. Live views keep their pins and all-pinned misses use scratch.
Writes update coherent bytes and merge each segment's minimum/maximum dirty
span. Dirty eviction and frame finalization flush that span, which may include
gaps between sparse edits. A bounded Vec<u64> records each dirty resident key
once; it adds no Rc pin and cannot exceed the resident segment count. Eviction
flushes and swap-removes its key, repairing the moved key's independent index.
Finalization visits only dirty keys, without scanning or changing clean cached
segments. Default successful write frames retain only bounded
clean entries across messages. Stable bytes own every committed record; the
entire cache can be lost without losing a committed mutation. Store's decoded
module values remain separate and last only one synchronous frame.
Storage implementations and wrappers must provide frame_begin,
frame_finish and frame_discard. Store scopes activate them through
using/using_read: pending owner retirement and module publication precede the
final segment flush. Ordinary returned business errors still finalize. A failing
frame_finish after callback execution traps instead of returning an error that
could commit retained ownership without stable publication. Pre-entry provider
rejection can return normally. Unwinding discards buffers and production rollback
belongs to the IC. Queries may reuse clean update buffers but discard all cache
backing on exit and never flush. Unscoped
raw mutation/growth invalidates clean aliases. Provider extraction and
root/provider teardown discard all Global cache backing before KEEP.
Mutation failures abort through the provider. There is no journal or local
transaction engine. Buffer remains reusable transient codec workspace.
The separate plain module lends native fixed-record references from checked
aligned buffers. Its unsafe Plain certificate requires initialized,
padding-free, pointer-free bytes, all bit patterns valid and a frozen physical/
endianness contract. Records must fit one segment allocation with alignment at
most eight. This is a native authoring proof separate from ordinary Codec records;
it supplies no new native stable Map or qualified Plain application
conversion/KEEP.
Codec fixes a compiled record size and exact positional byte interpretation.
Its required __CODEC_POLICY describes its own implementation. Derives hash
the selected declaration; manual codecs provide their complete encoder/decoder,
owning-value and helper recipe. Store's retained Record<Data> marks the complete
encoded payload and seeds its separate compiled codec closure. That closure
follows logical generic/field types and frames each codec's fixed size, without
interpreting saved bytes or recursively evaluating policy constants. An unused
Codec API on a native record contributes no codec policy. Erased fixed key/value
codecs also retain their concrete contracts. Custom Drop/comparator/serializer
and saved-Wire meaning remain explicit frozen-owner inputs; the closure does
not infer arbitrary business dependencies. Ordinary Store binding does not
hash this admission-only policy metadata.
HylixValue adds explicit owning-edge retirement. Ordinary decode, reads,
snapshot clone and Drop are inert. New resources start with one owner; insertion
transfers it, removal transfers it back, and unchanged record writeback does not
change ownership. Shared carriers add a second owning edge only through explicit
share/retain. Retiring the true owner recursively releases its compiled fields.
retain is never an automatic attach hook for record or collection writes.
The engine's derive crate implements Codec and HylixValue.
Collections owns the scoped field-transfer derive
cdk_rsx_collections::hylix::OwnedRecord.
These are trusted infrastructure contracts. A nonowning snapshot must not be retired or persisted as a second owning alias. Collections implement algorithms, sharing and transfer APIs; Store implements module ownership and lifetimes. The foundation deliberately has no per-entry reference graph or schema registry.
with_encoded initializes and lends only the used prefix of operation-local
stack bytes for fixed codecs up to 4 KiB, with one exact-size transient buffer
for larger values. Store frame publication and collection edit guards use it.
It preserves complete encoding
and writeback; it does not compare records or infer changed ownership.
Named-struct Codec derives also emit Record::FIELDS typed spans. Each token
uses the same declaration order and complete field codec as encode/decode and
preserves the original field visibility. It separately proves the current
native offset with offset_of!. Store's
memory.field(Record::FIELDS.counter).write() borrows that field of the current
frame value under the existing whole-module borrow. Field::then composes both
offsets; enums have no unconditional field projection. A token is not an
allocation capability and does not bypass generation, bounds or scope checks.
Store decodes a module once per frame and publishes each remaining dirty record
once into coherent arena bytes at successful frame finalization, before the
final segment flush. Collection row Edit guards continue to publish into those
bytes on Drop. Retirement/conversion consume the current module value before
freeing its allocation.
Codec::decode_slice and with_encoded_slice retain exact positional element
formats. Bytes decode through a bulk copy and encode by lending the original
input slice. Other nonzero-size elements use their ordinary compiled codecs;
partial records are rejected and zero-size elements require a separate count.
Collection guards retain a ContextLoan, separate from the short byte-provider
borrow. Each synchronous frame has a distinct epoch. An edit checks its epoch
before publishing, and frame finalization panics before returning any result if
guards remain. Native execution then stays quarantined until its test host
restores the complete snapshot and calls the unsafe rollback reset; IC execution
uses whole-message trap rollback. A stale guard never becomes valid in a later
frame, including one for a different arena.
License
This crate is available under the ICP3V License.