15.1 Can R Environments Safely Be Shared?

DESIGN QUESTIONSSTATUS: OPEN QUESTIONLEVEL: DEEPREVIEWED: 2026-11-18
Before this page Environments, Race Conditions

question-status: Open.

15.1.1 Context

Environments in R are reference-semantics mutable maps. They hold bindings from name to value. Because environments are references, two pointers to the same environment see each other’s writes immediately.

In a multi-interpreter design, sharing a large environment across interpreters can avoid copying costs. But environments mutate during normal evaluation. A binding can change. A new binding can be added. Lazy promises stored in frames force on read, which writes into the frame.

The garbage collector also writes bookkeeping into every reachable object. This happens even on read-only accesses. These writes make safe sharing hard.

15.1.2 Stakes

Large shared data often lives in an environment, and every loaded package is a namespace environment with hundreds or thousands of bindings. In apartment-model designs, each interpreter has its own copy. Copying costs memory and time; sharing saves both but creates a synchronization problem.

If environments can be safely shared, multi-interpreter parallelism becomes practical for workloads that import the same large packages. If not, each interpreter pays the copy cost.

15.1.3 Known constraints

An environment stays mutable unless something locks it, and most are never locked: the global environment, function frames, and the environments packages keep as mutable state. Any code that can reach one can change a binding in it.

Lazy evaluation adds a hidden mutation. Forcing a promise stored in an environment frame evaluates its expression and writes the value into the promise.

The garbage collector reaches every object reachable from a root and writes a mark bit into each object’s header to record that it is live. These writes happen whether the program is reading or writing. The GC treats every object it reaches as its own, but in a shared-environment design an object can belong to several interpreters.

R already has locking. lockEnvironment() stops new bindings from being added, and with bindings = TRUE it also locks the existing ones; R seals every package namespace this way after loading it. A locked environment is still not safe to share. Many namespace bindings are lazy-load promises that write their value when first forced, and the collector writes into every object it marks.

Locking an environment fixes its bindings, not the objects they point to. A locked binding can hold a mutable environment, and at R level nothing marks an object itself as immutable.

15.1.4 Candidate approaches

Copy environments on transfer. threaded-r’s channels do this today: they serialize a value and rebuild it in the receiving interpreter’s heap. Each interpreter’s environments stay isolated and no synchronization is needed. The cost is memory and copy time, which I expect to grow with the environment; it has not been measured.

Can frozen environments be shared without synchronization? An environment locked with its bindings before any other interpreter can reach it cannot be rebound by anyone. But its lazy-load promises still write when forced, the collector still marks its objects, and a locked binding can still hold a mutable object. This approach would limit sharing to a small set of explicitly frozen, fully forced objects, and it is not clear that set is large enough to matter.

Lock-per-environment synchronization is a possible design, though reasoning suggests that mutex contention on every binding access creates overhead. Additionally, lazy promises still mutate on read. Even a read-only access from the application’s perspective forces a promise and dirties the object. Environment locks alone cannot synchronize with the GC’s internal writes.

Some systems use conflict-free replicated data types to share maps. Each interpreter keeps a local copy. Updates propagate via message channels. Conflicts merge using a deterministic rule. Or, one interpreter owns the environment. Other interpreters send messages to read or modify. This approach decouples the GC and concurrency models. But it adds latency and complexity. Code written assuming shared-memory environments breaks under message semantics.

15.1.5 Evidence so far

No measurements exist. The design space is open.

threaded-r has not tested shared environments; it copies on transfer. r-in-rust keeps its object layout behind opaque C headers, so it could try sharing designs that GNU R’s layout rules out. Neither project has measured the memory or time cost of copying against sharing.

15.1.6 Experiments needed

First, measure the copy cost. How much time and memory does copying a 1,000-binding environment cost? At what size does sharing save more than its synchronization costs?

Second, prototype a shared-environment mode with a lock per environment. Measure the mutex contention. How often does a binding access block? Does contention scale with thread count?

Third, prototype CRDT or message-based mirrors. This requires new evaluation semantics. Code that writes to a shared environment sends a message instead of writing directly. Measure latency and throughput. Compare to locking.

Fourth, quantify the GC write problem. How many bytes does the GC write during a collection? Do these writes concentrate on headers that are accessed frequently? If so, are there heap layouts that reduce false sharing?

15.1.7 Current status

This question is open. Both threaded-r and r-in-rust can explore it.

Where this appears

  • threaded-r Multi-interpreter design must decide whether to copy environments on transfer or permit selective sharing with synchronization.
  • r-in-rust (rrs) Owns its object layout behind opaque C headers, so it can explore sharing designs that GNU R’s layout rules out.

Where to go next

Back to top