3.1 Why R Is Difficult to Parallelize

GAPSSTATUS: STABLELEVEL: INTROREVIEWED: 2026-11-18

Seven constraints keep GNU R to one thread of R evaluation per process: one interpreter per process, about 1,574 mutable globals, GC bookkeeping inside every object, mutable environments, lazy evaluation, a C API that hands compiled code raw pointers, and copy-on-modify built on single-threaded reference counts. Each is rooted in the language or in GNU R’s implementation, they reinforce each other, and each needs a different fix. The linked concept pages cover each one in depth.

3.1.1 The Seven Constraints

One Interpreter, One Thread

All R evaluation in a GNU R process runs on one thread. The parser, the evaluator, the memory allocator and the garbage collector run there, and the base library assumes it.

GNU R runs one interpreter per process, and all R evaluation in that process happens on one thread: the read-eval-print loop calls eval() for each top-level expression. Compiled code that R calls runs on that thread too. It may start threads of its own, as a threaded BLAS does, as long as they never call back into R.

This is why parallel packages such as parallel and future start several R processes. They cannot run R code in parallel inside one process, so they partition the work, serialize the data, send it to worker processes, deserialize the results and merge them. That works, at the price of serialization and duplicated memory.

Moving from one interpreter per process to multiple interpreters per process (the apartment model) requires threading the interpreter code. Many codepaths assume they own the process. They must respect apartment boundaries and synchronization rules. The C API must clarify which functions are thread-safe and under which conditions.

Global Mutable Runtime State

GNU R’s C sources hold a great many mutable global variables, and almost all of them are global on purpose: caches, the symbol table, string pools, package registries, environments and handler stacks. Each is an optimization or a convenience for a program that has one interpreter.

threaded-r’s census of R 4.6.1 counted 1,574 mutable globals. They include the symbol table (which maps names to R symbols), the string cache (which interns strings so equal strings share one object), and the global environment, along with handler stacks for conditions and errors and scratch buffers reused across calls. Code that reads or writes these globals takes no lock.

Threads share memory by default. If two threads read and write the same global without coordination, the result is a race condition, which can corrupt data or crash the process. To run several interpreters in one process, each interpreter needs its own copy of every global that belongs to an interpreter. Moving one is simple in principle and slow in practice: each global has to be found, moved into an interpreter state structure, and every piece of code that touches it updated to reach it there.

Garbage Collection Bookkeeping Inside Every Object

SEXP is the C typedef for an R object. Every SEXP is a pointer to a SEXPREC structure on the heap. That structure embeds:

  • Mark bits, which the GC uses to identify live objects during a mark phase.
  • Pointers that link the object into its generation list.
  • A reference count, which tracks how many locations hold pointers to this object.

None of these waits for the program to modify the object. The collector sets mark bits and relinks list pointers on every object it reaches, and a reference count changes whenever a new reference to the object is made, for example when it is bound to a name or passed to a function.

Every SEXPREC embeds mutable GC bookkeeping. Passing an object to a function or binding it to a name updates its reference count, and a collection writes mark bits and list pointers into objects the program never modifies. So no R object is read-only under concurrent access without extra synchronization.

In a shared-memory parallel system, read-only objects can be shared between threads without locking. R objects are not read-only even when code treats them that way. Two threads that each pass the same vector to a function can race on its reference count, and two concurrent collections can race on its mark bits. Locking every object access would remove the parallelism. The redesign, moving the bookkeeping out of objects or giving each interpreter its own heap, is substantial. See Garbage Collection for details.

Environments as Shared Mutable State

Environments map names to values, stored as a linked frame or a hash table. In R they are a language construct, not a library data structure. Functions close over environments. Code looks up variables by walking the lexical environment chain. Assignment modifies environments in place. Code in the global environment modifies shared state.

In GNU R, environments are mutable SEXP objects. Code can modify an environment using assign() or <-. Multiple function scopes can refer to the same environment. If two threads execute code that shares an environment and both modify it, the modifications can race.

Environments are where R keeps mutable state. Any parallelism strategy that lets threads share environments, instead of isolating them, has to serialize access to them. Isolating them means copying, which breaks closures that share an environment and rules out library patterns that depend on one.

Lazy Evaluation

R delays evaluation of function arguments. When f(x) is called, R wraps x in a promise, a data structure holding the unevaluated expression and its environment. The function body forces the promise when it first accesses x. The forced value is cached in the promise so later accesses do not re-evaluate.

