The problem
The interpreter created closures by carrying the surrounding environment. That preserves lexical scope, but retains bindings a lambda may never reference.
The goal was to reduce that overhead while preserving the language’s behavior.
The approach
I implemented recursive free-variable analysis over the expression tree, covering binding forms, nested lambdas, mutation, control flow, and function application. Closure creation then filtered the captured environment to the bindings relevant to that lambda.
Measurement
I added targeted checks for free-variable behavior and compared the modified interpreter with the original source implementation.
| Implementation | Runtime |
|---|---|
| Original | 5.14 s |
| Filtered closure environments | 1.35 s |
Approximately 74% less runtime on the assignment benchmark. Hardware details and run-to-run variance are not available, so this result should be treated as specific to that benchmark.
Constraints
The optimization depends on correct handling of scope and binding throughout the expression tree. Reducing the captured environment must not remove a binding the lambda can reference.
Source remains private in accordance with course policy.