lumbda/c
russell@unturf.com 78fbd906f0
python + c bytecode VM: OP_SELF_TAIL_CALL frame-unwind fix
Both bytecode VMs had a latent O(n^2) defect on self-recursive tail
calls invoked from inside let/let*/letrec/letrec*/do bodies. The
self-tail-call op assumed reusing "current env" was safe, but current
env was the innermost let* frame, not the lambda body env. Each iter
pushed a fresh let* frame on top (PUSH_ENV at compile site), the
self-tail-call rebound params into that frame & jumped to ip=0 without
unwinding. Env chain grew linearly with iters; every var lookup walked
O(n) chain; effective O(n^2) behaviour.

Symptom observed 2026-06-14: 156k circ-ops walk hung > 5min instead of
1.4s. K=5 doctrine reducers ran 30+ runaway lumbda procs at 99% CPU
across multiple `make sweep-doctrine` invocations before we tracked
it back to language layer (initially misdiagnosed as K=5 substrate).

Fix: track scope depth at compile time on CodeObj (scope_depth bumped
on PUSH_ENV emit, decremented on POP_ENV emit). Record self_base at
lambda body entry (0 unless internal defines pushed a frame). At
self-tail-call emit, encode pops_needed = scope_depth - self_base in
the op arg. Runtime handler unwinds that many env frames before
rebinding params + jumping to ip=0.

Tree-walker (c/lumbda without --fast) already worked - it walks the
ast & lets recursion clean up frames naturally. Asm tier also fine -
no self-tail-call op, uses different lambda-call convention.

Verification:
  python tier: 571 tests PASS, our 100k let* repro 1.04s wall (was infinite)
  c tier:      205 tests PASS, same repro 0.05s wall (was infinite)
  asm tier:    158 tests PASS (no fix needed, never had the bug)

Portal-resume backwards-compat: pre-fix portals stored OP_SELF_TAIL_CALL
arg as 2-tuple. Deserializer fills pops=0 when 'pops' key is absent,
so an old portal resumes at correct behaviour at the cost of slow walk
on its very next self-tail-call body (no worse than pre-fix).

Memory note saved at reference_lumbda_let_star_in_tail_loop in our
foxhop blackops memory for future agents.
2026-06-14 14:58:30 -04:00
..
.gitignore rename: uncommonlisp -> lumbda throughout the repo 2026-04-19 10:20:11 -04:00
bench.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
bignum.c c-tier bignum — arbitrary-precision integers unblock secp256k1 widths 2026-06-06 20:23:37 -04:00
builtins.c fork-self + waitpid-nonblock + exit-immediate + sleep primitives across c-tier + python-tier 2026-06-11 09:24:51 -04:00
eval.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
gc.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
jit.c rename: uncommonlisp -> lumbda throughout the repo 2026-04-19 10:20:11 -04:00
jit.h rename: uncommonlisp -> lumbda throughout the repo 2026-04-19 10:20:11 -04:00
lumbda.h python + c bytecode VM: OP_SELF_TAIL_CALL frame-unwind fix 2026-06-14 14:58:30 -04:00
main.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
Makefile c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
portal.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
printer.c c-tier bignum — arbitrary-precision integers unblock secp256k1 widths 2026-06-06 20:23:37 -04:00
reader.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
test.c c: precise GC tracing for NaN-boxed Values 2026-06-07 17:18:45 -04:00
types.c c tier: rationals on int/int division — matches python lumbda 2026-06-14 14:23:54 -04:00
vm.c python + c bytecode VM: OP_SELF_TAIL_CALL frame-unwind fix 2026-06-14 14:58:30 -04:00