17.2 Why the Apartment Model First?
17.2.1 Question
Should I build isolated interpreters with one owner thread each, or aim for a shared-interpreter model with free threading?
17.2.2 Context
GNU R is single-threaded by design. One interpreter per process. Parallelism comes from running multiple processes.
There are two contrasting visions for parallelism in R. The apartment model isolates interpreters. Each interpreter has one owner thread. Interpreters do not share mutable state. The shared-interpreter model has one interpreter shared by multiple threads. All threads can access all objects concurrently. Synchronization protects shared state.
The apartment model is simpler. It preserves the single-threaded assumption inside each apartment. The shared-interpreter model requires retrofitting synchronization into code that was never designed for it. This is hard and error-prone.
17.2.3 Constraints
Native packages assume single-threaded process-global state. A C function can store state in a static variable, allocate on a global heap, or call functions that do these things. Single-threaded code is not thread-safe. GNU R is single-threaded and its C API is not thread-safe. There is no lock an extension can acquire or release. If two threads touch the same static variable in sequence (not concurrently), they corrupt state through cross-interpreter state leakage and violated ownership or lifecycle assumptions inside the library.
Example: an R package wrapper around a C library can initialize the library once in a static variable. The C library expects single-threaded use. Two threads calling into the package reach that one variable, and serializing them with a lock does not help. The accesses stop overlapping, and the library still sees one caller’s state where it expected another’s. A lock fixes concurrency. It does not fix the assumption that there is only ever one caller.
Package authors do not rewrite their C code when R’s threading model changes. If R moved to shared-interpreter threading, compiled code written for one caller would go on assuming one caller, and the first package to meet a second thread would fail.
Performance is another constraint. Adding atomics and synchronization to single-threaded code adds cost to every operation it touches. A field that was never shared gets locked anyway, a function that was not reentrant has to become reentrant, and every optimization that assumed a single thread has to be removed or made safe. Retrofitting free threading is expensive.
Memory is the cost on the other side. Each interpreter needs its own symbol table, base environment and heap. threaded-r measured 20.4 MB of live R objects in one freshly bootstrapped R, which scales to 163 MB at eight interpreters (docs/p4/duplication-cost.md), before any user data.
17.2.4 Options considered
Shared interpreter with free threading. One interpreter, many threads. All threads access all objects. Use locks and atomics to protect shared state. This is the direction of CPython’s free-threaded build and the Java approach. Benefits: code can share data freely, with no copying and low latency. Costs: every C package must be retrofitted, every shared operation pays for atomics, and the retrofit is subtle and error-prone.
Apartment model with isolated interpreters. Each interpreter has one owner thread. Interpreters are isolated. Sharing happens at the serialization level. Multiple workers can run in parallel. Each works on independent data. Benefits: R-level state is isolated without touching package source, and each apartment keeps the single-threaded assumption a package was written against. Costs: data must be copied, large data copy is expensive, sharing a large dataset requires message passing, and native statics stay process-global, so packages still need per-package work.
Hybrid model. Start with apartments. Graduate to free threading later as a separate layer. This provides a transition path. Early adopters get isolation. Later, a subset of packages opt into thread-safe sharing. Benefits: staged rollout, no flag-day conversion. Costs: two threading models to maintain, API surface must support both.
17.2.5 Decision
I am building the apartment model first: isolated interpreters, one owner thread each, sharing through serialization. Free-threaded shared interpreters stay future research.
The reason is sequencing, not preference. Apartments can be built against the package ecosystem that exists today, and free threading cannot.
17.2.6 Reasons
Native packages are the deciding constraint. Forking GNU R kept compatibility with the C API, which left one problem: how to add parallelism without breaking packages.
Apartment isolation buys back the single-threaded assumption at the R level, which is most of it. What it cannot reach is the C statics inside a shared library, because there is one copy of those per process no matter how many interpreters run above them. So native packages still need per-package work. The bet is that this work is smaller and more tractable than retrofitting synchronization into every package, not that it is zero.
Free threading would ask the authors of thousands of compiled packages to make their code thread-safe. Some of those packages have no active maintainer, so the retrofit would never finish, and every package left behind could crash the first time a second thread reached it.
The cost is per-package work, and the record shows both halves of that. When the corpus first ran against a THREAD worker on 2026-08-16, three of the twenty-nine base and recommended packages were declared capable. By 2026-08-20 twenty-eight of them ran on a worker, Matrix included, the largest single migration the project has done, and the twenty-ninth, tcltk, stays on the main interpreter by decision. The work is real and goes package by package, and for this corpus it finished.
methods is the instructive case. Its native statics were migrated in patch 0077 and it still could not be declared capable: the crash that blocked it was in R’s own lookup of native routines for .Call(), not in methods. When it was declared capable on 2026-08-17 (patch 0106), the attempt exposed two more process-wide races in base R itself, in the S4 construction code and in the radix sort behind order() and sort() (patches 0107 and 0108). Migrating a package’s own state is necessary and not sufficient.
Performance is another factor. An uncontended atomic operation costs more than a plain one, and a lock costs more than no lock. A free-threaded retrofit pays that on paths that never see a second thread, and isolation avoids it.
17.2.7 Capability levels
I frame this as three capability levels:
Level 1: Owner-thread R with parallel native kernels. One interpreter per process, on its owner thread. Parallelism comes from native kernels, such as a threaded BLAS or an OpenMP loop, that run on other threads without calling back into R. GNU R already offers this.
Level 2: Isolated interpreters with no SEXP crossing. Several interpreters per process, one owner thread each. No object crosses between interpreters; values cross by serialization. This is what threaded-r implements, through makeCluster(type = "THREAD") and its channels. Sharing large read-only objects between interpreters would extend it, and is not built (see the shared immutable vectors question).
Level 3: Free-threaded shared interpreter. One interpreter, many threads, full sharing. It requires retrofitting native packages and solving the compatibility problem, so I keep it as research for now.
17.2.8 Consequences
The apartment model uses more memory: each interpreter has its own symbol table, base environment and heap. At 20.4 MB of live objects per interpreter, the project judged 163 MB at eight interpreters “not obviously unacceptable” on a machine that would run eight of them (docs/p4/duplication-cost.md), and the cost grows linearly with the count.
Shared data is slower to use. A large matrix cannot be read by every interpreter at once without synchronization, so today it is serialized and copied into each interpreter, which costs time and memory.
Packages split into those declared capable on a worker and those that are not, and a user has to know which is which.
Package code that keeps state in C statics stays a problem. In an apartment, a static is still one copy per process, shared by every interpreter, which is why threaded-r keeps an undeclared package’s native code with the interpreter that loaded it first. Under level 3, many threads would reach the same static at once. Nothing can force package authors to fix this, so users have to choose the threading model by the packages they use.
17.2.9 Evidence that would change my mind
If apartment isolation proves too weak for the workloads that matter, I reconsider. For instance, if users need to share large mutable data between all interpreters, isolation becomes the bottleneck. If serialization turns out cheap enough, isolation stays viable; if it is so slow that it cancels the gain from parallelism, shared-interpreter threading becomes attractive despite its costs.
If per-interpreter memory overhead defeats the purpose, I reconsider. The base state scaled from that one measurement, 163 MB at eight interpreters, is not the problem at that scale. User data copied into every interpreter can still be.
If a significant fraction of CRAN packages prove impossible to run in apartments, isolation is insufficient. The capability corpus so far covers only the 29 base and recommended packages, so this is still open for CRAN at large.
If the native-package ecosystem moves toward threading, with packages using atomics and locks, the retrofit cost falls. If packages begin opting into shared-interpreter threading, a migration path opens, but that change would be slow.
Where this appears
- threaded-r Architectural decision that shapes the multi-interpreter approach.