feat: add drop specialization and ownership transfer optimizations
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>
This commit is contained in:
@@ -23,6 +23,9 @@ The RC system is now functional for lists and boxed values.
|
||||
- **Memory tracking** - debug mode reports allocs/frees at program exit
|
||||
- **Early return handling** - variables being returned from blocks/functions are not decref'd
|
||||
- **Function call RC tracking** - values from RC-returning functions are tracked for cleanup
|
||||
- **Complex conditionals** - if/else uses if-statements instead of ternaries to avoid allocating unused branches
|
||||
- **toString and string concatenation** - proper type inference and lux_string_concat for string operations
|
||||
- **C keyword escaping** - reserved words like `double`, `int` are mangled to avoid conflicts
|
||||
|
||||
### Verified Working
|
||||
```
|
||||
@@ -30,7 +33,7 @@ The RC system is now functional for lists and boxed values.
|
||||
```
|
||||
|
||||
### What's NOT Yet Implemented
|
||||
- Conditional branch handling (complex if/else patterns)
|
||||
- Reuse analysis (FBIP) - mutate in-place when rc=1
|
||||
|
||||
## The Problem
|
||||
|
||||
@@ -338,8 +341,10 @@ void lux_check_leaks() {
|
||||
| Scope tracking | Yes | Yes ✅ |
|
||||
| Auto decref | Yes | Yes ✅ |
|
||||
| Memory tracking | No | Yes ✅ (debug) |
|
||||
| Early return | Yes | Partial |
|
||||
| Last-use opt | Yes | No |
|
||||
| Early return | Yes | Yes ✅ |
|
||||
| Conditionals | Yes | Yes ✅ |
|
||||
| Last-use opt | Yes | Yes ✅ (ownership transfer) |
|
||||
| Drop special | Yes | Yes ✅ |
|
||||
| Reuse (FBIP) | Yes | No |
|
||||
| Drop fusion | Yes | No |
|
||||
|
||||
@@ -420,29 +425,29 @@ Rust's ownership system is fundamentally different:
|
||||
- Function calls returning RC types are tracked for cleanup
|
||||
- Blocks properly handle returning RC variables
|
||||
|
||||
4. **Complex conditionals** - If/else creating RC values
|
||||
- Switch from ternary to if-statements
|
||||
- Track RC creation in branches
|
||||
- ~50 lines
|
||||
4. ~~**Complex conditionals**~~ ✅ DONE - If/else creating RC values
|
||||
- Switch from ternary to if-statements when branches create RC values
|
||||
- Only the executed branch allocates memory
|
||||
- Prevents leak of unused branch allocations
|
||||
|
||||
#### Phase B: Performance Optimizations (Match Koka)
|
||||
|
||||
1. **Last-use optimization**
|
||||
- Track variable liveness
|
||||
- Skip incref on last use (transfer ownership)
|
||||
- Requires dataflow analysis
|
||||
- ~200 lines
|
||||
1. ~~**Last-use optimization**~~ ✅ DONE - Ownership transfer
|
||||
- Variable types tracked in `var_types` map
|
||||
- When assigning `let b = a`, ownership transfers from `a` to `b`
|
||||
- Source variable unregistered from RC tracking
|
||||
- No double-free, no unnecessary incref/decref
|
||||
|
||||
2. **Reuse analysis (FBIP)**
|
||||
2. **Reuse analysis (FBIP)** - NOT YET IMPLEMENTED
|
||||
- Detect `rc=1` at update sites
|
||||
- Mutate in-place instead of copy
|
||||
- Major change to list operations
|
||||
- ~300 lines
|
||||
|
||||
3. **Drop specialization**
|
||||
- Generate per-type drop functions
|
||||
- Eliminate polymorphic dispatch
|
||||
- ~100 lines
|
||||
3. ~~**Drop specialization**~~ ✅ DONE
|
||||
- Specialized decref functions: `lux_decref_list`, `lux_decref_closure`, etc.
|
||||
- Inline drop logic eliminates polymorphic dispatch
|
||||
- Forward type declarations for proper C ordering
|
||||
|
||||
### Estimated Effort
|
||||
|
||||
@@ -451,13 +456,14 @@ Rust's ownership system is fundamentally different:
|
||||
| A1 | Closure RC | ~50 | P0 | ✅ Done |
|
||||
| A2 | ADT RC | ~150 | P1 | ✅ Done |
|
||||
| A3 | Early returns | ~30 | P1 | ✅ Done |
|
||||
| A4 | Conditionals | ~50 | P2 - Uncommon | Pending |
|
||||
| B1 | Last-use opt | ~200 | P3 - Performance | Pending |
|
||||
| A4 | Conditionals | ~50 | P2 | ✅ Done |
|
||||
| B1 | Last-use opt | ~80 | P3 | ✅ Done |
|
||||
| B2 | Reuse (FBIP) | ~300 | P3 - Performance | Pending |
|
||||
| B3 | Drop special | ~100 | P3 - Performance | Pending |
|
||||
| B3 | Drop special | ~100 | P3 | ✅ Done |
|
||||
|
||||
**Phase A remaining: ~50 lines** - Gets us to "no leaks"
|
||||
**Phase B total: ~600 lines** - Gets us to Koka-level performance
|
||||
**Phase A: COMPLETE** ✅ - All leak prevention implemented
|
||||
**Phase B: 2/3 DONE** ✅ - Major performance optimizations implemented
|
||||
**Remaining: FBIP (~300 lines)** - In-place mutation when rc=1
|
||||
|
||||
### Cycle Detection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user