Promises are write-once mutable cells. A concurrent read and a first evaluation can race. More subtly, if two threads force the same promise independently, both may evaluate the expression. The expression can have side effects (like updating a global variable). Side effects run twice, producing incorrect results.

Promises are mutable cells that cache a single computed result. Concurrent access to an unevaluated promise from multiple threads can cause the expression to evaluate multiple times, duplicating side effects. Forcing a promise is a one-time write, but there is no atomicity guarantee that a second force will skip evaluation.

In a multithreaded R, function arguments can be forced on any thread. If multiple threads can run user code, multiple threads can force a shared promise. The promise value caches across the lifetime of a function call. Duplicated evaluation breaks programs that rely on side effects running once per call.

The C API Freezes the Surface

R provides a C API for writing extensions. That API hands compiled code real pointers into vector payloads, and lets it allocate, protect and evaluate on the assumption that it is the only caller. Thousands of CRAN packages compile against this interface and ship as binaries.

Once packages are compiled, GNU R’s C API acts as an ABI (application binary interface): it fixes the exported symbols, their signatures and calling conventions, and the public constants compiled into every package binary. It does not fix SEXPREC, which is opaque to package code; since R 4.2.0, defining USE_RINTERNALS no longer exposes it. For a parallel design, that means the object representation can change and the accessor functions cannot. A representation change costs the packages that rely on non-API entry points.

Many proposed fixes to the single-threaded limitation change how objects are laid out in memory. Moving mark bits outside the object, or redesigning the symbol table, changes the representation and not the interface, so conforming packages need no source changes. What would force them to change is a new signature or a changed public type. Coordinating that across the thousands of packages with compiled code would be slow and expensive. See What Is an ABI for details.

Copy-on-Modify with NAMED and Reference Counting

R uses copy-on-modify. When code assigns an object to a new name (for example, y <- x), R does not copy the object; both names point to it. Later, if code modifies y, R checks whether the object may be shared. If it may, R copies it before modifying; if not, R modifies it in place. Since R 4.0.0 that check reads a reference count, which replaced the older NAMED field.

The check is an optimization: it avoids copies when an object has one owner. It does not survive threads. A thread that modifies an object must know whether other threads hold pointers to it, and R’s reference counts are plain, non-atomic integers updated by whichever code makes or drops a reference.

Copy-on-modify relies on reference counts to avoid unnecessary copies, and they were designed for single-threaded execution. Under concurrent access, the decision to modify in place is unsafe without atomic reference counts and per-thread ownership tracking. GNU R has neither.

The performance of R code depends on copy-on-modify. If every assignment copied, R would be unusable on large data. Thread-safe copy-on-modify needs atomic reference counts and a memory model that says who owns an object under concurrency. That is a significant redesign, though not an impossible one: CPython’s free-threaded build met the same problem with its reference counts and answered it with biased reference counting (PEP 703).

3.1.2 Where These Constraints Live

The following diagram maps the constraints to their origins in the specification or implementation.

flowchart TB
  R["R program"] --> sem & impl & api
  subgraph sem["language semantics"]
    single["one interpreter,<br/>one thread"]
    lazy["lazy evaluation"]
    envs["mutable<br/>environments"]
  end
  subgraph impl["GNU R implementation"]
    globals["~1,574 mutable<br/>globals"]
    gcbits["GC bookkeeping<br/>inside objects"]
    named["reference counts<br/>for copy-on-modify"]
  end
  subgraph api["C API and ecosystem"]
    abi["C API surface<br/>frozen by packages"]
  end

Each constraint has a root cause in the language semantics, the GNU R C implementation, or the published C API.

3.1.3 Consequences

R users do use multicore machines and clusters today, through the process-based packages below. What they pay is the overhead those packages carry: a full R process per worker, and serialization for every value that crosses between processes. Where that overhead is small next to the work, the workarounds are enough. Where it is not, R users move the work to another language or accept one core.

3.1.4 Existing Workarounds

Several strategies work around these constraints. None removes them.

Fork Clusters

parallel::makeCluster() with type = "FORK" starts worker processes with the Unix fork() call. A forked child starts with the parent’s memory, shared copy-on-write, so creating it is fast and cheap. Forking is the usual approach on Unix systems, through mclapply() and fork clusters.

A forked child shares its parent’s memory pages copy-on-write, and the kernel copies a page the first time the child writes to it, so a new worker costs almost no memory at first. sunlight’s measurements show that this does not last: a full garbage collection in the child writes mark bits into nearly every reachable object, which privatizes the pages, and the measured workers had lost almost all sharing before their first task. In sunlight’s benchmark of 2026-08-01, the forked pool fell far short of the project’s goal of five times less memory than the best separately started pool; the experiment linked below has the figures and their caveats.

