1.5 Data Movement Is Part of the Algorithm

WORKLOADSSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18

1.5.1 Data movement shapes the bottleneck

In a sequential program the computation sets the runtime. In a parallel program the data movement counts as well: how much each worker receives and how much it sends back. On my desktop, serializing a 400 MB matrix took about 0.7 seconds before a single byte had moved, and a gigabit network link needs at least eight seconds to carry a gigabyte. Either cost can become the slowest phase of the job.

1.5.2 The hidden cost of closures

A closure captures its environment. Send the closure to a worker and you send the environment along. Here is the concrete shape.

make_filter <- function(threshold, lookup_table) {
  lookup <- lookup_table  # capture it
  function(x) {
    x[x > threshold & x %in% lookup]
  }
}

filter_fn <- make_filter(10, runif(10000))
length(serialize(filter_fn, NULL))  # about 160 KB

The closure filter_fn keeps the environment of the make_filter() call, which holds threshold, lookup_table and lookup. When you serialize the closure to send it to a worker, serialization writes that whole environment. lookup and lookup_table are two bindings to the same 80 KB vector, and serialization writes the vector once for each binding, so the closure costs about 160 KB. The worker receives the closure plus all its captured data.

If you send this closure to every worker, every worker receives a copy of lookup_table. With thirty-two workers, you transmit the table thirty-two times.

1.5.3 Environment chains and parent references

Environments have parent environments. A closure’s captured environment points to a parent, which points to its parent, up to the global environment or a package namespace.

outer <- function() {
  big_data <- rnorm(1e6)

  inner <- function() {
    function(x) {
      x + mean(big_data)
    }
  }

  inner()
}

process_closure <- outer()
length(serialize(process_closure, NULL))  # about 8 MB

The innermost closure’s own environment is the frame of the inner() call, which is empty. Its parent is the frame of the outer() call, which holds big_data and inner. Serialization does not stop at the closure’s immediate environment: it walks the parent chain and writes every frame it meets, so process_closure serializes to about 8 MB, the size of big_data.

The walk stops at the global environment, at package namespaces and at the base environment. R writes those as references, and their contents do not travel. A function defined at top level therefore carries none of your global objects with it, which is why a PSOCK worker reports a missing object until you clusterExport() it. Inside the frames it does write, serialization cannot know which bindings the function uses, so it writes all of them.

1.5.4 Full copies in flight

When the main process serializes an object, the object exists in the main heap. Serialization converts it to bytes. Those bytes travel to the worker. Deserialization allocates new memory on the worker heap and rebuilds the object as a full copy. The object now exists twice: main heap and worker heap. With N workers receiving the same data, the object exists N+1 times simultaneously.

big_matrix <- matrix(rnorm(1e7), nrow=1000)  # ~80 MB
results <- parallel_map(1:32, function(i) {
  use_matrix(big_matrix, i)  # matrix serialized to all 32 workers
})

For this pattern, peak memory includes the original on the main process plus 32 copies on workers. That is 33 copies of 80 MB, or 2.6 gigabytes, for a matrix that only 32 workers read in parallel.

1.5.5 Shared immutable data

The way to avoid this is to share data immutably. Do not move it. Workers read pointers to data that lives once, on the main process or a shared store.

This approach requires:

  1. The data structure exists before tasks begin. No task-generating phase builds different data for each task.
  2. The data is immutable during task execution. No task writes to the shared structure.
  3. Workers must reach the same memory. Threads do this through one address space. Separate processes can share physical pages only through the operating system, by fork’s copy-on-write, by mapping the same file, or by mapping a shared-memory object.

Spawned R workers fail item 3 by default. A pointer from the main process means nothing in a worker’s address space, so the data is serialized and copied, unless both sides map the same file.

Threads in one process satisfy item 3: one thread allocates the matrix, and another reads it through the same pointer with no copy. Several interpreters in one process, as in threaded-r, share the address space but not the heap, because each interpreter owns its heap. Passing an R object between them without a copy is still a design goal there.

1.5.6 Zero-copy claims and what they actually mean

Some parallel systems advertise zero-copy data movement: the data lives in memory once, and a worker receives an address or a handle instead of a byte stream. The object’s bytes stay in place and only the address moves.

That is not free. The receiver still needs a message saying where the data is and that it is safe to read, and between processes it needs a shared mapping that the operating system sets up. Zero-copy also does not survive writes. Shared pages stay shared only while nobody writes them, and in a forked R worker the garbage collector writes object headers, so pages meant to be shared become private copies.

sunlight preloads packages in a template process and forks its workers from it, so the packages are never serialized and no worker loads them again. The memory does not stay shared: sunlight’s measurements found that a worker’s first garbage collection privatizes the inherited pages, so before its first task a forked worker holds a private copy of most of the template’s heap (fork, copy-on-write, and the first GC). The task function and its arguments are still serialized and sent with every chunk of work.

threaded-r’s THREAD cluster serializes the objects it passes between interpreters, as PSOCK does, but through memory instead of a socket. Each interpreter owns its own heap.

1.5.7 Moving less data

Partition early. Split the input before starting tasks, so each worker receives only its slice and no large object goes to every worker.

Preload what workers need. On Unix, load reference data (lookup tables, model coefficients, geographic boundaries) in the parent before forking, and the workers inherit it without serialization. On spawned workers, send it once per worker with clusterExport() or mirai’s everywhere(), not once per task.

Compute locally. If a worker can generate its own random data instead of receiving serialized data, the savings are immediate. One call to rnorm() beats deserializing a vector.

Return only summaries. Workers compute on huge datasets but return only small results. One worker processes a terabyte of genomic data and returns a p-value. The return trip costs microseconds instead of seconds.

Avoid implicit environment capture. Write functions that accept data as explicit arguments, and do not create the worker function inside a function whose frame holds large objects. The future package works differently from parallel here: it finds the global variables a function uses and exports them with each future, so a large global named in the function body travels as well.

1.5.8 The architecture signal from data movement costs

When data movement dominates your parallel runtime, it signals a mismatch between your parallelism strategy and your workload.

If serialization takes ten seconds and computation takes one second, you chose the wrong tool. The cost of moving data overwhelms the speedup.

If you send a gigabyte to each worker every iteration, ask whether that data changes. If it does not, send it once and keep it on the workers between iterations.

If the same large function, or the data its environment holds, travels with every task, the transmission is never amortized. Forking after the function exists, as mclapply() does, lets the children inherit it without serialization. With persistent workers, export it once with clusterExport() or mirai’s everywhere() and call it by name in each task.

Further reading

  • R Core Team, Package ‘parallel’ <https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf>, the package vignette, with worked examples that export data to workers.
  • Serialization. How R turns objects into bytes.
  • Closures. What a closure keeps alive.
  • Environments. Frames and the parent chain.

Where this appears

  • sunlight Packages preloaded in the template process are never serialized or reloaded by the forked workers.
  • threaded-r Interpreters share one address space but not a heap, and the THREAD cluster still serializes what it passes between them.

Where to go next

Back to top