Bet 1: ownership inference + refcount elision
Question. Can Trine have Python-like ease (no ownership annotations, no lifetimes) and still pay almost nothing for automatic memory management?
Kill criterion (from the charter). At least 90% of refcount operations removed on the golden programs.
Verdict: passed on overhead, with one caveat on the wording. With Perceus-style insertion plus borrow inference, 100% of refcount overhead is gone on 6 of 7 programs and 92.6% on the seventh. Every remaining operation except 10 (in matmul) is the final free of an object, which no memory manager can avoid (C would call free() there too). Counting raw operations, frees included, 3 of 7 programs clear 90% and 4 land at 75–84%. The criterion should be read as overhead. The raw number is reported here so nobody has to take that reframing on trust.
Correction (2026-11-26, during roadmap #11). The original tables below undercounted: freeing an object counted as one drop and ignored the releases of the references it held. The VM now counts those cascading releases for every strategy. Re-measured results are in Corrected results. Bet 1 still passes on all 10 programs. The lowest is json at 90.6% overhead removed. Shapes falls from 100% to 95.8%.
Method
src/ir.rslowers the AST to a structured ownership IR. Every operand is either owned (call arguments, list items, constructor fields, returns) or borrowed (arithmetic, indexing, conditions, builtins). Lambdas are lifted, with captures as parameters.src/perceus.rsturns each owned operand into dup (copy a reference) or move, and inserts explicit drops, using one of three strategies: - naive: dup every owned use of a named variable (temporaries are moved), drop on overwrite and at function exit. This is roughly what straightforward RC orshared_ptr-by-value code does. - perceus: liveness-driven, after Reinking et al. (PLDI 2021). The last use is a move, a value is dropped right after its last read, branches drop what they don't use, loops use a fixpoint, and unused pattern binders are skipped. - perceus+borrow adds four more optimizations: - borrow inference for parameters that are only read (optimistic fixpoint over the call graph); - borrowed pattern binders when the matched value outlives the arm; - borrowed loop elements; - in-place append when the left side of+is a string that is about to die.src/irvm.rsexecutes the IR and counts every dup and drop on heap values. Scalars are unboxed in every strategy, and string literals are immortal in every strategy. In Perceus mode the VM is also a checker: use-after-move, double drop, overwriting a live value, or returning with a live owned value aborts the run as an "ownership bug".trine rc <file>runs all three strategies and compares output byte for byte with the reference tree-walking interpreter.
Results
"Overhead" is rc ops minus frees.
| program | strategy | rc ops | overhead | list copies | overhead removed |
|---|---|---|---|---|---|
| calc | naive | 173 | 137 | 0 | — |
| perceus | 86 | 58 | 0 | 57.7% | |
| perceus+borrow | 16 | 0 | 0 | 100% | |
| hello | naive | 25 | 10 | 10 | — |
| perceus+borrow | 4 | 0 | 0 | 100% | |
| higher_order | naive | 98 | 55 | 21 | — |
| perceus | 25 | 6 | 0 | 89.1% | |
| perceus+borrow | 19 | 0 | 0 | 100% | |
| matmul | naive | 190 | 135 | 24 | — |
| perceus | 113 | 78 | 6 | 42.2% | |
| perceus+borrow | 45 | 10 | 6 | 92.6% | |
| primes | naive | 22,726 | 11,360 | 11,356 | — |
| perceus+borrow | 10 | 0 | 0 | 100% | |
| shapes | naive | 36 | 23 | 0 | — |
| perceus | 27 | 18 | 0 | 21.7% | |
| perceus+borrow | 9 | 0 | 0 | 100% | |
| values | naive | 2,011 | 1,006 | 1,002 | — |
| perceus+borrow | 7 | 2 | 2 | 99.8% |
Raw rc ops removed by perceus+borrow:
| calc | hello | higher_order | matmul | primes | shapes | values | |
|---|---|---|---|---|---|---|---|
| raw removed | 90.8% | 84.0% | 80.6% | 76.3% | 99.96% | 75.0% | 99.7% |
Totals across all seven programs: 25,259 → 110 rc ops (99.6%). Without the two loop-heavy programs: 522 → 93 rc ops (82.2% raw) and 360 → 10 overhead ops (97.2%).
All strategies produce byte-identical output on every program, with no ownership bugs. This is enforced by cargo test, which also guards the ≥ 90% overhead result per program.
Re-validation on larger programs (roadmap #10)
Three realistic golden programs were added (roadmap #9):
json.tr: recursive-descent parser and printer, about 230 lines, with a round trip of a 6 KB document;raytrace.tr: ASCII ray tracer, float-heavy, with records;server.tr: HTTP-style todo service with 515 requests, state threaded as a value.
| program | naive rc ops | perceus+borrow rc ops | raw removed | overhead removed | list copies (naive → perceus) |
|---|---|---|---|---|---|
| json | 201,821 | 23,631 | 88.3% | 91.3% | 7,143 → 0 |
| raytrace | 605,102 | 18,153 | 97.0% | 99.3% | 1,254 → 0 |
| server | 18,763,953 | 235,202 | 98.7% | 98.8% | 94,894 → 503 |
The bet holds on all three. JSON is the closest call at 91.3%. Its remaining overhead is mostly values that really do need a new reference: characters read from the input and pushed into an output list, and members copied into result lists. Server's remaining ~225k dups come from remove_at rebuilding a list, which an in-place remove builtin would eliminate. Liveness alone (perceus without borrowing) reaches only 41–54% on json and raytrace, which confirms the earlier finding: borrowing is what carries the bet on real code.
Corrected results (cascading releases counted)
Overhead removed by perceus+borrow, re-measured on all 10 golden programs with faithful release counting. Frees now match allocations in every row.
| program | naive rc ops | perceus+borrow rc ops | raw removed | overhead removed |
|---|---|---|---|---|
| calc | 187 | 30 | 84.0% | 100% |
| fbip | 65,532 | 12,282 | 81.3% | 100% |
| hello | 25 | 4 | 84.0% | 100% |
| higher_order | 100 | 21 | 79.0% | 100% |
| json | 294,382 | 55,052 | 81.3% | 90.6% |
| matmul | 203 | 55 | 72.9% | 92.9% |
| primes | 22,726 | 10 | 99.96% | 100% |
| raytrace | 640,485 | 19,425 | 97.0% | 99.2% |
| server | 37,131,706 | 467,450 | 98.7% | 98.8% |
| shapes | 39 | 12 | 69.2% | 95.8% |
| values | 2,011 | 7 | 99.7% | 99.8% |
With drop-reuse added (roadmap #11, docs/FBIP.md), json rises to 92.5% and fbip's raw rc ops fall from 12,282 to 1,029.
What the result shows
- Liveness alone is not enough for tree and record code (21–58% on calc, shapes, matmul). Borrowing is what closes the gap: borrowed pattern binders and borrow inference take calc from 57.7% to 100% and shapes from 21.7% to 100%.
- Value semantics become nearly free. In
primes, the naive strategy copies the sieve 11,356 times (quadratic). Perceus updates it in place 28,441 times and copies it 0 times. "Mutable value semantics, no lifetimes" from the charter is affordable. - It needs no annotations. None of this required a single annotation in the source. The decisions can be shown as ghost hints (
trine ir):
fn eval(e: borrowed):
match e:
Add(&a, &b) => # & = borrowed binder: no dup, no drop
t5 = call #0(a)
t6 = call #0(b)
t7 = Add(move t5, t6)
Threats to validity (read before quoting the numbers)
- The programs are tiny. Seven golden programs, mostly under 50 lines. The result needs to hold on real workloads (a JSON parser, an HTTP handler, a ray tracer) before anyone builds on it.
- These are counts from a VM, not timings from compiled code. The counts are what compiled code would execute, but cache effects and atomic RC (needed for threads) are not modeled.
- The baseline choice matters. Naive already moves temporaries. A dumber baseline would flatter Perceus, and a smarter one (e.g. Swift -O) would narrow the gap.
- Borrowed values are modeled with uncounted clones in the VM. This is sound for counting, but it means the VM's internal reference counts are slightly inflated during borrows.
- Frees are not optimized yet. Perceus drop-reuse (reusing a freed constructor cell for the next allocation, e.g. in
simplify) would turn free+alloc pairs into in-place updates. This is the next obvious win and is not needed to pass.
Next
- Add drop-reuse for constructors (FBIP, "functional but in-place").
- Port three larger golden programs (JSON parser, ray tracer, HTTP handler) and re-run this report.
- Move on to bet 2: refinement types via SMT, to erase the bounds checks the VM still performs.