feat: add Perceus-inspired reference counting infrastructure

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>
This commit is contained in:
2026-02-14 12:27:54 -05:00
parent 2a2c6d2760
commit 56f0fa4eaa
3 changed files with 446 additions and 42 deletions

View File

@@ -164,27 +164,26 @@ result->length = nums->length;
## Current Limitations
### 1. Memory Leaks
### 1. Memory Management (Partial RC)
**Everything allocated is never freed.** This includes:
- Closure environments
- ADT values
- List elements and arrays
- Strings from concatenation
RC infrastructure is implemented but not fully integrated:
- ✅ Lists, boxed values, and strings use RC allocation
- ✅ List operations properly incref shared elements
- ⏳ Automatic decref at scope exit (not yet implemented)
- ⏳ Closures and ADTs still leak
This is acceptable for short-lived programs but not for long-running services.
**Current state:** Memory is tracked with refcounts, but objects are not automatically freed at scope exit. This is acceptable for short-lived programs but not for long-running services.
### 2. Limited Effects
### 2. Effects ✅ MOSTLY COMPLETE
Only `Console.print` is supported, hardcoded to `printf`:
All major effects are now supported:
- `Console` (print, readLine)
- `Random` (int, float, bool)
- `Time` (now, sleep)
- `File` (read, write, append, exists, delete, isDir, mkdir)
- `Http` (get, post, put, delete)
```c
static void lux_console_print(LuxString msg) {
printf("%s\n", msg);
}
```
Other effects (File, Http, Random, etc.) are not yet implemented in the C backend.
All effects use evidence passing for O(1) handler lookup.
### 3. If/Else Side Effects
@@ -275,7 +274,7 @@ See [docs/EVIDENCE_PASSING.md](EVIDENCE_PASSING.md) for details.
## Future Roadmap
### Phase 4: Perceus Reference Counting
### Phase 4: Perceus Reference Counting 🔄 IN PROGRESS
**Goal:** Deterministic memory management without GC pauses.
@@ -284,7 +283,18 @@ Perceus is a compile-time reference counting system that:
2. Detects when values can be reused in-place (FBIP)
3. Guarantees no memory leaks without runtime GC
**Example - reuse analysis:**
**Current Status:**
- ✅ RC infrastructure (header, alloc, incref/decref, drop)
- ✅ Lists use RC allocation with proper element incref
- ✅ Boxed values (Int, Bool, Float) use RC allocation
- ✅ Dynamic strings use RC allocation
- ⏳ Automatic decref at scope exit (TODO)
- ⏳ Closure RC (TODO)
- ⏳ Last-use optimization (TODO)
See [docs/REFERENCE_COUNTING.md](REFERENCE_COUNTING.md) for details.
**Example - reuse analysis (future):**
```lux
fn increment(xs: List<Int>): List<Int> =
List.map(xs, fn(x) => x + 1)