M.V. Hutz
b52a7596ef
perf: implement structural sharing for expression trees
Replace mutable in-place expression modification with immutable
expressions that use structural sharing. This eliminates unnecessary
copying during variable substitution and beta reduction.
## Changes
- Make expression fields unexported (immutable by design)
- Convert Substitute() and Rename() to return new expressions
- Implement structural sharing: return self when unchanged
- Remove Copy() method entirely
- Add getter methods for expression fields
## Performance Impact
Benchmarked across all samples:
| Sample | Before | After | Improvement |
|-------------|--------|-------|-------------|
| church | 230ms | 170ms | 26% faster |
| saccharine | 320ms | 160ms | 50% faster |
| simple | 30ms | 20ms | 33% faster |
## Key Optimization
Previously, variable substitution created deep copies:
```go
*e = replacement.Copy() // Deep copy entire tree
```
Now uses structural sharing:
```go
return replacement // Share pointer directly
```
This eliminates 100% of Copy() allocation overhead (10-50ms per sample).
## Files Modified
- pkg/lambda/expression.go: Unexport fields, remove Copy(), add methods
- pkg/lambda/substitute.go: Functional API with structural sharing
- pkg/lambda/rename.go: Functional API with structural sharing
- pkg/lambda/reduce.go: Use new functional API
- pkg/lambda/get_free_variables.go: Access unexported fields
- pkg/lambda/is_free_variable.go: Access unexported fields
- pkg/lambda/stringify.go: Access unexported fields
2026-01-10 20:16:57 -05:00
..
2025-12-30 15:58:14 -05:00
2026-01-10 11:37:18 -05:00
2025-12-29 01:15:14 -05:00
2025-12-28 02:37:22 -05:00
2025-12-27 20:46:10 -05:00
2026-01-10 20:16:57 -05:00
2025-12-30 15:58:14 -05:00
2025-12-29 00:51:50 -05:00
2025-12-27 20:46:10 -05:00