Buffered stable memory experiments

The qualified main result selects the fresh buffered arena with dirty-key finalization and dense buffers: 653,114,199,925 instructions /105 rounds, improved latency and separately checked native retained RRT. The typed-row lane was removed after a latency regression. Repeated stable API diagnostics are opt-in and separately measured; ordinary performance artifacts exclude them.

The target is lower complete metered instructions while keeping the application model: one root, private composable module memory, explicit conversions and safe KEEP upgrades. Stable memory remains the durable source of domain data. A bounded working set may use heap during execution; it must not quietly replace stable persistence with surviving ordinary heap objects.

The user requested two independent approaches, not a commitment to the current allocator, codecs, handle widths or access machinery:

Lane Hypothesis Initial implementation boundary
Current-code experiment Reusing typed collection records can avoid repeated searches, decoding and publication. Extend the existing scoped access model with bounded typed working records; keep callbacks, ownership and collection mutation coherent.
Fresh stable design A new buffered arena with direct offset views can amortize stable API copies and expose heap-like typed access. Fresh chunk buffers, dirty spans and allocator/access primitives, with an adapter for the real workload and a separate fixed-layout typed-record proof.

Both lanes start at 55f4f103. They have separate worktrees and build targets. They may replace prototype internals; there is no requirement to preserve the old stable format or keep an alternate implementation for compatibility.

Cost model

The fresh PocketIC pricing check confirms the aligned 64 KiB region geometry, including five-byte boundary crossings. First-access and first- dirty fees are execution-local, not paid anew for every stable API call. Each nonempty stable call still copies and pays 20 + requested_bytes; heap buffer access and writes matter independently. A wholly untouched stable source range can use the first-read bypass, so smaller or split reads are not automatically cheaper. Buffer size and read/write grouping must be measured.

The existing PocketIC native trace exports heap regions only. It does not expose stable mapping/dirty sets or repeated stable API calls. Report actual native trace counts, application-observed API counts/bytes and modeled stable effects as separate measurements. Never label their difference pure computation.

Evaluation

The qualified complete stable control uses 745,060,274,341 instructions over 123 rounds; retained control uses 105,264,735,031 over 49. Their exact identities and qualification are in the selected report.

Use an unchanged small four-canister heavy mix to reject poor candidates cheaply, then complete matched Heavy16 for promising integrations. Include frame-final publication, dirty-buffer flushing, callbacks and the normal maintenance tail in cost accounting. Preserve workflow, message, block and retry-byte accounting. Report latency and grown memory alongside instructions; do not add isolated savings or compare a diagnostic build with an ordinary build as a speedup.

Required correctness boundaries include read-your-writes, shared/mutable loan exclusion, retirement and reuse, no stale cached bytes after moving/freeing an allocation, query immutability and IC late-trap rollback. Buffers must have an explicit lifetime and flushing rule. A context epoch is not an allocation pin. Clean bounded cache entries may remain between calls when every access passes through the coherent backend and successful updates flush to stable before commit. Reopen/upgrade must discard or reconstruct that cache; losing it must not lose application state. This is an experimental option, not a requirement that every frame start cold. Compare its cold/warm costs and heap footprint. Direct typed views require initialized, aligned bytes and a proven physical type contract; ordinary Rust pointers and transient cache state are not stable data.

Focused native checks precede selected actual-Wasm lifecycle/upgrade gates. Clearly identify guarantees omitted by a standalone prototype. Successful microbenchmarks do not by themselves qualify production integration.