14.4 AST Walking vs Bytecode

APPROACHESSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18

14.4.1 The choice

After parsing source code into an abstract syntax tree, how is the code executed? The evaluator can traverse the tree directly (AST walking), or it can compile the tree to bytecode and then interpret the bytecode.

AST walking is simple. Bytecode is faster but adds compilation overhead.

14.4.2 Overview

This choice affects performance on tight loops and long-running code. It also affects the complexity of the runtime: AST interpreters are simple to build, bytecode VMs require more infrastructure.

The choice also shapes what a JIT compiler can build on. Most machine-code JITs start from bytecode, as in V8 and HotSpot. An AST interpreter can be compiled too, through partial evaluation of the interpreter itself, which is how Truffle-based runtimes such as FastR work.

14.4.3 Option A: AST Walking

The parser produces an AST. Each node is a function call, variable lookup, or literal. The evaluator walks the tree recursively.

To evaluate x + y, the evaluator dispatches on the node type. It looks up + and finds it is a builtin. It looks up x and y in the environment and calls the function with those arguments. Then it walks the child nodes.

Benefit: the evaluator is simple. A small recursive evaluator fits in a few hundred lines, is easy to debug, and takes new operators and new types without a compiler change.

Benefit: debugging information is accurate. The evaluator has the original source location. Error messages include line numbers and context.

Cost: interpretation is slow. The evaluator must dispatch on the node type every time. A tight loop over a million iterations makes a million dispatch decisions.

Example: for (i in 1:1e6) { x <- i + 1 }. The evaluator walks the for loop body a million times. Each iteration dispatches on assignment, looks up the variable, dispatches on addition, looks up the constant 1. This overhead multiplies.

14.4.4 Option B: Bytecode

The compiler converts the AST to a linear sequence of instructions. x + y becomes two load instructions (load x, load y) and one call instruction (call +). The evaluator is a loop that reads an instruction, executes it, and moves to the next.

Benefit: the evaluator is a tight loop. It still dispatches once per instruction, but each dispatch is a jump through a table instead of a recursive call that inspects a tree node, and constants and variable slots can be resolved once at compile time.

Cost: compilation takes time. A one-shot expression compiled once must pay the compilation cost. If the compilation time exceeds the speedup from bytecode execution, it loses.

Cost: debugging is harder. The compiler rewrites the tree. Line number information must be threaded through. Error messages become less precise.

Cost: the compiler is complex. Optimizing bytecode (constant folding, dead code elimination) requires a substantial codebase.

Example: R’s own compiler is written in R, so compiling a function costs far more than evaluating a short expression once. That is why R compiles a closure only on its first or second call and leaves single top-level expressions, other than loops, to the AST evaluator.

14.4.5 Scored on the nine dimensions

Dimension AST Walking Bytecode
Isolation Per-interpreter, each evaluator is independent Per-interpreter, bytecode is compiled per-interpreter or shared
Data movement No, AST walks do not move data No, bytecode interpretation does not move data
Startup cost Zero: evaluate immediately after parsing Nonzero: compilation before execution
Steady-state overhead High: dispatch per node, millions of overhead decisions Low: tight loop with minimal overhead
Memory behavior AST occupies memory, one tree per code object Bytecode occupies memory in addition to the AST in R, which keeps the original expression in the constant pool, and compilation needs buffers
Portability All platforms support recursive function calls All platforms support instruction loops
Package compatibility C packages call back into R evaluator C packages call back into R evaluator, same interface
Failure containment Error in evaluator affects only the expression Error in compiler affects bytecode generation, error in bytecode VM affects only execution
Determinism Evaluation order matches source, predictable Evaluation order matches bytecode, predictable if compilation is deterministic

14.4.6 Where each one wins

AST walking wins for code that runs once (compile-then-discard scripts), debuggability, and simplicity. Interactive REPLs favor AST because compilation overhead is visible.

AST walking also wins when the code is highly variable and unlikely to be hot. Procedurally generated code that runs once does not benefit from bytecode.

Bytecode wins for code that runs many times (tight loops, long-running servers) and when the compilation cost is amortized. Scientific computing favors bytecode.

Most production JITs start from bytecode. V8 compiles JavaScript to bytecode for its Ignition interpreter and optimizes hot functions from there, and HotSpot compiles JVM bytecode.

Hybrid systems are practical. A tracing JIT, as in LuaJIT and PyPy, records the operations of a hot loop and compiles that trace to machine code, while everything else stays in the interpreter. V8 does not trace; it compiles whole functions.

14.4.7 R today

GNU R’s eval() in src/main/eval.c walks the AST recursively. It is the original evaluator, and it still runs top-level code, closures that have not been compiled yet, and anything the compiler declines.

R 2.13.0 added the byte-code compiler as the compiler package. R 3.4.0 turned its JIT on by default at level 3: closures are compiled on their first or second call, and top-level loops are compiled before they run. Since R 3.5.0, packages are byte-compiled when they are installed. The bytecode is stored in the closure’s body and run by the bytecode interpreter in eval.c. Nothing is compiled to machine code.

C code evaluates R code with Rf_eval() or R_tryEval(). When the call reaches a compiled closure, R runs its bytecode, so the C interface is the same either way.

14.4.8 Relevance to my work

threaded-r inherits R’s hybrid system: AST walking plus bytecode compilation.

Byte compilation can be turned off. compiler::enableJIT(0) or the environment variable R_ENABLE_JIT=0 disables the JIT for a session, and R CMD INSTALL --no-byte-compile installs a package without compiling it.

In practice, most R code that runs repeatedly is bytecode. Base and recommended packages ship byte-compiled, other packages are compiled at install time, and the JIT compiles user closures after their first or second call. The AST evaluator still runs top-level code outside loops, and functions the compiler declines, such as ones that call browser().

r-in-rust evaluates the AST directly and has no bytecode compiler yet. A bytecode path may matter less for it than for GNU R, since its parallelism comes from many interpreters, not from a faster single one.

Is the bytecode path the right base for machine-code compilation in a parallel R? Bytecode maps more directly onto a compiler backend such as LLVM. Partial evaluation of an AST interpreter, as in FastR, avoids keeping two evaluators in step.

Where this appears

  • threaded-r Inherits R’s hybrid: an AST evaluator plus a bytecode compiler, JIT-enabled by default, that produces bytecode, not machine code.
  • r-in-rust (rrs) Uses AST walking as the evaluator. No bytecode compiler yet.

Where to go next

Back to top