18.3 sunlight

PROJECTSSTATUS: DEVELOPINGLEVEL: INTROREVIEWED: 2026-11-15

sunlight is my R package for parallel workers on unmodified GNU R, on POSIX systems only. It uses a forkserver design: a template R process loads a declared set of packages once, plus an optional setup expression, and then forks the worker processes. Each worker inherits the loaded state through copy-on-write, so it skips R startup and every library() call. R itself is not modified.

18.3.1 The real advantage: warm-start elasticity

A new worker under the usual approaches is a fresh R process, and it has to start R and then load every package itself: read each namespace’s lazy-load database, load its shared libraries, run its load hooks. With a stack like Seurat that takes seconds. A forked worker has all of it already.

My own benchmark run of 2026-08-01 shows that the larger advantage is in pools that grow and shrink, not in the first cold start. A worker added to a pool whose template is already running costs a fork and a handshake, not a fresh R process.

config sunlight cold sunlight warm best incumbent cold cold ratio warm ratio
8 workers, tidyverse 0.72 s 0.22 s 1.29 s (parallel) 1.8x 5.9x
32 workers, Seurat 4.82 s 4.50 s 14.87 s (mirai) 3.1x 3.3x

Standing up workers from a warm template was 3.3x to 5.9x faster than the best incumbent’s cold start. An earlier cold-cache run measured the same effect at 3.6x to 4.4x.

Three caveats apply to this measurement. First, run order biased the results in sunlight’s favor. All five frameworks ran in sequence in one process, with sunlight last, so it benefited from a warm filesystem cache. The same 8-worker cold start reads 0.72 s in this run and 1.48 s in the earlier cold-cache run, where it was behind the best incumbent’s 1.20 s; both are recorded. Second, this is WSL2 on Windows 11, not bare-metal Linux, and a bare-metal reproduction is still owed. Third, crew launched only 5 of its 8 configured workers and 8 of its 32 under this harness, so its memory figures describe fewer workers than configured and it is left out of the memory comparison.

18.3.2 Kill criteria and what I learned

I set two kill criteria before measuring: 10x on cold start to first result and 5x on aggregate memory, both against the best incumbent. Both fail. The best cold-start ratio is 3.1x, and even the warm-start ratio, 5.9x at best, is under 10x. The memory results:

config sunlight PSS best incumbent RSS headroom
8 workers, tidyverse 819.6 MB 1048.1 MB (parallel) 1.28x
32 workers, Seurat 11667.7 MB 12808.0 MB (future) 1.10x

That is 1.10x to 1.28x against a 5x goal. The package DESCRIPTION used to say the aggregate memory footprint drops sharply, and I removed that claim after the measurement instead of moving the goal. The page on copy-on-write explains the shortfall: a full garbage collection in a worker writes mark bits into every reachable object, which privatizes the pages the template was supposed to share, and in the measured workers nearly the whole heap was private before the first task.

The two columns use different measures. sunlight’s aggregate is PSS (proportional set size), which splits each shared page among the processes sharing it. The incumbents’ aggregate is RSS (resident set size), which counts a shared page once in every process that maps it, and their PSS was not recorded. The vignette treats the two as equal for separately started processes, but those processes still share the pages of mapped shared libraries, so the comparison leans somewhat toward sunlight.

18.3.3 Reproducibility and packaging

Per-task L’Ecuyer-CMRG streams are bit-identical at any worker count or chunking, so reproducible RNG carries through a parallel map without manual setup.

Task functions must be self-contained: sunlight checks their free variables when a task is spawned and reports unresolved names by name. Inputs are serialized and snapshotted at spawn, so later changes in the caller cannot reach a task. Results come back in input order. Warnings, messages and output are captured on the worker and replayed in the caller’s session. A thread governor, on by default, clamps each worker’s BLAS, OpenMP and data.table thread counts to a fair share of the machine: the number of cores divided by the number of workers, rounded down, and never less than one.

The conformance suite holds 197 tests at version 0.1.0. Adapters connect to crew, targets and mirai. sunlight_register_serial() lets a class of external pointer (arrow, torch and similar) cross the pool boundary, which the default rejects.

18.3.4 Usage

library(sunlight)

pool <- hatch(8, preload = c("dplyr", "tidyr"))

# dplyr is already loaded on every worker. `n` is a shared input: pmap()
# passes it by name and snapshots it when the call starts.
n <- 20
results <- pmap(pool, 1:100, function(i, n) {
  dplyr::n_distinct(sample(n, i, replace = TRUE))
}, n = n)

shutdown(pool)

hatch() starts the template and forks the pool. pmap() is the parallel map. By default it cuts the input into roughly 16 chunks per worker, striding so that neighboring elements land in different chunks, freezes every chunk up front, and hands the next chunk from one queue in the parent to whichever worker is free. That is demand-driven dispatch, not work stealing, although the package NEWS calls it work stealing. shutdown() is idempotent and reaps every worker. Beside pmap, built on the same scheduler, sits the task API: spawn() to submit, is_ready() to poll, await() to collect, snapshot() to see the inputs a task received, and cancel() for cooperative cancellation.

18.3.5 Current state

sunlight is at version 0.1.0, MIT licensed, and requires R >= 4.1 and a POSIX system with fork(). It is not on CRAN; it installs from source. A 197-test conformance suite checks its behavior, and adapters exist for crew, targets and mirai. The measurements on this page come from the benchmark run of 2026-08-01 on WSL2, and they are my own, not an independent replication.

18.3.6 Lessons

The memory saving that copy-on-write promises mostly does not survive R: a full garbage collection in a forked worker writes into nearly every page of R objects it inherited, and the measured workers had lost about four fifths of their sharing before their first task. What is left is the startup saving, and it is largest when a pool keeps adding workers to a template that is already loaded.

18.3.7 Repository

The sunlight repository is not public yet.

Further reading

Where to go next

Back to top