flowchart TD
A["process: platform,<br/>locale, malloc state"]
B["interpreter: heap,<br/>symbols, options"]
C["thread: call stack,<br/>current interpreter"]
A -->|accessible to| B
A -->|accessible to| C
B -->|bound to one thread| C
13.1 Multi-Interpreter Architecture
13.1.1 Motivation
GNU R executes R code in a single thread within each process. That was a reasonable design when R started in 1993. On a machine with dozens of cores, R code uses one of them unless it hands its work to a multithreaded C library such as a parallel BLAS.
Packages such as parallel, future and furrr work around this by running R code in separate worker processes. Each process carries its own copy of the runtime. Forking lets workers share memory pages copy-on-write, but every worker still has its own interpreter, and a socket worker starts cold. The limit underneath is one interpreter per process.
The alternative is multiple interpreters in one process, each on its own thread, sharing only what they must. This requires careful isolation.
13.1.2 Mental model: Apartments
Imagine a building where each apartment has its own kitchen, bedroom, and utilities. Residents do not enter each other’s apartments. Mail (messages) passes through the lobby. This is the apartment model.
In an R context, each apartment is one complete interpreter. It owns its own global environment, loaded libraries, and heap. Interpreters run on separate threads. One thread owns each interpreter. That thread is the only one that can modify its interpreter’s state.
No ordinary SEXP crosses an interpreter boundary, because SEXPs are not thread-safe. Instead, threads communicate by copying data or through explicitly shared immutable constants.
The model is meant to keep the promise compiled packages rely on: each one sees one interpreter at a time, as it does in GNU R. threaded-r does not get that for free. A compiled package must be rebuilt against the fork, whose ABI tag refuses a package built for stock R, and its shared object stays confined to the first interpreter that loads it unless the package declares itself capable of running in more than one.
13.1.3 Mental model: What one interpreter is
An interpreter holds far more than a parser and an evaluator. Here is what it must hold to work as an isolated unit.
The figure below shows how one process contains three independent apartments, each with its own interpreter, heap, and owner thread. A small shared region at the bottom holds immutable constants (the immortal arena).
The apartment model. Each interpreter owns one thread and all of its own mutable state, so no SEXP ever crosses a boundary. Only immortal objects, which no collector touches, are shared.
13.1.4 Contents of one interpreter
Symbol table and namespaces. Every symbol the interpreter has interned, and the namespace of every package it has loaded. Each interpreter has its own symbol table. One package loaded in apartment A does not affect apartment B.
Global environment and search path. The top-level namespace where <- assigns at the prompt. The chain of environments that determine name resolution. Each interpreter has its own.
Heap and garbage collection state. All allocated SEXPs. The mark-sweep state during garbage collection. Free lists. The age structure for generational collection. This is per-interpreter in threaded-r because each SEXP in one interpreter is unreachable from others.
Options. getOption() and options() work on interpreter-local state. digits, width, scipen. Per-interpreter isolation means one apartment can change digits without affecting others.
Random number generator state. The seed and internal state of the default RNG. Per-interpreter so that set.seed() in apartment A does not reset apartment B’s stream.
Connections. Open files, sockets, pipes. In the full design each interpreter has its own connection table, so a connection number in apartment A means nothing in apartment B. threaded-r still keeps GNU R’s process-wide table, with a lock around claiming and freeing slots. The operating system’s file descriptors stay process-wide in any design.
Graphics devices. The active plotting device. threaded-r has split this only part of the way: file devices such as pdf() work on worker interpreters, and interactive devices stay with the main interpreter because they need a process-wide event loop.
Condition handlers. The current tryCatch() or withCallingHandlers() stack. Kept with the owner thread in R_ThreadState, so it is per-interpreter in effect, because error propagation is local to one interpreter.
Interrupt flag. A request to stop the current computation. Per-interpreter, so stopping apartment A’s work leaves the others running. In threaded-r, Ctrl+C itself reaches only the main interpreter, and a worker is stopped through its own cancel request.
The eleven pieces of state one interpreter owns. Isolating an interpreter means giving it a private copy of every box in this grid.
13.1.5 Three-level state model
Upstream GNU R holds about 1,574 mutable global variables, and the retrofit has to give each one a home. threaded-r uses three levels.
threaded-r splits 1,574 upstream globals across three levels: process, per-interpreter, and per-thread.
Process-global state is what every interpreter shares: platform information, the C library’s malloc() state, signal handlers, and, in threaded-r today, the locale. Whatever lives here must be read-only, set once at startup, or protected by a lock.
Per-interpreter state is the meat of the isolation. Each interpreter has its own struct R_InterpreterState. It grew as the patch series moved process-global state into it. The symbol table, options and the RNG state all live here, and the heap sits in a companion R_GCState that the interpreter owns.
Per-thread state lives in a thread-local storage struct R_ThreadState. This includes the current interpreter, the call stack, and the error flag. It tracks which thread is executing and what work it is doing. All 35 of the globals the census assigned to per-thread state now live here, and none of them still exists as a free-standing symbol.
The census found 1,574 mutable globals in upstream R. All 35 of the per-thread ones now live in R_ThreadState, and R_InterpreterState had 556 members after patch 0127 (2026-08-22). The migration is not finished, and the remainder is not a simple subtraction: the struct also picked up members that were never separate globals.
This three-level model allows threads to run interpreters in parallel. Thread A executes interpreter-1 (whose state it owns). Thread B executes interpreter-2 (a different state struct). No lock guards the evaluator loop itself, though a few process-wide locks remain around code the fork has not split.
13.1.6 Internal mechanics: The apartment constraint
Isolation requires enforcement. Three rules make it concrete.
Rule 1: One owner thread per interpreter. Each R_InterpreterState is owned by exactly one thread. That thread can modify any field without a lock. Another thread cannot touch that interpreter’s state. If thread B needs to know something from interpreter-1, thread A must copy the data or signal an event.
Rule 2: No SEXP crosses a boundary. SEXPs are not thread-safe. They lack synchronization for concurrent access. A SEXP allocated in interpreter-1’s heap is a pointer to interpreter-1’s heap. Thread B cannot dereference it. Thread A must serialize the value if thread B needs it.
Rule 3: Shared data must be immutable. If data is shared across interpreters, it must never change. No thread can mutate shared memory. This eliminates data races.
The immortal arena is a shared immutable region holding a small fixed set of builtin constants, among them NULL, NA_STRING and the empty string. All interpreters read the same pointers. These SEXPs are never freed and never modified. This is analogous to CPython PEP 683 immortal objects.
13.1.7 The hard part
Three obstacles stand between those rules and a working fork.
Native package global state
Compiled packages are C, C++ or Fortran, and much of that code keeps state in file-scope static variables: a cached pointer, a flag set by an initialization routine, a workspace allocated once. In GNU R this is safe, because only one interpreter ever runs the code.
What may compiled package code assume when several interpreters run it? A plain dlopen maps a shared object once per process, so two interpreters that load the same package see the same static variables, and using them concurrently is a data race. threaded-r’s current answer is confinement: the first interpreter to load a shared object owns it, and other interpreters are refused unless the package declares itself capable. Whether that holds up beyond base and recommended packages is the open part.
Earlier alternative R implementations (Renjin, FastR, Rho, pqR, Riposte) each had to decide what to do about compiled packages. Packages and Namespaces Across Interpreters covers how each one fared.
Garbage collection bookkeeping
Every SEXP header holds a mark bit and a generation number, and two pointers in the header thread the object onto the collector’s lists. The collector writes all of these when it marks an object or moves it between generations. The evaluator writes the reference count, in the same header, when a value is bound to a variable or stored in a list. Code that only reads a value can still cause a write to its header.
This means sharing a SEXP between interpreters requires synchronization on every field access. The cost makes naive sharing impractical. Per-interpreter heaps eliminate this. Each interpreter collects its own heap. No garbage collector touches another interpreter’s objects.
See garbage collection for details.
Builtin constants and the immortal arena
Some values must be readable everywhere. NULL, NA_STRING and the empty string are used constantly. They are “builtin constants” that the evaluator checks by pointer identity. In threaded-r, TRUE and FALSE are per interpreter.
If interpreter-1 has NA_STRING at address 0x12345 and interpreter-2 has it at 0x54321, the pointers do not match. A compiled package that cached NA_STRING or R_NilValue in a static while running in one interpreter would then compare wrongly in the other.
The immortal arena solves this. It holds the eleven builtin constants, R_EmptyEnv, and the 38 symbols that the C API exports by pointer with their immortal print names, allocated once when the process bootstraps, and every interpreter uses the same addresses. Each one keeps its mark bit permanently set and sits on no generation list, so the collector’s existing “already marked” test in FORWARD_NODE skips it before writing anything, and its reference count is pinned at the maximum.
The memory cost is a small, fixed set of objects. The cost in care is larger: nothing may ever clear those mark bits, and the design depends on R’s collector unmarking only the objects it finds on generation lists.
13.1.8 Other systems
Few runtimes run isolated interpreters in one process. How Other Runtimes Do It compares four of them in detail; this is the short version, with what each means for R.
Python: PEP 684 and the GIL
CPython has had subinterpreters in its C API since 1997, but until Python 3.12 they shared one GIL. PEP 684 (Python 3.12) let a subinterpreter have its own GIL, and PEP 734 (Python 3.14) added the concurrent.interpreters module. Extension modules were the hard part, because many keep state in C globals. CPython’s answer is to refuse them: an isolated subinterpreter raises ImportError for any extension that has not opted in through multi-phase initialization and, when the interpreter has its own GIL, the per-interpreter-GIL module slot, while the main interpreter still loads everything.
Transfer to R: refusing unaudited native code, instead of hoping it is safe, is the same rule threaded-r applies to compiled packages.
Lua: Designed for isolation from day one
The Lua C library has no global variables; lua_newstate creates an independent state with its own heap, collector, string table and globals. The host program creates states, and every API call names the state it acts on. A state is not thread-safe, so a program that wants parallelism runs one state per OS thread.
Transfer to R: Lua never had to retrofit this. R has 1,574 mutable globals to sort, and every compiled package was written for a single interpreter.
V8: Isolates with explicit boundaries
V8 calls its unit an isolate: “a VM instance with its own heap”. The embedder creates isolates with v8::Isolate::New(), and objects from one isolate must not be used in another, so values cross by structured clone, a copy. Isolates share a process, so a crash in native code still takes every isolate down.
Transfer to R: one collector per heap removes cross-interpreter GC coupling, which is the arrangement threaded-r now has.
Ruby: Ractors and object-level sharing rules
Ruby 3.0 added Ractors, still experimental in Ruby 4.0. Threads in different Ractors run in parallel. Only shareable objects (deeply frozen values, classes and modules) cross freely; others are deep-copied or moved. Through Ruby 4.0 all Ractors share one heap and every collection stops all of them, and Ruby 4.1.0-preview1 moves to per-Ractor collection.
Transfer to R: the apartment model has no language-level sharing rule. R code cannot share an object with another interpreter at all, which keeps native packages safer and makes threads less flexible.
Erlang/BEAM: Message passing at language design level
Each Erlang process has its own heap, and a message is copied into the receiver’s heap. Large binaries and literals are the exceptions: they live outside process heaps and are passed by reference. The language was designed around this.
Transfer to R: R workloads pass large vectors and matrices, so copying every message is expensive. Erlang’s exception for large binaries points at the same idea as shared immutable vectors.
13.1.9 The apartment model in threaded-r
threaded-r implements the apartment model using the three-level state model. Each apartment is one R_InterpreterState.
R_NewInterpreter() builds an interpreter on the calling thread and returns its R_ThreadState, with the new interpreter current on that thread. The calling thread owns it. R_NewInterpreterEx(R_INTERP_BOOTSTRAP_BASE) also attaches base and the fork’s worker package set, which a thread needs before it can run ordinary R code. Both return NULL until some interpreter in the process has finished the process-wide bootstrap. R_DestroyInterpreter() tears the interpreter down, on the same thread.
#include <RInterpLifecycle.h> /* threaded-r's embedding header */
/* on the thread that will own the interpreter */
R_ThreadState *st = R_NewInterpreterEx(R_INTERP_BOOTSTRAP_BASE);
/* ... evaluate R code with the ordinary C API ... */
R_DestroyInterpreter(st);A thread owns its interpreter for the interpreter’s whole life. threaded-r has no call that moves an interpreter to another thread; an attach and detach design exists on paper only. Most code never calls these functions directly: R_InterpreterSpawn() builds an interpreter on a fresh thread and runs one R expression there.
Each threaded-r interpreter has its own heap and its own garbage collector. The churn gate, which counts nodes on a worker interpreter’s generation rings that live in the main heap, reads zero against a baseline of 814 to 6,432 per run, verified twice.
Interpreters communicate through channels (R_ChannelCreate, R_ChannelSend, R_ChannelRecv, R_ChannelClose). A channel carries bytes, never a SEXP. The sender serializes the value and the receiver unserializes it into its own heap, so both interpreters end up with independent copies of the data.
Explicitly shared immutable data can reduce copying overhead. If a vector is marked immutable, both interpreters can read the same pointer, provided neither ever writes to its header. This is under exploration.
Copying costs time for large data. What it buys is isolation: one apartment’s garbage collection does not pause another’s.
13.1.10 Tradeoffs and the native package bet
Retrofitting isolation into GNU R is architecturally hard. The codebase assumes process-global state everywhere. Threads must coordinate through locks, messages, and serialization.
Serialization is the price. A task whose input and output are large next to its compute time can run slower across interpreters than in one R session. Where that crossover sits has not been measured.
The bet is that the apartment model keeps what compiled packages rely on. Rebuilt against the fork, a package runs without source changes in the interpreter that loads it first, where it sees one global environment and one heap, as in GNU R. Running it in several interpreters at once needs its C statics audited or moved, which is the work the base and recommended packages went through.
So the bet still depends on the open question above. For a package to serve several interpreters at once, its C state has to be made per-interpreter or shown to be safe to share.
The apartment model prioritizes native package compatibility. A design that shared one interpreter across threads would need every compiled package to become thread-safe first.
13.1.11 Lessons and next steps
The three-level state model is a working architecture, not a final design. Some of the original 1,574 globals are still process-global, and the project’s risk register names the locale and interactive graphics, and a package’s native code outside the audited base and recommended set still loads in only one interpreter.
Per-interpreter heaps are no longer a proposal: each interpreter has its own heap and collector. The cost is that nothing except the immortal arena can be shared by pointer, so every value that crosses an interpreter boundary is copied.
The native package question will decide whether the design is usable beyond the packages the project has audited.
Further reading
- PEP 684, “A Per-Interpreter GIL” <https://peps.python.org/pep-0684/>, Eric Snow. Documents the retrofitting problem in a mature runtime.
- PEP 683, “Immortal Objects, Using a Fixed Refcount” <https://peps.python.org/pep-0683/>, Eric Snow and Eddie Elizondo. The closest published analogue of the immortal arena.
- Lua 5.4 Reference Manual,
lua_newstate<https://www.lua.org/manual/5.4/manual.html#lua_newstate>. The reference for an interpreter built around an explicit, independent state. - Getting started with embedding V8 <https://v8.dev/docs/embed>, the V8 embedder’s guide, which defines isolates and contexts.
- Ractor, Ruby’s Actor-like concurrency abstraction <https://docs.ruby-lang.org/en/master/language/ractor_md.html>. Object-level sharing rules.
- threaded-r, my fork of GNU R, where the census of mutable globals was done. Its repository is not public yet.
Where this appears
- threaded-r The core design that enables one process to run multiple interpreters on separate threads without a lock on the evaluator.
- r-in-rust (rrs) From-scratch design reference: each interpreter owns its heap, symbol table, and thread state from the start.