Atoll has no free, no new, no allocator argument and no lifetime
annotation. It also has no tracing collector on the path your program
runs down. What it has is four mechanisms with disjoint jobs:
| Mechanism | Job | Who decides |
|---|---|---|
| Moves | transfer ownership of a buffer | the type — a value that owns one moves, and its first name is gone |
| Borrows | grant access without ownership | you — p &T on the parameter |
| Reference counting | keep a shared block alive | the compiler |
| Cycle collection | reclaim what counting cannot | the compiler |
Each does one thing. That is the whole design: an earlier version of this compiler had one mechanism inferring all four, and the inference disagreed with itself — a struct that was boxed to one layer and inline to another, a value lent to a callee and then released as though it were owned.
struct Node {
v int
next ^Node?
}
fn sum(n &Node) int {
return n.v + match n.next { Some(k) => sum(k) None => 0 }
}
fn main() int {
mut names: []string = ["a"]
held: []string = names // moves — `names` is gone
chain: ^Node = ^Node { v: 20, next: ^Node { v: 2, next: None } }
return sum(chain) + held.len()
}Nothing in that program says how long anything lives. held owns the list and
gives it back; chain owns two blocks and n &Node says sum will not keep
what it is handed.
What you write, and what the compiler works out
The split is not arbitrary. A fact is left to the compiler only when a wrong answer costs performance, and written down when a wrong answer costs correctness.
| Decided for you | A wrong answer costs |
|---|---|
| where a release goes | nothing — it is placed from liveness |
| whether a block is on a cycle | a wasted slot in the collector’s buffer |
| You write it | A wrong answer would cost |
|---|---|
^T — this value gets its own block |
layers disagree about a type’s width |
p &T — this parameter will not keep it |
a dangling pointer |
T? — this may be absent |
a null read as a value |
&T on a field — this field will not keep it |
a leak, or a double release |
Every row in the first table fails toward correct and slower. Every row in the second was inferred once, and each one has a defect behind it.
Two things that decide whether ordinary code is fast are on neither table,
because there is nothing to decide. A borrow never counts: p &T is one
address, and whatever lent it keeps the target alive for as long as the borrow
is used. A write never copies a block behind the program’s back: the block
a write reaches is unique, or the write is refused while a borrow of it is
still to be used, and .clone() is how to ask for a second one.
What crossing a call costs
Nothing about a call is deferred to a runtime decision, so the cost is readable off the signature.
| The parameter is | What travels |
|---|---|
| a scalar | the value |
an owned multi-word value — a List, a string |
its lanes, as separate wasm values; the callee’s parameter is its own copy |
| a large value — past four lanes | one address and a block copy, which is one instruction rather than thirteen |
borrowed (p &T) |
one address; the caller keeps the storage across the call |
a &mut self receiver |
one address, always. Never a copy with a write-back |
The middle row is the default rather than an optimization, and what it buys beyond matching the machine is that a callee’s parameter is not a second name for the caller’s storage. One buffer under two names across a call boundary is precisely what a borrow declares, so the borrow row is where it belongs.
The receiver row is why c.bump() works. Under a copy-in/copy-out convention
every mutation of a multi-field value has to write every field back to wherever
the receiver lives, and a partial write-back tears the value — new data,
stale len, reads past the end of a block. With a pointer there is nothing to
write back and nothing to tear.
What none of this buys is fewer frames: an owned parameter’s lanes are stored into a frame slot on entry, because the body still works through an address.
The unit is the block
There is one reference count per block, and it lives with the block rather than with any value naming it. Two consequences are language rules and not implementation detail:
A reference to one element pins the whole buffer. There is no sub-block
count and no per-view count, so &xs[0] keeps every element alive, not one.
A change ends a reference into a list. A reference borrows the list it was
taken from, and a use of it after the list changed is refused (ATOLL1097), to
read or to write. See
Values.
Read next
- Destruction — where a release goes, what it
does, and what
Dropis for. - References — where a
^Tmay go, and where it may not. - Cycles — what makes one, what collects it, and when.
The surface these pages assume is on two other chapters:
Values for moves and clone, and
Pointers for ^T, & and x.^. For the
mechanism underneath — counts, walks, trial deletion, the allocator — see
Ownership.