Commit 84cecbf
committed
runtime: closure-cycle collector — honest env refcounts + registry mark-sweep
The env<->fn cycle (docs/CLOSURE_CYCLE_GC.md) is now reclaimed, both
mid-run and at exit, following the staged plan in the design note.
Stage 1 — Env lifetime is a real refcount. env_refcount counts every
owner: creator/frame, closures (make_fn), child envs (the parent link
is an owned edge), and a chunk's parked env_cache. env_free's
captured-gated semantics are replaced by env_incref/env_decref;
LOOP_ENV_FRESH/END move the frame's ref explicitly (also fixing the
latent fn_env leak on early return from a loop scope); vm_park_call_env
requires refcount == 1 and the parked env keeps its owned parent ref,
making the take-side parent compare dangling-pointer-free. OP_IMPORT
releases the module env's creator ref so fn-free modules are reclaimed.
Stage 2 — the collector. Captured envs register in a per-thread
intrusive list at OP_CLOSURE (global env excluded); collection triggers
from the registration threshold (zero cost on dispatch/call hot paths)
and once at exit. The universe is everything reachable from registered
envs over owned edges only — env slots, parent, fn->closure, fn->chunk,
chunk->functions[], chunk->env_cache, list/dict elements — and roots
are derived from refcounts alone: any node whose rc exceeds its
in-universe reference count has an external holder, so no VM root-set
enumeration exists to get wrong. Accounting mismatches abort the
collection (leak, never free). Unmarked nodes are pinned, edge-cleared,
and released through the ordinary destructors. gc_collect_at_exit
additionally snapshots global container bindings (pinned, accounted) so
global-scope pure value cycles — a list appended to itself — die at
teardown. spawn() drains the registry and disables collection; spawned
programs keep the previous tolerated-leak behavior, still visible to
LSan.
Results: 500 discarded closures leak 0 allocations (was 6,004); a 100k
closure-churn loop runs ~40% faster with peak RSS 124 MB -> 4.3 MB;
bench_perf / bench_dmg_shape / bench_idxset are flat. Suite: 1832/1832
release and ASan (detect_leaks=1); tolerated-leak tally 28 -> 13, every
remaining report byte-identical to the pre-collector baseline
(spawn-thread programs + pre-existing non-closure shapes); fuzz 44/44.
test_closure_cycles.eigs (17 checks, incl. dict-routed cycles) is now
gated strictly leak-clean — section [87] opts out of rc_ok's tolerance,
so a collector regression fails CI instead of being tallied.
https://claude.ai/code/session_01UiKD8EDViybJuxHbHLr9TW1 parent 7d3bccf commit 84cecbf
15 files changed
Lines changed: 890 additions & 220 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
5 | 41 | | |
6 | 42 | | |
7 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
61 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
62 | 77 | | |
63 | 78 | | |
64 | 79 | | |
| |||
121 | 136 | | |
122 | 137 | | |
123 | 138 | | |
124 | | - | |
| 139 | + | |
125 | 140 | | |
126 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
127 | 149 | | |
128 | 150 | | |
129 | 151 | | |
| |||
156 | 178 | | |
157 | 179 | | |
158 | 180 | | |
159 | | - | |
160 | | - | |
161 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
162 | 185 | | |
163 | 186 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | 187 | | |
169 | 188 | | |
170 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
208 | 223 | | |
209 | 224 | | |
210 | 225 | | |
| |||
0 commit comments