JupiteX Get the app
Science & Technology28 Nov 2026 · about 7 min

rlox: the Lox language in Rust, as a tree-walker and a bytecode VM

The brief

The surprising part is that rlox is not just one interpreter: it implements the same small, dynamic language in two different ways. Written in Rust, it follows Robert Nystrom’s Crafting Interpreters, building both a tree-walk interpreter called jlox and a bytecode VM called clox. Comparing them shows how different execution models handle the same language features. Both back ends support variables, control flow, functions, closures, classes, inheritance, this, and super. For example, a Lox counter function can return a closure that keeps updating a local variable. The tree-walker uses environments; the VM uses stack slots and upvalues. Both share scanner.rs, which turns source text into tokens, and a test suite. You can run either back end from a REPL or execute a file. The VM is the default, while the --tree option selects the tree-walker. The article describes this as a way to compare what happens inside each implementation, even when both produce the same output. It gives build and run commands, but no performance measurements.

01

What is rlox, and what does it let you do with the Lox programming language?

The surprising part is that rlox is not just one interpreter: it implements the same small, dynamic language in two different ways. Written in Rust, it follows Robert Nystrom’s Crafting Interpreters, building both a tree-walk interpreter called jlox and a bytecode VM called clox. Comparing them shows how different execution models handle the same language features.

Both back ends support variables, control flow, functions, closures, classes, inheritance, this, and super. For example, a Lox counter function can return a closure that keeps updating a local variable. The tree-walker uses environments; the VM uses stack slots and upvalues. Both share scanner.rs, which turns source text into tokens, and a test suite.

You can run either back end from a REPL or execute a file. The VM is the default, while the --tree option selects the tree-walker. The article describes this as a way to compare what happens inside each implementation, even when both produce the same output. It gives build and run commands, but no performance measurements.

02

What is the difference between a tree-walk interpreter and a bytecode virtual machine?

The key difference is what executes the program. A tree-walk interpreter evaluates the program’s syntax tree, moving through its structures as it runs. A bytecode virtual machine instead runs a sequence of instructions produced by a compiler. The two approaches can implement the same language, but organize the work differently.

In rlox, the tree-walker parses source into an AST, resolves local references, then evaluates the tree. Its environments use Rc<RefCell<..>>. The VM’s single-pass Pratt compiler emits bytecode while parsing, without making an AST. Its machine runs that code using a stack and call frames. For a closure, the tree-walker retains an environment reference; the VM uses captured upvalues.

Both rlox back ends support the full Lox language, including closures and inheritance, and the article says they produce the same output for its example. The VM also has Invoke and SuperInvoke instructions for method calls. The article explains the designs and their trade-offs, but does not provide a measured speed comparison between them.

03

How much faster was rlox’s VM than its tree-walker on the fib(27) benchmark?

There is no fib(27) benchmark result in the provided article text. It does not give run times for either rlox back end, so a speedup cannot be calculated from the information available. Naming a multiplier would mean inventing a result.

The article does offer an explanation for why the VM might perform differently. Its compiler emits bytecode directly, and the machine executes that code using stack slots and call frames. The article also says keeping locals on the stack is where the VM’s speed comes from. It describes fast paths for method calls that avoid creating a bound method object just to call it.

Those implementation details are not a substitute for benchmark figures. To answer the question with a specific speedup, one would need comparable fib(27) timings for the VM and tree-walker. The supplied text does not include those timings or say how such a benchmark was run. Therefore, the accurate answer is that the speed difference is unspecified here.

04

What can happen when a reference-counted VM has a cycle, such as an object holding a closure that captures it?

A cycle is a group of objects that refer to one another, so their reference counts may never fall to zero. In general, reference counting alone cannot reclaim such objects, even when the rest of the program can no longer reach them. This can leave memory occupied unnecessarily. That is a general memory-management fact, not a cycle detail explained in the article.

For example, an object might hold a closure, while that closure captures the object. Each keeps a reference to the other. When external references disappear, the pair can still retain nonzero counts. The article describes rlox’s VM as using reference counting and names closures and captured upvalues, but does not state that this exact cycle occurs in rlox.

The article contrasts rlox’s approach with clox’s mark-and-sweep garbage collector, calling reference counting a natural tool in Rust and saying it has a known cost. It does not say how rlox handles cycles or whether it detects them. In general, a reference-counted design needs a way to prevent or handle cycles if they arise.

05

How does a closure keep a local variable alive after the function that created it has returned?

A closure can outlive the function that created it, yet still need one of that function’s local variables. The VM cannot leave that local in its original stack slot after the function returns, because the slot is about to disappear. It must preserve the captured value somewhere the closure can reach.

In the article’s counter example, makeCounter creates count and returns the nested next function. Each call to next increases count. In the VM, count begins in a stack slot. When makeCounter returns, the VM closes the upvalue by moving the value off the stack to a place the closure owns. The tree-walker handles the same example differently: its environment object is kept alive through a reference held by the closure.

Open and closed upvalues are the VM’s mechanism for handling captured locals. The article presents them as the cost of keeping locals on the stack, which it connects to VM speed. The example demonstrates why closures can preserve state across calls after their creator has returned. The two back ends achieve that behavior through different storage strategies.

06

Why does the tree-walker resolve local variable references before running a program?

The resolver is a tree-walker stage that runs before the program executes. It walks the AST and calculates the scope depth of every local variable reference. That matters because a variable should refer to the declaration visible where its code was written, not to a same-named declaration that appears later during execution.

The article describes the bug this prevents: without resolution, a closure can see a different variable after a later declaration shadows the one it captured. The resolver records how far away the correct environment is for each local reference. Then the interpreter can jump directly to that environment instead of searching a chain of names at run time.

This means resolution is not just a speed tweak; it preserves the intended binding of local variables in closures. In rlox, it is one of three tree-walker stages, alongside parsing and interpreting. The VM takes a different route: its compiler emits bytecode directly while parsing and places locals in stack slots. The article presents both approaches as ways to make the same language behave correctly.

07

What is bytecode, and why can a stack VM execute it faster than repeatedly walking a syntax tree?

Bytecode is executable instructions emitted by a compiler for a virtual machine. Instead of evaluating the source program’s syntax tree node by node, the VM follows those instructions. This changes how execution is organized and can reduce the work of repeatedly interpreting tree structure. The article describes rlox’s compiler as emitting bytecode directly while parsing, without building an AST.

In rlox, the VM is a stack machine: locals live in stack slots, and execution uses call frames. A closure’s captured local becomes an upvalue. Method calls can use Invoke and SuperInvoke instructions, which perform lookup and calling together rather than creating a bound method object only to discard it. The tree-walker, by contrast, evaluates the AST using environments.

The article connects stack-resident locals to the VM’s speed and explains the method-call fast paths. These are implementation reasons a bytecode VM can be faster than repeatedly walking a tree, not a reported benchmark result. The provided text gives no timing or fib(27) speedup, so it does not quantify the difference for rlox.

This brief was written by AI from the original reporting and checked by other models. Names, figures and quotes come from the source; read it for full context.

Read more in the JupiteX app

Pulse is free. New stories every 4 hours, each one broken into the questions that explain it.

Or read more news on the web