← All work

uScheme optimization

Keeping only the environment a closure actually needs.

Programming languages · Standard ML · Course project

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.

01 / AnalyzeExpression treeFind the lambda’s free variables.
02 / RetainReferenced bindingsFilter the surrounding environment.
03 / CreateSmaller closurePreserve lexical scope.
A conceptual view of closure creation, without reproducing the course implementation.

Measurement

I added targeted checks for free-variable behavior and compared the modified interpreter with the original source implementation.

Runtime on the assignment benchmark
ImplementationRuntime
Original5.14 s
Filtered closure environments1.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.

← All workNext: Tufts SIS Modern →