When rc=1 at update sites, mutate in-place instead of allocating new:
List.reverse:
- Swap element pointers in-place instead of creating new list
List.take:
- Truncate list in-place, decref dropped elements
List.drop:
- Shift elements to front in-place, decref dropped elements
List.map:
- Mutate elements in-place, decref old values before storing new
List.filter:
- Filter in-place by shifting kept elements, decref filtered-out elements
All operations check LUX_RC_HEADER(list)->rc == 1 at runtime and
fall back to allocation when rc > 1 (list is shared).
This completes Phase B performance optimizations:
- B1: Last-use optimization (ownership transfer) ✅
- B2: Reuse analysis (FBIP) ✅
- B3: Drop specialization ✅
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Phase B Performance Optimizations:
Drop Specialization:
- Add specialized decref functions: lux_decref_list, lux_decref_closure,
lux_decref_string, lux_decref_boxed
- Inline drop logic eliminates polymorphic dispatch through lux_drop
- Forward type declarations (typedef struct X_s X) for proper C ordering
Ownership Transfer (Last-Use Optimization):
- Track variable types in var_types HashMap for type inference
- When assigning let b = a where a is RC-tracked:
- Unregister source variable from RC cleanup
- Register destination variable instead
- Prevents double-free and eliminates unnecessary incref/decref pairs
Also:
- Fix type inference for variable references in infer_expr_type
- Add is_rc_tracked() and unregister_rc_var() helper functions
- Update REFERENCE_COUNTING.md with Phase B progress
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add pop_rc_scope_except() to skip decref'ing returned variables
- Block expressions now properly preserve returned RC variables
- Function returns skip cleanup for variables being returned
- Track function return types for call expression type inference
- Function calls returning RC types now register for cleanup
- Fix main() entry point to call main_lux() when present
Test result: [RC] No leaks: 17 allocs, 17 frees
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
ADT values with pointer fields (like recursive Tree types) now properly
manage memory:
- Assign unique type tags (starting at 100) to each ADT type
- Track which ADTs have pointer fields that need cleanup
- Generate lux_drop_adt() function with per-ADT drop logic
- Allocate ADT pointer fields with lux_rc_alloc instead of malloc
- Track ADT variables with pointer fields in scope
- Emit field cleanup code at scope exit (switch on tag, decref fields)
Test results:
- ADT test: [RC] No leaks: 6 allocs, 6 frees
- List test: [RC] No leaks: 31 allocs, 31 frees
- Closure test: [RC] No leaks: 8 allocs, 8 frees
- All 263 tests pass
Remaining: early returns, complex conditionals.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Closures and their environments are now properly reference-counted:
- Allocate closures with lux_rc_alloc(sizeof(LuxClosure), LUX_TAG_CLOSURE)
- Allocate environments with lux_rc_alloc(sizeof(LuxEnv_N), LUX_TAG_ENV)
- Enable Lambda in expr_creates_rc_value() to track closure variables
- Add lux_decref() after List higher-order operations (map, filter, fold,
find, any, all) to clean up inline lambdas
Test results:
- Closure test: [RC] No leaks: 8 allocs, 8 frees
- List RC test: [RC] No leaks: 31 allocs, 31 frees
- All 263 tests pass
Remaining for full memory safety: ADT RC, early returns, conditionals.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- C_BACKEND.md: Update memory management from "Leaks" to "Scope-based RC",
update comparison tables with Koka/Rust/Zig/Go
- LANGUAGE_COMPARISON.md: Add status column to gap tables, add RC row
- OVERVIEW.md: Add C backend RC to completed features, update limitations
- REFERENCE_COUNTING.md: Add "Path to Koka/Rust Parity" section with:
- What we have vs what Koka/Rust have
- Remaining work for full memory safety (~230 lines)
- Performance optimizations for Koka parity (~600 lines)
- Cycle detection strategy
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add scope tracking for reference-counted variables in the C backend:
- Add RcVariable struct and rc_scopes stack to CBackend
- Track RC variables when assigned in let bindings
- Emit lux_decref() calls when scopes exit (functions, blocks)
- Add memory tracking counters (alloc/free) for leak detection
- Fix List.filter to incref elements before copying (prevents double-free)
- Handle return values by incref/decref to keep them alive through cleanup
The RC system now properly frees memory at scope exit. Verified with
test showing "[RC] No leaks: 28 allocs, 28 frees".
Remaining work: early returns, complex conditionals, closures, ADTs.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements Phase 1-3 of the RC system for automatic memory management:
- Add LuxRcHeader with refcount and type tag for all heap objects
- Add lux_rc_alloc, lux_incref, lux_decref, and lux_drop functions
- Update list allocation to use RC (lux_list_new uses lux_rc_alloc)
- List operations (concat, reverse, take, drop) now incref shared elements
- Update boxing functions (box_int, box_bool, box_float) to use RC
- String operations (concat, int_to_string, readLine) return RC strings
- File and HTTP operations return RC-managed strings
The infrastructure is ready for automatic decref insertion at scope exit
(Phase 4) and closure RC (Phase 5) in future work.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>