15.3 Can Immutable Vectors Be Shared Across Interpreters?

DESIGN QUESTIONSSTATUS: OPEN QUESTIONLEVEL: DEEPREVIEWED: 2026-11-18
Before this page Serialization, Shared Memory

question-status: Open.

15.3.1 Context

Large read-only data is the payload that makes copying hurt. It has to be serialized to reach a worker, or shared without being copied at all. A single-cell analysis can hold an expression matrix of 20,000 genes by 100,000 cells. In an apartment-model design, each interpreter needs its own copy, copying a matrix of that size is expensive, and memory use multiplies with the interpreter count.

Sharing one read-only copy across interpreters would solve this. But R has no way to declare a vector immutable, and code can modify a vector whenever it holds the only reference to it. That makes a shared-immutable design hard.

15.3.2 Stakes

If immutable vectors can be shared safely, multi-interpreter parallelism becomes practical for data-parallel workloads: the data stays in one place, the interpreters scatter work across it, and memory use stays flat as the interpreter count grows.

If they cannot, each interpreter needs its own copy, and multi-interpreter parallelism suits only workloads with small, easily distributed data. Data-parallel work on large inputs keeps paying for one copy per interpreter.

15.3.3 Known constraints

R has no way to declare a vector immutable. A function receives a vector, modifies it, and returns it; copy-on-modify protects the caller’s copy, but nothing marks the object itself as read-only.

Reference counts change whenever a vector is bound, stored or passed, even if it is never modified, unless the count has been pinned at its maximum with MARK_NOT_MUTABLE. C code can do that, but it leaves the header, mark bits and attributes writable. GC bits change during every collection. ALTREP vectors can materialize when their data pointer is first requested. Attributes are mutable.

The SEXP header contains mark bits and generation list pointers, and a collection writes them even when the program only reads the vector. A read-only vector still gets marked and scanned.

Since R 4.0.0 the old NAMED field is computed from a reference count, which R uses to decide whether a vector can be modified in place or must be copied first. The count changes whenever a reference to the vector is made or dropped: binding it to a name, storing it in a list, passing it to a function. So code that only reads a vector still writes its header as it passes the vector around.

ALTREP vectors can defer their data. A compact integer sequence such as 1:1e6, for example, allocates its full contents only when something asks for its data pointer. That write is a side effect of reading, and it makes ALTREP vectors unsafe to share without coordination.

Attributes hang off the vector in a separate object but belong logically to it. If an attribute changes, the vector’s logical value changes, and R does not forbid changing the attributes of a vector nobody meant to modify.

15.3.4 Candidate approaches

Immortal or frozen object regions. Extend the immortal arena from the per-interpreter GC question to large data. A vector frozen at creation would never be collected, and its mark bit would stay permanently set, so no collector writes to it, and any interpreter could read it from a shared region. The cost is explicit opt-in: a programmer has to declare which vectors are frozen, and large data has to be built once and frozen before it is shared. CPython’s immortal objects (PEP 683) are the closest precedent.

Out-of-band buffers referenced by handle. Keep the data (numeric arrays, strings) in an external buffer store, and make R vectors handles to it. Sharing a vector means sharing a handle to a read-only, versioned buffer that the collector never scans. This is the Apache Arrow model. The cost is an indirection and a new object layout, and C code that reaches vector data directly would break.

Format-level sharing only. Serialize large vectors once to a columnar format such as Arrow or Parquet, and let each interpreter read its own copy. No synchronization is needed because the copies are independent, but the data is duplicated, so this is fast copying, not sharing. It suits data that crosses process or machine boundaries, not shared memory on one machine.

15.3.5 Evidence so far

No measurements exist for header-write traffic on read-heavy workloads. Does reading a vector cause enough mark-bit and reference-count writes to make sharing infeasible, or is the cost small enough to live with?

Neither threaded-r nor r-in-rust has prototyped shared immutable vectors yet.

Some R packages, arrow and polars among them, already keep data in out-of-band buffers. They show that a handle to such a buffer is workable from R, but an Arrow array is not an R vector, and base R functions see one only after a conversion. Full integration would need changes to evaluation, assignment, and the primitive operations.

15.3.6 Experiments needed

Measure header-write traffic on read-heavy workloads. Run benchmarks that read large vectors repeatedly without modifying them. How many header writes do collections and reference-count updates cause per pass over the data? Does contention on cache lines matter?

Prototype a frozen-vector region behind ALTREP. Create an ALTREP wrapper that represents a frozen vector in shared memory. Test that multiple interpreters can read it concurrently. Measure latency of access through the ALTREP layer. How much does indirection cost compared to native vectors?

Test immutability guarantees. If two interpreters hold a reference to the same frozen vector, can one interpreter verify that the other has not modified it? What are the performance costs of verification?

Benchmark serialization and deserialization of large matrices using Arrow format. Compare the cost of deserializing into each interpreter’s private copy versus accessing a shared immutable vector via indirection.

15.3.7 Current status

This question is open. The threaded-r and r-in-rust projects have not committed to an approach. The immortal region idea is promising and extends existing work. The out-of-band buffer approach requires deeper changes to vector semantics but offers cleaner separation of concerns.

Where this appears

  • threaded-r Large read-only datasets are where copying into every interpreter costs most, and sharing them would need new object semantics.
  • r-in-rust (rrs) As a new implementation, it could make immutability a first-class concept instead of retrofitting it.

Where to go next

Back to top