Decision D004 · accepted
Memory: Perceus, plus a cycle collector over mutable cells only
A record of the language's design, as it stood on 2026-11-18: written for the people who build Diaspar, and kept as it was written. Its code is a sketch, not a program of the site, and an older record may show syntax that a later one changed. It names files of the language's source, which is not public yet.
Status: accepted (2026-11-15)
Decision
All values are managed by precise reference counting with reuse (Perceus, as in Koka and Lean 4). Global definitions are not counted: they live as long as the image does. For the few places where cycles can form, a small cycle collector looks only at mutable cells: atoms, channels and cowns (revised 2026-11-17: actors are a library over channels, D035).
How it works
Every value has a reference count. When it drops to zero, the memory is freed at once. When the count is 1, nobody else can see the value, so "change a copy" can happen in place. This is automatic copy-on-write.
(def v #[1 2 3])
(def w (conj v 4)) ; v is still in use, so conj copies
(defn build [n]
(loop [acc #[] i 0]
(if (= i n)
acc
(recur (conj acc i) (inc i))))) ; acc is unique, so conj appends in place, no copies
Why cycles are not the main problem
An immutable value cannot form a cycle: a new value can only refer to values that already exist. Cycles need mutation, so they appear only in known places:
- mutable cells (atoms): a cell points to a value that contains the same cell;
- channels and cowns (D035): a channel's buffer can hold a closure that holds the channel;
- references between actors: actors hold each other's addresses. An actor's mailbox is a channel, and actors never outlive the scope that started them, so such a cycle ends with its scope (D035);
- recursive local closures;
- global definitions in the REPL: not counted at all.
For cells a cycle collector that looks only at them is enough. CPython does this: reference counting plus a cycle collector over containers only. Ferron's runtime already had such a collector.
Comparison
| Perceus | Tracing collector | |
|---|---|---|
| When memory is freed | At once, predictably | When the collector decides |
| Pauses | None | Yes, or a complex incremental collector |
| In-place update when unique | Yes | No: the collector does not know the count |
| Cycles | Leak without a cycle collector | Not a problem |
| Runtime complexity | Increment, decrement, free | Stack maps, safepoints, write barriers; especially hard with fiber stacks and a C backend |
| Code outside "ordinary" Diaspar | Almost none | About the size of Go's runtime |
Why
- The restricted context (D002) shrinks to the allocator: no stack maps, safepoints, or write barriers.
- It implements the copy-on-write value semantics proven in Ferron directly.
- Lean 4 is self-hosted on this scheme, so it works for a compiler.
- The C backend (D003) and fibers (D006) do not need C to know where pointers live.
Rejected alternatives
- A tracing collector. Its only advantage is that cycles need no thought, but it makes the runtime large, breaks in-place update, and fits badly with a C backend and fibers.
- Ownership as in Rust or Carp. Hard to combine with a live REPL; Loon's inferred ownership is not yet proven.
Risks and open questions
- No measurements of Perceus in a live environment with redefinition were found. This is the main risk of the decision; check it on early prototypes.
- Values shared between OS threads need an atomic count. Proposed in D028 and D035: counts are plain until a value is published to another fiber; then it is marked shared, and its counts are atomic.
- When to run the cycle collector: by a threshold of cells, by a timer, or explicitly.