14.5 Copying vs Zero-Copy Transfer
14.5.1 The choice
When one thread or interpreter sends data to another, the data must move from source to destination. Copying means the destination allocates new memory and duplicates every byte. Zero-copy transfer means both can read the same memory without duplication.
Copying is safe. Zero-copy transfer is fast but complex.
14.5.2 Overview
This choice appears in both inter-process and inter-interpreter communication. It couples tightly with isolation: isolated interpreters naturally copy data, while shared heaps use pointers.
The choice affects latency and throughput. For small data, copying is fast enough. For large data, the copy can dominate the work.
14.5.3 Option A: Copying
Thread A serializes its data to a buffer. Thread B reads the buffer and deserializes into its own heap. Both own their copy.
Correctness is easy to argue. Each thread owns its data, no synchronization is needed on the data itself, and modifications in one copy do not affect the other.
Cost: time. A vector with ten million integers requires 40 MB of memcpy. At 10 GB/s, that is four milliseconds. A workflow that sends a million such vectors spends over an hour copying.
Cost: memory. Peak memory is at least twice the original, because the source still holds the data while the destination is copying. A serialized buffer in between can make it three times.
Example: a bioinformatics workflow processes gene expression matrices. Each matrix is 20,000 genes by 500 samples of doubles, 80 MB. Copying fifty of them moves 4 GB, about 0.4 seconds at 10 GB/s. Against 30 seconds of analysis, that is about 1 percent.
14.5.4 Option B: Zero-Copy Transfer
Memory is mapped into both interpreters. Both hold pointers to the same bytes. No copy is needed.
Cost: the data must be immutable. If one interpreter modifies the data, the other sees the change (bad). The data must be registered as immutable and protected by hardware or software.
Cost: the transfer mechanism is complex. Sharing a heap-allocated object across process boundaries requires OS support (mmap, shared memory regions). The object layout must be identical on both sides (same word size, same alignment).
Cost: cleanup is tricky. When the receiver is done, it often cannot free the memory by itself, because the sender or another reader may still use it. Someone has to know when the last user is done. A reference-counting system or explicit lifetime management is required.
Example: a training script loads a 1 GB validation set once and passes it to a hundred workers. Copying moves 100 GB through memory and holds a hundred copies. Mapping the same physical pages into every worker avoids both.
14.5.5 Scored on the nine dimensions
| Dimension | Copying | Zero-Copy Transfer |
|---|---|---|
| Isolation | Complete, destination owns its copy | Weak, both read the same memory |
| Data movement | Full copy required, proportional to data size | Single transfer of pointer, constant time |
| Startup cost | Identical for both | Identical for both |
| Steady-state overhead | Linear in data size | Constant: only a pointer or handle moves |
| Memory behavior | Peak memory at least 2x original size | Peak memory is 1x, mapping is in-place |
| Portability | All platforms support memcpy | Requires OS support for shared memory or mmap |
| Package compatibility | Straightforward: each copy is independent | Complex: requires immutability enforcement |
| Failure containment | Corruption in one copy does not affect others | Corruption is visible to all readers, requires write protection |
| Determinism | Deterministic if copy is deterministic | Deterministic if mapping is deterministic |
14.5.6 Where each one wins
Copying wins when data is small (strings, scalars) or modification is expected (each receiver modifies its copy). It also wins when code correctness matters more than speed.
Copying is the safe default. Mistakes in immutability enforcement cannot corrupt shared state. Copy-on-write heaps amortize the cost.
Zero-copy wins when data is large (matrices, data frames) and read-only (analysis, training on static datasets). It also wins when latency is critical (interactive dashboards, real-time filters).
Devices make the same tradeoff. GPU APIs copy between host and device memory by default, and offer pinned or unified memory so the device can read a host buffer without a staging copy.
Hybrid systems are practical. Small data copies. Large data maps into shared memory if immutable. Fork copy-on-write, which sunlight uses, is one version of this: every worker starts by sharing all of the template process’s pages.
14.5.7 R today
GNU R does not face this choice. One process, one interpreter, and all data lives in one heap. No transfer is needed.
The parallel package copies. A socket worker receives everything by serialization: clusterExport sends named variables, and parLapply serializes the function and each chunk of the data. mclapply forks instead, so the child starts with the parent’s whole heap through copy-on-write pages, and only results are serialized back.
sunlight uses fork copy-on-write. The template process and its workers share physical memory pages until one of them writes, and the first write to a page gives the writer a private copy. sunlight’s Day 7 benchmark measured its PSS against the best incumbent’s RSS at 819.6 MB against 1048.1 MB with 8 workers (1.28x), and 11,667.7 MB against 12,808.0 MB with 32 workers (1.10x). Setting PSS against RSS biased those numbers toward sunlight; the run was on WSL2, and crew is excluded from the comparison because it did not launch all its workers.
The difference is small partly because R’s collector writes a mark bit into the header of every live object it reaches, so a full collection in a worker can copy every page that holds one. Fork, Copy-on-Write, and the First GC is the experiment for this.
On Unix-alikes, R inherits the process model: data in a forked child is logically a copy from birth. The kernel makes that cheap through copy-on-write, but the programming model assumes copying.
14.5.8 Relevance to my work
threaded-r uses copying for inter-interpreter data transfer today. Each interpreter owns its heap, and values cross between interpreters by serialization over channels.
Explicitly shared immutable vectors can reduce copying overhead. A vector marked immutable and pinned in the immortal arena can be read by all interpreters without copying. This requires enforcement at the language level.
r-in-rust keeps no global mutable state in its interpreter, so copying is the natural default for moving data between its interpreters.
What are the tradeoffs between copying and zero-copy for R workloads? A measurement comparing serialization time, copy time, and work time will clarify the break-even point.
Where this appears
- threaded-r Uses copying for inter-interpreter data transfer. Explores zero-copy via shared immutable vectors.
- r-in-rust (rrs) Each interpreter owns its data. Transfer requires copying.
- sunlight Uses fork copy-on-write to share parent process state with workers initially.