Fork clusters still pay for process isolation once the child is running. Each worker has its own heap and collector, data is copied on write or serialized explicitly, and caches are not shared. The model suits embarrassingly parallel problems with little communication between workers, and fits poorly with iterative algorithms, dynamic scheduling and fine-grained synchronization.

See Fork, Copy-on-Write, and the First GC for measurements.

PSOCK (Socket) Clusters

parallel::makeCluster() with type = "PSOCK", the default type on every platform and the only built-in type that works on Windows, starts each worker as a separate R process and talks to it over a local socket. Every value sent is serialized.

For large data or fine-grained parallelism, that serialization dominates. Each worker is a full R process with its own heap, and nothing is cached in common. See Serialization for the performance implications.

Future, Mirai, and Callr

Packages like future, mirai, and callr add higher-level interfaces: better ergonomics, dynamic scheduling, and job queues. Internally they still run work in separate R processes. future’s multisession and multicore backends use socket workers and forked workers, mirai runs its own daemon processes connected over NNG sockets, and callr starts each job in a fresh R process. What they add is convenience and scheduling, not concurrency inside one process.

sunlight

sunlight is my R package that runs a prewarmed forkserver. A template R process loads the declared packages once, for example the tidyverse or Seurat, and then forks the workers, so a new worker skips R startup and library().

In sunlight’s benchmark of 2026-08-01, the first result at 32 workers with Seurat loaded came after 4.8 seconds, against 14.9 seconds for the best incumbent, and adding workers to a running pool was 3.3 to 5.9 times faster than the best incumbent’s cold start. The run was on WSL2, all frameworks ran in one process with sunlight last against a warm filesystem cache, and nobody else has reproduced it. sunlight does not touch data movement, GC coupling or shared caching: it attacks startup cost, not the constraints that block parallelism inside one process.

Workers remain isolated processes: data is still copied, and each worker still runs its own collector. sunlight is a targeted optimization that works for its use case, not a path to shared-memory parallelism inside one R process.

3.1.5 Limits of the Workarounds

Every workaround above relies on process isolation. Each worker is a separate process with its own memory space, heap, and garbage collector, and that has costs:

  • Startup: Each worker starts R and loads its packages itself: it reads each namespace’s lazy-load database, loads its shared libraries and runs its load hooks.
  • Memory: Each worker has its own heap. A vector in the main process is copied to each worker (for PSOCK) or copied on first write (for fork), so memory use multiplies with the worker count.
  • Serialization: Data has to be serialized, sent over a socket, and rebuilt on the other side. For large data structures, serialization is the bottleneck.
  • No shared cache: Symbol tables, compiled bytecode and loaded packages are private to each process. A warm cache in the main process does nothing for the workers.

How much these costs weigh depends on the workload. In sunlight’s benchmark, the best incumbent took 14.9 seconds to bring up 32 workers with Seurat loaded and return a first result, before any real work. Whether that matters depends on how long the tasks run and how often the pool is rebuilt.

3.1.6 Paths Forward

Three architectural changes could remove or soften these constraints. Each is a design hypothesis, and the evidence for each so far comes from my own projects.

Isolated In-Process Interpreters

The apartment model places multiple interpreters in one process. Each interpreter owns its own state: globals, symbol table, package registry, heap, and garbage collector.

Several interpreters in one process can beat process-isolated parallelism. A shared address space and no process startup would cut startup and memory costs, though values still have to be serialized to cross between interpreters. The binding constraint is native code: packages compiled against the C API assume they have the runtime to themselves, and their C statics stay one copy per process however many interpreters run above them.

threaded-r’s demo at N=2 measured 1.8x faster startup and 2.1x less memory than a PSOCK cluster on a base-only workload, on WSL2. Whether that holds at larger N and for real package stacks is not measured. threaded-r takes the gradual route for native code: a package declares itself capable, and the native code of a package that has not stays with the interpreter that loaded it first. As of 2026-08-20 that covers 28 of R’s 29 base and recommended packages, and no CRAN package beyond them has been through it.

See threaded-r project and Multi-Interpreter Architecture.

A Runtime Designed for Concurrency from the Start

A new R interpreter written for concurrency from the ground up could avoid many constraints. Each interpreter owns its state, the collector could be designed for concurrent marking, promises could be atomic cells, and environments could be immutable or use optimistic concurrency.

