6 R Runtime Internals

R RUNTIMEREVIEWED: 2026-09-13

You have used R for years without seeing its implementation. This section opens up GNU R at the places you have already run into from the R prompt: lazy evaluation, copy-on-modify, garbage collection, and the odd reference behavior of environments.

Anyone who tries to parallelize R meets these internals. The features that make R pleasant for data analysis are the same ones that make threading it hard.

After this section

  • Trace what happens when R evaluates an assignment or function call
  • Explain why R appears to copy vectors on assignment and when it does not
  • Describe how R’s garbage collector works and why a collection stops all other work on the heap
  • Define environment, closure, and promise. Connect each to R’s semantics
  • Read and understand the SEXPREC structure and what fields matter for parallelism

The pages

  1. How R Executes Code. The interpreter loop, evaluation steps, and how one thread runs them.
  2. What Is an R Object?. How R represents values and how that representation blocks parallelism.
  3. SEXPs. The C structure that holds every R value.
  4. Copy-on-Modify, NAMED, and Reference Tracking. Why R does not copy on assignment, when it does, and how reference counting decides.
  5. ALTREP and Deferred Representation. Compact vector representations, materialization on read, and memory efficiency.
  6. Environments. Name bindings, scope, and the chain of parent environments.
  7. Closures. Functions that carry their environment with them.
  8. Promises and Lazy Evaluation. Why unevaluated arguments are dangerous in parallel code.
  9. Conditions, Errors, and Unwinding. The condition system, handlers, cleanup, and stack unwinding on errors.
  10. Garbage Collection. Why the garbage collector is the synchronization bottleneck in multithreaded R.
  11. Connections, External Pointers, and Finalizers. Resources outside R, cleanup, and why they cannot be copied or shared.
  12. Namespaces and Package Loading. Package isolation, symbol registration, and process-global state in native code.

Before this section

Read Orientation, then Interpreter Fundamentals and Memory in either order.

Where this leads

ABI, FFI, and Compatibility explains why rewriting R is hard: native packages depend on R’s C API, and on the behavior behind it, staying the same. Multi-Interpreter Architecture shows how my work isolates each interpreter’s state. Rust for Runtime Engineering teaches language rules that prevent many of these hazards from the start.

Parsing and expressions covers how source text becomes the symbols, calls and pairlists that the evaluator walks. How R Calls C covers PROTECT and UNPROTECT. Serialization internals and the C API and thread safety round out the implementation details.

S3 and S4 dispatch are outside this section’s scope. Argument matching gets a short treatment on How R Executes Code, and execution contexts appear on the conditions page.

Back to top