14.1 Processes vs Threads vs Interpreters
14.1.1 The choice
This comparison addresses how to execute parallel R code across multiple CPU cores. Three strategies exist: spawn separate OS processes, share one interpreter across OS threads, or run multiple isolated interpreters within one process.
Each approach has different implications for shared data, startup time, and compatibility with native packages.
14.1.2 Overview
The question grows with the number of workers. For a single worker, startup overhead is acceptable. For thirty-two workers running short tasks, startup can dominate the wall clock time. Whether native packages run unchanged decides whether a design is usable by most R code at all.
The choice also decides how data moves, whether synchronization is needed, what the API looks like, and which workloads run efficiently.
14.1.3 Option A: OS Processes
A separate process is a complete isolated runtime. Each process has its own heap, symbol table, global environment, and memory space. The operating system manages isolation at the page level.
Parallelism works: each process runs on a separate core. Execution continues independently. One process cannot read another’s memory directly. Sharing data requires explicit serialization or return values. Processes that crash do not affect others.
Processes are R’s own answer. The parallel package arrived in R 2.14.0, folding in the earlier snow and multicore packages. A socket (PSOCK) worker is a fresh R process that loads its packages before it begins work. mclapply forks the current process on Unix-alikes instead, so a child starts with everything the parent had loaded. Either way the cost is at startup; once running, a worker needs no synchronization.
Startup cost can dominate short tasks. A heavy package such as Seurat takes seconds to load before any user code runs, and every cold worker pays that again. A task of a few seconds gains little from a worker that spends most of its life starting. A prewarmed forkserver, the approach sunlight takes, pays the loading cost once in a template process and forks workers from it.
14.1.5 Option C: Multiple Interpreters in One Process
Several complete interpreters run on separate threads within one process. Each interpreter owns its own heap, symbol table, and global state. See multi-interpreter architecture for internals.
No SEXP crosses a boundary without serialization. Data sharing requires copying or explicit message passing. Each interpreter runs without locks on its own state, so no contention exists on interpreter-local data.
Native packages work in one interpreter at a time. Package C code sees what it expects: one interpreter, one global environment. It does not get private copies of its C statics, though. A shared object is mapped once per process, so two interpreters that load the same package share its static variables. threaded-r confines each shared object to the first interpreter that loads it, unless the package declares itself capable; Packages and Namespaces Across Interpreters has the details.
Example: a binding that keeps a GPU context in a static variable. Two interpreters that both load it would see the same context, so either the package moves that state into per-interpreter storage, or it stays confined to one interpreter.
14.1.6 Scored on the nine dimensions
| Dimension | OS Processes | Shared Interpreter | Isolated Interpreters |
|---|---|---|---|
| Isolation | Complete process isolation enforced by OS | Requires synchronization on all shared state | Complete per-interpreter isolation by design |
| Data movement | Explicit copy via fork or serialization | In-memory pointer access, no copy | Copy or message-passing required |
| Startup cost | High for a cold process that starts R and loads packages; low for a fork of a warm parent | Low: existing interpreter reused | Medium: a new interpreter, no new process |
| Steady-state overhead | Low: no sync overhead once running | High: lock and atomic costs on every access | Low: no locks within interpreter |
| Memory behavior | A full runtime per process; fork copy-on-write shares pages until they are written | One heap, one symbol table shared across threads | A heap per interpreter, one copy of the code |
| Portability | Every platform can spawn processes; fork is Unix-only | Needs OS threads, which every platform R supports has | Needs OS threads; threaded-r builds and runs on Linux and Windows, not yet on macOS |
| Package compatibility | Very high: native packages work unchanged | Very low: C packages race unless retrofitted | High in one interpreter; several interpreters need the package confined or declared capable |
| Failure containment | Process crash does not affect others | A native crash kills the process and every thread in it | An R error stays in its interpreter; a native crash kills the whole process |
| Determinism | Fully reproducible with fixed seeds and message order | Requires memory ordering, happens-before semantics | Reproducible if message order is deterministic |
14.1.7 Where each one wins
OS processes win when startup cost is already paid once (prewarmed forkserver), cold starts do not matter (batch jobs), or maximum compatibility is essential. Fork copy-on-write is efficient for workloads that read most data and write little.
Processes also win when the codebase was never designed for concurrency. Legacy packages and old code that manipulate global state work unchanged. No retrofit is needed.
Shared interpreters win when data is truly mutable and shared. All threads need to modify one large structure. The codebase must be designed for threading (Python with thread-safe packages, Java). Free threading avoids copy overhead and latency.
Shared interpreters excel in domains where data is partially mutable. A model that is read by all threads but updated slowly is natural to share.
Isolated interpreters win when native packages matter (R’s constraint), data is naturally partitioned (map-reduce workloads), and startup cost is amortized (long-running workers). They are ideal for stateless processing: each worker processes one task and returns results.
14.1.8 R today
GNU R runs one interpreter per process, and that interpreter evaluates R code on one thread. Some C code inside R, and the BLAS it links, can use threads of their own.
The parallel package works around this with OS processes. mclapply forks the parent on Unix-alikes, so a child starts with the parent’s packages loaded and shares its memory pages copy-on-write. makeCluster starts fresh R processes connected by sockets, and each one loads its own packages. Since R 4.0.0, a PSOCK cluster on one machine starts its workers in parallel, so startup no longer grows with the worker count the way it did.
sunlight’s own Day 7 benchmark, at 32 workers with Seurat loaded, measured a warm start of 4.50 s and a cold start of 4.82 s, against 14.87 s for the fastest incumbent (mirai): 3.3x warm and 3.1x cold. Two caveats travel with those numbers. All five frameworks ran in one process in a fixed order with sunlight last, against a warm filesystem cache. The run was on WSL2 on Windows 11, and a bare-metal Linux reproduction is still owed.
GNU R has no shared-interpreter threading. Writing R Extensions calls any use of the R API from threaded code “for experts only” and strongly discouraged, because many API functions modify R’s internal data structures and may corrupt them if called from several threads at once.
14.1.9 Relevance to my work
threaded-r chose isolated interpreters in one process. Compiled packages keep the single-interpreter view they were written for, and a new worker costs an interpreter instead of a process. Data movement costs more than under free threading, because values cross between interpreters by serialization.
r-in-rust uses isolated interpreters by design. The interpreter crates have no global mutable state, and a gate check enforces that. Only the C API crates keep a short, reviewed allow list. Each interpreter owns its heap, garbage collector and symbol table from the start, so there is no per-process initialization order to get wrong.
sunlight keeps OS processes and attacks their startup cost instead: a template process loads packages once and workers are forked from it. Its measured gain is largest on warm start, and it fell short of its own kill criteria (10x on cold start, 5x on aggregate memory), which the project recorded. Whether a prewarmed process pool can come close to in-process interpreters on startup is still open.
Does threaded-r need to optimize inter-interpreter communication latency? If message passing becomes a bottleneck, explicit zero-copy shared immutable data becomes attractive. But this requires type-level immutability enforcement.
Where this appears
- threaded-r Chose multi-interpreter architecture: threads within one process, isolated interpreters.
- r-in-rust (rrs) Explores a from-scratch design with multiple interpreters sharing no mutable state.
- sunlight Uses OS processes and fork copy-on-write. Explores prewarmed forkserver trade-offs.