An R runtime built for concurrency from the first day could remove most of these constraints. It has to reimplement the evaluator, the collector and the core libraries, and native package compatibility looked like the thing it would have to give up.

r-in-rust suggests that last part does not hold, because the C API asks for functions and not a layout. In its census of 2026-08-18, 28 of the thirty most downloaded CRAN packages with compiled code, built from source against its own headers, installed and loaded on it, and differential testing against a pinned GNU R checks its semantics.

The cost is reimplementation, and it is large: every builtin, every semantic corner, the whole base library. In return, a new runtime is free of the decisions that made single-threaded R convenient.

See r-in-rust project.

Shared Immutable Data Across Interpreter Boundaries

A middle path separates data from computation. Interpreters stay isolated, one per thread or one per process, but some data structures are immutable and can be shared across the boundary. Immutable vectors, persistent data structures and immutable closures can all be shared without coordination.

If R data structures were immutable, or had immutable variants, interpreters could share them safely. This would need language changes to support immutable bindings, a migration path for code that assumes mutability, and rewrites in parts of the base library. It is less radical than a full rewrite, and it does not require breaking the C API.

This path requires the language to change. R users expect mutable vectors and environments, and introducing immutable variants and getting users to adopt them would take years. Clojure and Scala show that persistent immutable collections can be the default in a practical language, but both started that way; R would be adding them late.

See Multi-Interpreter Architecture.

3.1.7 A Summary Table

Constraint Where It Lives Consequence Workaround Removes It
One interpreter, one thread GNU R implementation No in-process parallelism. Must use process isolation. Multiple processes, fork/PSOCK clusters Multi-interpreter per process (apartments)
Global mutable runtime state GNU R implementation Race conditions if two threads access the same global. Per-process isolation Move globals to per-interpreter state (feasible, laborious)
GC bookkeeping inside objects SEXPREC structure (implementation) Read-only sharing is unsafe. Reference counts race. Per-object locking (kills parallelism) or per-interpreter heaps Move GC metadata outside objects or design GC for concurrency
Environments are mutable Language semantics & implementation Shared closures are unsafe. Must isolate or lock. Copy environments per interpreter Immutable environments or optimistic concurrency
Lazy evaluation (promises) Language semantics & implementation Concurrent forcing duplicates side effects. Serialize access to promises Atomic promise cells or eager evaluation
C API freezes the surface ABI stability requirement Cannot rename a symbol or change a signature without breaking compiled packages: 5,569 of CRAN’s 25,474 packages ship compiled code (2026-10-21). Keep the exported surface fixed; the representation stays free New ABI or new runtime (r-in-rust)
Copy-on-modify with reference counts Implementation design Ownership tracking is not thread-safe, so the decision to copy is unsafe. Avoid mutation (impractical) or per-process isolation Atomic reference counting or immutable variants

3.1.8 Comparing Parallelism Models

The following diagram shows why process-isolated parallelism differs from in-process multi-interpreter parallelism.

flowchart LR
  subgraph today["today: processes"]
    m["master R process<br/>heap 1"]
    p2["worker process<br/>heap 2"]
    p3["worker process<br/>heap 3"]
    m -->|serialize| p2
    m -->|serialize| p3
  end
  subgraph future["hypothesis: one process"]
    i1["interpreter 1<br/>own heap"]
    i2["interpreter 2<br/>own heap"]
    i3["interpreter 3<br/>own heap"]
    shared[("shared immutable<br/>constants")]
    i1 & i2 & i3 --- shared
  end
  today ~~~ future

Process isolation (left) requires serialization and separate heaps. Multi-interpreter architecture (right) shares address space and can share caches and immutable data.

3.1.9 Open Questions

  • How should native package global state behave under multiple interpreters? Can packages be made thread-aware gradually?
  • Can promise forcing be made atomic without a large performance cost?
  • Is gradual migration toward apartment-aware packages realistic at the scale of CRAN?
  • How much of the base library can run safely in several apartments at once?
  • What do latency and throughput cost when values cross between apartments?

Further Reading

Where this appears

  • threaded-r Tackles most constraints by moving mutable state into per-interpreter apartments. The state inside native packages is the binding constraint.
  • r-in-rust (rrs) Avoids most constraints by design: no global mutable state in the interpreter, and one heap and collector per interpreter.
  • sunlight Works around process isolation startup cost via fork and copy-on-write. Does not address the underlying architectural constraints.

Where to go next

Back to top