Memory Model Deep-dive
Current value behavior and the planned tiered memory model with COW, optimized RC, cycles, ownership, and arenas.
The current Stage-0 docs show observable value behavior: immutable bindings, immutable lists, records as values, and
mutable opt-in collections. design/MEMORY_MODEL.md describes the planned memory model for the compiler/runtime.
1. Current observable behavior
Bindings are immutable by default. Lists and records behave as values: update helpers return new values rather than mutating the original.
# Source: .sources/vela/docs/SPEC_BY_EXAMPLE.md
xs = [10, 20, 30]
print(push(xs, 40))
print(xs)
r = {b: 2, a: 1}
print(set(r, "c", 3))
print(r)
Mutable arrays and dictionaries are explicit.
a = array_from([1, 2, 3])
array_set(a, -1, 99)
array_push(a, 4)
print(to_list(a))
2. Planned Tier 0
The memory model design calls the default tier "Tier 0." Its intended properties are:
- value semantics as the user-facing model,
- copy-on-write for large aggregates,
- optimized reference counting,
- automatic cycle collection for cyclic-capable structures,
- no requirement for beginners to write ownership annotations.
The cost model is intended to make assignment cheap by copying a handle, sharing read-only buffers, and copying only when a shared value is mutated.
3. Planned RC optimizations
The design names four optimization families:
- compile-time retain/release elision,
- move-on-last-use instead of retain/release pairs,
- cursor inference for loops and traversals,
- Perceus-style reuse analysis for unique dying cells.
These are compiler/runtime design goals, not Stage-0 interpreter guarantees.
4. Planned cycle handling
Reference counting alone leaks cycles. The design proposes an ORC-style cycle collector based on trial deletion, with
collector code emitted only for types that can participate in cycles. The goal is automatic cleanup without requiring
ordinary Tier-0 users to choose weak or unowned references.
5. Planned Tier 1
Tier 1 is opt-in and aimed at hot paths and library code. The planned surface includes:
own Tfor ownership transfer,&Tfor shared borrows,&mut Tfor exclusive mutable borrows,region/ arena allocation for bulk allocation and bulk free,purefunctions for read-only analysis and optimization.
The important design point is that Tier 0 and Tier 1 are planned to share one representation. Crossing the boundary should adjust compiler bookkeeping, not marshal data into a different object model.