How R Executes Code Today

ORIENTATIONSTATUS: STABLELEVEL: INTROREVIEWED: 2026-11-18

Between the text you type and its result, GNU R parses the code into a tree, evaluates the tree against environments, delays arguments as promises, compiles functions to bytecode, and collects garbage, all on one thread. Each stage below has its own page later in the corpus.

Parse to AST

When you type f(x + 1), the parser turns the text into a tree, an AST, that records the structure of the code. For f(x + 1) the tree is a call to f with one argument, and that argument is itself a call: + applied to x and 1.

Read more: how R executes code.

Tree-walk with environments

The evaluator walks this tree. To run f(x + 1) it first looks up f in the current environment, a table that maps names to objects. Every call to an R closure creates a new environment to hold that call’s arguments and local variables.

Each environment points to an enclosing one. A name missing from the current environment is looked up in its enclosure, then in that environment’s enclosure, until R finds the name or reaches the empty environment and reports an error. A function’s enclosure is the environment it was created in, which is what makes R’s scoping lexical.

Read more: environments and closures.

Promises delay argument evaluation

When R calls f(x + 1), it does not compute x + 1 first. It creates a promise, an object that holds the unevaluated expression x + 1 together with the caller’s environment, where x will be looked up.

The promise is forced, which means evaluated, the first time the function body needs the argument’s value, and R keeps the result for later uses. An argument the function never uses is never computed.

This is called lazy evaluation. It lets R skip work and it lets functions decide when and whether to evaluate their arguments.

Read more: promises and lazy evaluation.

Bytecode JIT for hot closures

R also has a compiler, the base package compiler, that turns closures into bytecode, a compact instruction format that R’s bytecode interpreter runs faster than the evaluator can walk a tree. R’s JIT does this automatically. At the default JIT level 3, R compiles most closures on their first or second call, leaving out small closures created inside other functions, and compiles top-level loops before they run. The JIT stops at bytecode: GNU R never generates machine code.

Read more: how R executes code.

Garbage collection over one heap

All R objects live in one heap, a pool of memory that R allocates from. When you create a vector or a list, it is allocated on the heap. When nothing refers to it any more, the garbage collector frees it at a later collection.

R’s garbage collector is a generational mark-and-sweep collector. It stops the R code, marks every object reachable from the roots, and frees whatever is left unmarked. Most collections cover only the younger generations, where most garbage is. A collection starts when an allocation would take the heap past its current limit, and it runs on the same thread as the R code.

Read more: garbage collection.

All on one thread

All of these stages run on one thread, one at a time. A second thread cannot run R code next to the first, because every stage reads and writes state the whole process shares: the heap, the symbol table, the stack of protected objects, and many global variables in R’s C code, none of them guarded by locks. That shared, unguarded state is what stops parallel R inside one process.

flowchart LR
    src["source text"] --> parse["parser<br/>(AST)"] --> eval["tree-walk<br/>(with promises)"]
    eval -->|JIT compiles<br/>closures| bc["bytecode<br/>interpreter"]
    eval --> heap[("one heap")]
    bc --> heap
    gc["GC (mark/sweep)"] --> heap

R’s execution pipeline: every stage runs on one thread, all stages share one heap.

Further reading

  • R Language Definition: Evaluation of expressions <https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Evaluation-of-expressions>
  • R Internals: Argument evaluation <https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Argument-evaluation>
  • Luke Tierney: A Byte Code Compiler for R <https://homepage.cs.uiowa.edu/~luke/R/compiler/compiler.pdf>

Where this appears

  • threaded-r Must preserve this execution model while distributing state across interpreters.
  • r-in-rust (rrs) Reimplements the parser, evaluator, promises and garbage collector in Rust. It has no bytecode compiler yet.

Where to go next

Back to top