Skip to main content

33  Benchmarks

This chapter compares R8 with the class systems of R, S3, S4, Reference Classes (RC), R6, and S7, on types that perform the same validation. The measurements come from one run of the benchmark in the r8 repository, bench/, at commit cc22440, on 2026-11-21: Apple M5, 32 GB, macOS 27.2; R 4.6.2, rustc 1.99.1, R8 0.4.0, S7 1.0.0, R6 2.6.1, bench 1.1.4. The numbers describe that machine; the scripts reproduce them on another with just bench.

33.1 Summary

Table 33.1 sets R8, with its values in R, against S7 and against the system with the lowest figure among S3, S4, RC, R6, and S7. Every row measures the same type in every system: GBM, the settings of a gradient boosting machine, with twelve checked fields and two rules across fields (The types defines it, and The code each row times shows it in each system).

Table 33.1: R8 against S7 and against the system with the lowest figure among S3, S4, RC, R6, and S7, for a GBM value and a package of 200 GBM types. Lower is less time or less memory.
Row R8 S7 Lowest other
Construction 4.06 µs 120 µs 24.3 µs (S3)
Field write, the changed field and the rules 2.21 µs 21.5 µs 3.16 µs (R6)
Whole-value validation 2.42 µs 81 µs 22.1 µs (S3)
Dispatch on a value 0.246 µs 1.6 µs 0.246 µs (R6)
Construction, fields of 10^6 elements 0.918 ms 2.33 ms 2.16 ms (S3)
Memory per value 680 B 1,180 B 744 B (S3)
Memory per declared type 6.44 KB 25.8 KB 4.04 KB (S3)
200 types usable after load 9 ms 265 ms 10 ms (S3)
Peak memory of a package of 200 types 6.09 MB 162 MB 0.828 MB (S3)
Full gc() with 200 types loaded 0 ms 40 ms -1 ms (S3)
saveRDS() size of a value 332 B 6,380 B 295 B (S3)

R8 has the lowest figure of the six systems in 7 of these 11 rows; another system has a lower one in: memory per declared type; peak memory of a package of 200 types; full gc() with 200 types loaded; saveRDS() size of a value. The sections below show every system on every row, and Which results apply sets out which rows matter for which programs.

33.2 Which results apply

Each chart measures one kind of work. A program pays for the kinds of work it does, as many times as it does them: a time per operation adds up with the number of operations, so 1 µs per value is 1 ms for 10^3 values and 1 s for 10^6. A difference of a few microseconds per value matters to a program that handles millions of values and not to one that builds a few.

A program that Sections that apply
declares a few types and builds values occasionally, such as a model’s settings or a run’s configuration 200 types for the time a package takes to load, and the first construction after load; the per-value times add up to little
builds, changes, or checks many values in a loop, such as one value per resampling fold, per record, or per simulation step GBM operations and Memory and saved size
holds large vectors, matrices, or tables in its values’ fields Values of 10^6 elements
sends values to future or mirai workers, or saves them with saveRDS() the saveRDS() size, and the first construction after load, which each new worker pays
reads or writes JSON, for files, web services, or programs in other languages the JSON read
ships a package that declares many types 200 types
holds many small records of one shape Many small values against one table

33.3 Method

33.3.1 The systems

  • S3: R’s informal class system. A value is a list with a class attribute; the benchmark’s S3 type checks its fields in a constructor function and in a $<- method, both written by hand.
  • S4: R’s formal class system, in the package methods. A class has typed slots and a validity method, which methods::new() and methods::validObject() run.
  • RC: Reference Classes (methods::setRefClass()). A value is a mutable object with fields and methods, changed in place.
  • R6: the R6 package. A value is a mutable object with public members and active bindings, changed in place; the benchmark’s R6 type checks in initialize() and in its active bindings.
  • S7: the S7 package, from the R Consortium’s working group on object-oriented programming. A class has typed properties and a validator.
  • R8 declares a type once, with its fields, their types and constraints, and its rules, and checks every value in compiled Rust. A value is an R list (“R8”).
  • R8 Rust is R8 with GBM and Wide20 declared in Rust (#[derive(R8)]) and their values held as Rust structs behind an R object (Values in R or in Rust).

33.3.2 The types

The benchmark declares four types, the same in every system. They model the configuration objects of rtemis, a machine-learning package for R, where a type’s checks keep a model from training with invalid settings.

GBM holds the hyperparameters of a gradient boosting machine: a model built from decision trees trained one after another, each fit to the errors of the trees before it, as in XGBoost and LightGBM. A GBM value is the setting of one such model before training. Its fields cover each kind of check a configuration type needs: bounded numbers, a pattern, optional fields, vectors, and rules across fields.

Field Meaning Type and constraint
learning_rate the weight of each new tree a double in (0, 1]
subsample the fraction of training cases each tree is fit on a double in (0, 1]
colsample the fraction of features each tree may use a double in (0, 1]
n_trees the number of trees an integer, at least 1
max_depth the depth of each tree an integer from 1 to 32
objective the kind of outcome "regression", "binary", or "multiclass" (a pattern)
num_class the number of outcome classes optional; an integer, at least 2
early_stopping_rounds the rounds without improvement after which training stops optional; an integer, at least 1
seed the seed of the random number generator optional; an integer
verbose whether training reports its progress a logical
weights a weight per training case a vector of doubles, each at least 0
feature_idx the features the model uses, by column a vector of integers, each at least 1

Two rules relate the fields: when early_stopping_rounds is set, it is below n_trees; and num_class is set exactly when objective is "multiclass". A value is valid when every field satisfies its constraint and both rules hold. In the benchmark, weights and feature_idx hold 10 elements, except in Values of 10^6 elements.

  • Wide20 has twenty doubles in [0, 1], each 0.1 by default: many scalar fields, which measure the cost each field adds and the filling of defaults.
  • Resampler describes how a model is evaluated: the kind of resampling (k-fold, bootstrap, or stratified subsampling), the number of resamples, the fraction of cases used for training, and whether to stratify.
  • GBMRun records one evaluation run: a GBM, a Resampler, the date, the outcome as a factor with the levels "control" and "case", a matrix of feature importances with two columns, and a list of Resamplers, one per fold, with the rule that there is one fold per resample. It measures nested values and R’s composite types.

33.3.3 The code each row times

Each system’s GBM as R holds it after the benchmark declares it, and the helpers that the timed code calls, printed from the benchmark’s scripts at commit cc22440 (bench/code.R). S3, S4, RC, R6, and S7 call the same hand-written checks, shown under “Shared checks”; R8 checks from its declaration. “Inputs” are the values every construction receives.

GBM

function (learning_rate, subsample, colsample, n_trees, max_depth, 
    objective, num_class = NULL, early_stopping_rounds = NULL, 
    seed = NULL, verbose, weights, feature_idx) 
structure(signal(check_gbm(list(learning_rate = learning_rate, 
    subsample = subsample, colsample = colsample, n_trees = n_trees, 
    max_depth = max_depth, objective = objective, num_class = num_class, 
    early_stopping_rounds = early_stopping_rounds, seed = seed, 
    verbose = verbose, weights = weights, feature_idx = feature_idx))), 
    class = "s3_GBM")

$<-.s3_GBM

function (x, name, value) 
{
    f <- unclass(x)
    f[name] <- list(value)
    structure(signal(check_gbm(f)), class = "s3_GBM")
}

set_fr

function (x, name, value) 
{
    structure(signal(check_gbm_field(unclass(x), name, value)), 
        class = "s3_GBM")
}

validate_gbm

function (x) 
invisible(signal(check_gbm(unclass(x))))

complexity

function (x) 
UseMethod("complexity")

complexity.s3_GBM

function (x) 
1L

GBM

function (learning_rate, subsample, colsample, n_trees, max_depth, 
    objective, num_class = NULL, early_stopping_rounds = NULL, 
    seed = NULL, verbose, weights, feature_idx) 
methods::new("s4_GBM", learning_rate = as_dbl(learning_rate), 
    subsample = as_dbl(subsample), colsample = as_dbl(colsample), 
    n_trees = as_int(n_trees), max_depth = as_int(max_depth), 
    objective = objective, num_class = as_int(num_class), early_stopping_rounds = as_int(early_stopping_rounds), 
    seed = as_int(seed), verbose = verbose, weights = as_dbl(weights), 
    feature_idx = as_int(feature_idx))

validity of s4_GBM

function (object) 
{
    r <- check(slots_of(object, names))
    if (inherits(r, "bench_problem")) 
        unclass(r)
    else TRUE
}

set_fr

function (x, name, value) 
{
    v <- signal(GBM_CHECKS[[name]](value))
    methods::slot(x, name) <- v
    signal(rules_gbm(list(early_stopping_rounds = x@early_stopping_rounds, 
        n_trees = x@n_trees, objective = x@objective, num_class = x@num_class)))
    x
}

complexity for s4_GBM

function (x) 
1L

GBM

function (learning_rate, subsample, colsample, n_trees, max_depth, 
    objective, num_class = NULL, early_stopping_rounds = NULL, 
    seed = NULL, verbose, weights, feature_idx) 
rc_GBM$new(learning_rate = learning_rate, subsample = subsample, 
    colsample = colsample, n_trees = n_trees, max_depth = max_depth, 
    objective = objective, num_class = num_class, early_stopping_rounds = early_stopping_rounds, 
    seed = seed, verbose = verbose, weights = weights, feature_idx = feature_idx)

initialize

function (...) 
{
    if (...length() == 0L) {
        return(invisible(.self))
    }
    f <- fields_of(GBM_FIELDS, list(...))
    f <- signal(check_gbm(convert_gbm(f)))
    for (n in names(f)) field(n, f[[n]])
    invisible(.self)
}

validate

function () 
invisible(signal(check_gbm(values())))

set

function (name, value) 
{
    f <- values()
    f[name] <- list(value)
    f <- signal(check_gbm(convert_gbm(f)))
    field(name, f[[name]])
    invisible(.self)
}

set_fr

function (name, value) 
{
    v <- signal(GBM_CHECKS[[name]](value))
    r <- list(early_stopping_rounds = early_stopping_rounds, 
        n_trees = n_trees, objective = objective, num_class = num_class)
    if (name %in% names(r)) 
        r[name] <- list(v)
    signal(rules_gbm(r))
    field(name, v)
    invisible(.self)
}

complexity

function () 
1L

GBM

function (learning_rate, subsample, colsample, n_trees, max_depth, 
    objective, num_class = NULL, early_stopping_rounds = NULL, 
    seed = NULL, verbose, weights, feature_idx) 
r6_GBM$new(learning_rate = learning_rate, subsample = subsample, 
    colsample = colsample, n_trees = n_trees, max_depth = max_depth, 
    objective = objective, num_class = num_class, early_stopping_rounds = early_stopping_rounds, 
    seed = seed, verbose = verbose, weights = weights, feature_idx = feature_idx)

initialize

function (...) 
{
    private$f <- signal(check_gbm(fields_of(GBM_FIELDS, list(...))))
}

validate

function () 
invisible(signal(check_gbm(private$f)))

set_fr

function (name, value) 
{
    private$f <- signal(check_gbm_field(private$f, name, value))
    invisible(self)
}

complexity

function () 
1L

active binding n_trees

function (value) 
{
    if (missing(value)) {
        return(private$f[["n_trees"]])
    }
    f <- private$f
    f["n_trees"] <- list(value)
    private$f <- signal(check_gbm(f))
}

s7_gbm_properties()

function () 
{
    int_or_null <- S7::new_union(NULL, S7::class_integer)
    classes <- list(learning_rate = S7::class_double, subsample = S7::class_double, 
        colsample = S7::class_double, n_trees = S7::class_integer, 
        max_depth = S7::class_integer, objective = S7::class_character, 
        num_class = int_or_null, early_stopping_rounds = int_or_null, 
        seed = int_or_null, verbose = S7::class_logical, weights = S7::class_double, 
        feature_idx = S7::class_integer)
    out <- lapply(GBM_FIELDS, function(n) {
        w <- gbm_wrap(n)
        s7_property(n, classes[[n]], GBM_CHECKS[[n]], switch(w %||% 
            "", as_int = as_int, as_dbl = as_dbl))
    })
    stats::setNames(out, GBM_FIELDS)
}

s7_gbm_validator

function (self) 
{
    r <- rules_gbm(list(early_stopping_rounds = self@early_stopping_rounds, 
        n_trees = self@n_trees, objective = self@objective, num_class = self@num_class))
    if (!is.null(r)) 
        unclass(r)
}

GBM

<s7_GBM> class
@ parent     : <S7_object>
@ constructor: function(learning_rate, subsample, colsample, n_trees, max_depth, objective, num_class, early_stopping_rounds, seed, verbose, weights, feature_idx) {...}
@ validator  : function(self) {...}
@ properties :
 $ learning_rate: <double> = numeric(0)
 $ subsample: <double> = numeric(0)
 $ colsample: <double> = numeric(0)
 $ n_trees: <integer> = integer(0)
 $ max_depth: <integer> = integer(0)
 $ objective: <character> = character(0)
 $ num_class: <NULL> or <integer> = NULL
 $ early_stopping_rounds: <NULL> or <integer> = NULL
 $ seed: <NULL> or <integer> = NULL
 $ verbose: <logical> = logical(0)
 $ weights: <double> = numeric(0)
 $ feature_idx: <integer> = integer(0)

complexity

<S7_generic> complexity(x, ...) with 1 methods:
1: method(complexity, s7_GBM)

GBM

GBM <- R8::new_struct(
  "GBM",
  fields = list(
    learning_rate = R8::type_double(exclusive_min = 0, max = 1),
    subsample = R8::type_double(exclusive_min = 0, max = 1),
    colsample = R8::type_double(exclusive_min = 0, max = 1),
    n_trees = R8::type_integer(min = 1L),
    max_depth = R8::type_integer(min = 1L, max = 32L),
    objective = R8::type_character(pattern = "^(regression|binary|multiclass)$"),
    num_class = R8::option(R8::type_integer(min = 2L)),
    early_stopping_rounds = R8::option(R8::type_integer(min = 1L)),
    seed = R8::option(R8::type_integer()),
    verbose = R8::type_logical(),
    weights = R8::vec(R8::type_double(min = 0)),
    feature_idx = R8::vec(R8::type_integer(min = 1L))
  ),
  rules = list(
    R8::rule(
      "early_stop_below_trees",
      early_stopping_rounds < n_trees,
      "early_stopping_rounds is below n_trees.",
      when = !is.null(early_stopping_rounds)
    ),
    R8::rule(
      "num_class_iff_multiclass",
      (objective == "multiclass") == !is.null(num_class),
      "num_class is present exactly when the objective is multiclass."
    )
  )
)

Complexity

R8::new_trait("Complexity", methods = list(complexity = R8::trait_method(function(x) NULL)))

impl

R8::impl(Complexity, GBM, methods = list(complexity = function(x) 1L))
chk_double <- function(x, name, min = -Inf, max = Inf, min_open = FALSE, max_open = FALSE) {
  if (!is.numeric(x) || is.object(x)) return(problem(name, "is not a number"))
  if (length(x) != 1L) return(problem(name, "is not one value"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (is.na(x)) return(problem(name, "is NA"))
  if (!is.finite(x)) return(problem(name, "is not finite"))
  if (if (min_open) x <= min else x < min) return(problem(name, "is below its minimum"))
  if (if (max_open) x >= max else x > max) return(problem(name, "is above its maximum"))
  as.double(x)
}

chk_int <- function(x, name, min = -.Machine$integer.max, max = .Machine$integer.max) {
  if (!is.numeric(x) || is.object(x)) return(problem(name, "is not a number"))
  if (length(x) != 1L) return(problem(name, "is not one value"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (is.na(x)) return(problem(name, "is NA"))
  if (is.double(x) && (!is.finite(x) || x != trunc(x) || abs(x) > .Machine$integer.max)) {
    return(problem(name, "is not a whole number"))
  }
  if (x < min) return(problem(name, "is below its minimum"))
  if (x > max) return(problem(name, "is above its maximum"))
  as.integer(x)
}

chk_opt_int <- function(x, name, min = -.Machine$integer.max) {
  if (is.null(x)) NULL else chk_int(x, name, min = min)
}

chk_string <- function(x, name, pattern = NULL) {
  if (!is.character(x) || is.object(x)) return(problem(name, "is not a string"))
  if (length(x) != 1L) return(problem(name, "is not one value"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (is.na(x)) return(problem(name, "is NA"))
  if (!is.null(pattern) && !grepl(pattern, x, perl = TRUE)) return(problem(name, "does not match its pattern"))
  x
}

chk_flag <- function(x, name) {
  if (!is.logical(x) || is.object(x)) return(problem(name, "is not a logical"))
  if (length(x) != 1L) return(problem(name, "is not one value"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (is.na(x)) return(problem(name, "is NA"))
  x
}

chk_dvec <- function(x, name, min = -Inf) {
  if (!is.numeric(x) || is.object(x)) return(problem(name, "is not numeric"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (anyNA(x)) return(problem(name, "holds NA"))
  if (!all(is.finite(x))) return(problem(name, "holds a value that is not finite"))
  if (length(x) > 0L && min(x) < min) return(problem(name, "holds a value below its minimum"))
  as.double(x)
}

chk_ivec <- function(x, name, min = -.Machine$integer.max) {
  if (!is.numeric(x) || is.object(x)) return(problem(name, "is not numeric"))
  if (!plain(x)) return(problem(name, "carries attributes"))
  if (anyNA(x)) return(problem(name, "holds NA"))
  if (is.double(x) && any(x != trunc(x) | abs(x) > .Machine$integer.max)) {
    return(problem(name, "holds a value that is not a whole number"))
  }
  if (length(x) > 0L && min(x) < min) return(problem(name, "holds a value below its minimum"))
  as.integer(x)
}

GBM_CHECKS <- list(
  learning_rate = function(v) chk_double(v, "learning_rate", 0, 1, min_open = TRUE),
  subsample = function(v) chk_double(v, "subsample", 0, 1, min_open = TRUE),
  colsample = function(v) chk_double(v, "colsample", 0, 1, min_open = TRUE),
  n_trees = function(v) chk_int(v, "n_trees", min = 1L),
  max_depth = function(v) chk_int(v, "max_depth", min = 1L, max = 32L),
  objective = function(v) chk_string(v, "objective", "^(regression|binary|multiclass)$"),
  num_class = function(v) chk_opt_int(v, "num_class", min = 2L),
  early_stopping_rounds = function(v) chk_opt_int(v, "early_stopping_rounds", min = 1L),
  seed = function(v) chk_opt_int(v, "seed"),
  verbose = function(v) chk_flag(v, "verbose"),
  weights = function(v) chk_dvec(v, "weights", min = 0),
  feature_idx = function(v) chk_ivec(v, "feature_idx", min = 1L)
)

check_fields <- function(f, checks) {
  for (n in names(checks)) {
    v <- checks[[n]](f[[n]])
    if (inherits(v, "bench_problem")) return(v)
    f[n] <- list(v)
  }
  f
}

rules_gbm <- function(f) {
  if (!is.null(f$early_stopping_rounds) && !(f$early_stopping_rounds < f$n_trees)) {
    return(problem("early_stopping_rounds", "is not below n_trees"))
  }
  if ((f$objective == "multiclass") != !is.null(f$num_class)) {
    return(problem("num_class", "is present exactly when the objective is multiclass"))
  }
  NULL
}

check_gbm <- function(f) {
  f <- check_fields(f, GBM_CHECKS)
  if (inherits(f, "bench_problem")) return(f)
  r <- rules_gbm(f)
  if (is.null(r)) f else r
}

check_gbm_field <- function(f, name, value) {
  v <- GBM_CHECKS[[name]](value)
  if (inherits(v, "bench_problem")) return(v)
  f[name] <- list(v)
  r <- rules_gbm(f)
  if (is.null(r)) f else r
}

The arguments of a valid GBM, gbm_args(n = 10L); the rows of 10^6 elements call it with n = 1e6, and the compact rows keep feature_idx as seq_len(n):

{
    idx <- seq_len(n)
    if (!compact) 
        idx <- idx + 0L
    list(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
        max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
        seed = 2026L, verbose = FALSE, weights = rep(1, n), feature_idx = idx)
}

R8 Rust times the same expressions as R8, on types declared in Rust.

33.3.4 Equivalent work

Every system checks the same fields with the same checks in the same order: S3, RC, and R6 by hand in their constructors and setters, S4 through a validity method, S7 through property and class validators, and R8 by declaration. Every system’s types are declared in an installed package. Before any timing, every system passes an acceptance corpus of 40 cases, 33 for R8 Rust: each accepts the valid values, converts a whole double to an integer, rejects one invalid value per constraint and per rule, keeps a value unchanged after a failed write, and stores the fields of the valid GBM identically.

A field write runs in three variants. The system’s idiom checks every field again in S3, S4, RC, and R6, and the changed field and the rules in S7 and R8. The other two variants are equal in every system: the changed field and the rules, and every field again.

The system’s idiom The changed field and the rules
S3 y <- x; y$n_trees <- 200L set_fr(x, "n_trees", 200L)
S4 y <- x; y@n_trees <- 200L; validObject(y) set_fr(x, "n_trees", 200L)
RC x$set("n_trees", 200L) x$set_fr("n_trees", 200L)
R6 x$n_trees <- 200L x$set_fr("n_trees", 200L)
S7 y <- x; y@n_trees <- 200L the idiom
R8 y <- x; y$n_trees <- 200L the idiom

RC and R6 change a value in place; the other systems return a new value and keep the original. A further RC and R6 variant copies first ($copy(), $clone()), which keeps the original. R8 checks each declaration as it is made: field types, defaults against their types, rules, and references; the other systems check none.

33.3.5 Measurement

Times are medians from bench::mark(), after two evaluations of each expression, so that byte-compilation of a function’s first call falls outside them; a separate row times the first construction after the system’s package loads. Memory per value is the size of 101 values less the size of one, over 100, each value from distinct inputs. Memory per type is the R memory 100 declarations add (gc() cell counts), with, for R8, the bytes its registry allocates in Rust, counted by an allocator in a build of R8 for the purpose. Peak resident memory is a process’s maximum resident set size less that of a control process that loads the same packages and input and makes the same calls to gc(). Each row runs in a fresh R process.

33.4 GBM operations

The vectors of the GBM hold 10 elements, so these times measure each system’s own work on a value, not the size of its data (Figure 33.1).

Figure 33.1: Operations on one GBM value whose vectors hold 10 elements, by system: construction, a field read, whole-value validation, dispatch on the value, the first construction after the package loads, and a JSON read, which R8 alone performs. Median times; lower is less time.
  • Construction is building a value from its fields, with every check its type declares. Every program constructs values; the time adds up in a program that builds many, such as one value per record or per fold.
  • Field read is x$n_trees, reading one field. The systems differ by fractions of a microsecond, which add up only in loops that read fields millions of times.
  • Whole-value validation checks a value again in full (r8validate(), validObject()). It applies to code that checks values again before saving them, after receiving them, or after changing them in place.
  • Dispatch calls a method on a value, such as summary(x) or a trait’s method, as code organized around generics does. It adds up in loops that call a method per value.
  • First construction after load is the first value built after the type’s package loads, which includes compiling the type’s checks. A script that runs once pays it once, and so does each new future or mirai worker.
  • JSON read turns a JSON document into a value, for files, web services, and programs in other languages. Only R8 reads JSON among these systems.

The code each operation times, by system. x is a valid GBM; the first construction after load runs the construction once, in a fresh process, right after the system’s package loads.

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x$n_trees

Whole-value validation

validate_gbm(x)

Dispatch

complexity(x)

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x@n_trees

Whole-value validation

methods::validObject(x)

Dispatch

complexity(x)

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x$n_trees

Whole-value validation

x$validate()

Dispatch

x$complexity()

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x$n_trees

Whole-value validation

x$validate()

Dispatch

x$complexity()

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x@n_trees

Whole-value validation

S7::validate(x)

Dispatch

complexity(x)

Construction

GBM(learning_rate = 0.1, subsample = 0.8, colsample = 0.9, n_trees = 500L, 
    max_depth = 6L, objective = "multiclass", num_class = 3L, early_stopping_rounds = 20L, 
    seed = 2026L, verbose = FALSE, weights = args$weights, feature_idx = args$feature_idx)

Field read

x$n_trees

Whole-value validation

R8::r8validate(x)

Dispatch

complexity(x)

JSON read, where json is r8to_json(x)

R8::r8from_json(GBM, json)

A field write changes one field of a value, as a tuning loop does when it changes the number of trees between runs. A correct write checks the new field’s value and every rule across fields that reads it; checking more repeats work, and checking less admits invalid values. Figure 33.2 times three variants of n_trees <- 200L in every system, each with the same outcome: a valid new value, or an error that leaves the value unchanged.

  • The system’s idiom is the code the system’s documentation shows for changing a field, with the checks that code runs (Equivalent work lists it per system). In S3, S4, RC, and R6 it checks every field again, since their checks run on the whole value; in S7 and R8 it checks the changed field and the rules that read it.
  • The changed field and the rules checks the new field’s value and the rules that read the field, in every system: the checks a correct write needs and no more. S3, S4, RC, and R6 run it through a setter written for the benchmark (set_fr()); in S7 and R8 it is the idiom.
  • Every field again checks every field and every rule, in every system, as code does when it cannot tell which checks a change affects.

RC and R6 change the value in place; the other systems return a new value and keep the original. The code of each variant, by system, where xw is a valid GBM:

The system’s idiom

{
    y <- xw
    y$n_trees <- 200L
    y
}

The changed field and the rules

set_fr(xw, "n_trees", 200L)

Every field again

{
    y <- xw
    y$n_trees <- 200L
    y
}

The system’s idiom

{
    y <- xw
    y@n_trees <- 200L
    methods::validObject(y)
    y
}

The changed field and the rules

set_fr(xw, "n_trees", 200L)

Every field again

{
    y <- xw
    y@n_trees <- 200L
    methods::validObject(y)
    y
}

The system’s idiom

xw$set("n_trees", 200L)

The changed field and the rules

xw$set_fr("n_trees", 200L)

Every field again

xw$set("n_trees", 200L)

A write that keeps the original

{
    y <- xw$copy()
    y$set_fr("n_trees", 200L)
    y
}

The system’s idiom

xw$n_trees <- 200L

The changed field and the rules

xw$set_fr("n_trees", 200L)

Every field again

xw$n_trees <- 200L

A write that keeps the original

{
    y <- xw$clone()
    y$set_fr("n_trees", 200L)
    y
}

The system’s idiom

{
    y <- xw
    y@n_trees <- 200L
    y
}

The changed field and the rules

{
    y <- xw
    y@n_trees <- 200L
    y
}

Every field again

{
    y <- xw
    y@n_trees <- 200L
    S7::validate(y)
    y
}

The system’s idiom

{
    y <- xw
    y$n_trees <- 200L
    y
}

The changed field and the rules

{
    y <- xw
    y$n_trees <- 200L
    y
}

Every field again

{
    y <- xw
    y$n_trees <- 200L
    R8::r8validate(y)
    y
}
Figure 33.2: A write of one field of a GBM value whose vectors hold 10 elements, in the three variants defined above, by system. Median times in µs.

The second and third variants give every system the same work, so they compare the systems directly; the idiom shows what each system does when written as its documentation shows. A write that keeps the original takes 369 µs in RC ($copy()) and 54 µs in R6 ($clone()).

33.5 Values of 10^6 elements

The weights and feature indices of the GBM hold 10^6 elements each. In the compact rows, the feature indices are a compact sequence (seq_len(), ALTREP), on which R’s min() and anyNA() take constant time.

Figure 33.3: Operations on a GBM value whose weights and feature indices hold 10^6 elements each, by system: construction and whole-value validation with the indices in memory and as a compact sequence, and the peak memory beyond the input. Median times; lower is less time or less memory.

Figure 33.3 shows the same operations with large fields. The code is that of GBM operations and the field writes, with the inputs of gbm_args(n = 1e6).

  • Construction and whole-value validation with fields of 10^6 elements measure the time each system takes to check large data, such as case weights or indices; the time grows with the data. They apply to types whose fields hold data, not only settings.
  • The compact rows hold the feature indices as 1:n or seq_len(n), which R stores as a compact sequence without the elements. They apply when a field receives a generated sequence of indices.
  • Peak memory beyond the input is the memory a system uses while it constructs and checks the value, beyond the data itself. It applies when the data is large compared with the machine’s memory: a system that copies the data needs that much more.
Figure 33.4: The JSON document of a GBM value whose weights and feature indices hold 10^6 elements each, for R8 and R8 Rust, the systems that read JSON: the write, the read, and the read’s peak memory beyond the document’s text. Median times; lower is less time or less memory.

Figure 33.4 shows the JSON document of the same value: its size, 8.9 MB, is that of the data written as text.

  • JSON write and JSON read of a value with fields of 10^6 elements measure the time to save such a value to a file or a service and to load it back, which grows with the data. The code is R8::r8to_json(x) and R8::r8from_json(GBM, json). They apply to programs that keep data-bearing values as JSON documents, such as a run’s record or a dataset’s description.
  • Peak memory of the read is the memory the read holds beyond the document’s text, measured in a process that reads the text and the value against one that reads the text alone. R8 parses a document into a tree that holds an array of numbers as one run of doubles, 8 B a number. It applies when documents are large compared with the machine’s memory.

Field writes into these values span three orders of magnitude, on a log axis (Figure 33.5). They show whether writing one field costs a fixed time or a time that grows with the data, as it does when a write checks or copies every field. A system’s idiom is one of the other two variants, so its points lie under theirs: every field again in S3, S4, RC, and R6, the changed field and the rules in S7, R8, and R8 Rust.

Figure 33.5: A write of one field of a GBM value whose weights and feature indices hold 10^6 elements each, in the three variants, by system. Median times in ms, on a log axis.

33.6 Memory and saved size

Figure 33.6: The memory one more value takes, for the GBM, Wide20, and GBMRun types, and the size of a GBM value saved with saveRDS(), by system.

An R8 Rust value also holds 146 B (GBM) and 160 B (Wide20) in Rust, beside its memory in R.

Figure 33.6 shows the memory of values and their saved size.

  • Memory per value is the memory one more value takes, its data excluded, since the values share their vectors. It applies to programs that hold many values at once, such as a list of results or a cache: 10^6 GBM values at 680 B each take 680 MB in R8. GBMRun holds nested values, so its figure includes theirs.
  • The saveRDS() size is the size of one value saved to a file. future and mirai serialize values the same way to send them to workers, so it also applies to sending values to workers, once per value sent.

33.7 200 types

A package that declares 200 GBM types, loaded in a fresh process. These rows apply to packages that declare many types, and to everyone who loads such a package: the load time and memory are paid by every R session and every worker that uses it. S7, R6, and RC lazy-load their class objects, which reading the package’s bindings brings into memory; R8 registers its types as its package loads and compiles a type’s rules at its first value. “Usable” is the load, every binding read, and one value of each type constructed.

Each system’s package holds the benchmark’s checks.R and types.R and declares its types at the top level, each with gbm_type(), which declares the system’s GBM (The code each row times) under the name it is given. The R8 package registers its types as it loads:

# R/zz-types.R, here for R8
GBM_1 <- gbm_type("R8", "GBM_1", where = environment())
GBM_2 <- gbm_type("R8", "GBM_2", where = environment())
# ... through GBM_200

# R/zzz.R, R8 only
.onLoad <- function(libname, pkgname) {
  R8::r8register()
}
Figure 33.7: A package that declares 200 GBM types, by system: the time to declare them in a session, the time until each is usable after the package loads, the package’s peak memory, and the time of a full gc(), the last two beyond a control process.

Figure 33.7 shows the package’s declarations, load, memory, and garbage collection.

  • Declaring 200 types in a session is the time the declarations take to run. A package’s declarations run when it is installed, not when it loads; a script that declares its types at the top runs them on every run.
  • The 200 types usable after load is the time from loadNamespace() to one value of each type: what a user of the package waits at the start of a session, and what each new worker pays.
  • Peak memory of the package is the memory its 200 types hold once loaded, which every process that loads the package pays.
  • Full gc() is the time R’s garbage collector spends on the package’s objects in each full collection. It applies to long sessions and memory-heavy work, where R collects often.

Figure 33.8 shows the memory per declared type, in R and in Rust. A package’s types multiply it: 200 R8 types at 6.4 KB each take 1.3 MB.

Figure 33.8: Memory per declared type, in R and in Rust (R8’s registry), by system.

33.8 Many small values against one table

R8 alone: 10^6 values of a struct of two doubles, as a list of R8 values (vec(S)) and as a table (type_table(of = S)). Table 33.2 compares the two. It applies to data shaped as many records of one type, such as one row per observation: a table holds each field as one column, and a list holds a value per record, each with its own fields and attributes.

Table 33.2: 10^6 values of a struct of two doubles in R8, as a list of values (vec(S)) and as a table (type_table(of = S)).
vec(S) type_table(of = S)
Construction, s 4.05 0.001
Size, MB 389 15.3
Peak memory beyond the input, MB 711 0.984

33.9 Reading the results

  • One machine and one run; times on another machine differ, and the order between close systems can change.
  • A field read in S4 (x@n_trees) takes about 16 ns, measured as a loop of 10^7 reads, below the timer’s resolution of about 41 ns; its median and its bar read 0.
  • The first construction after load includes byte-compilation of each system’s functions and, for R8, the compilation of the type’s rules.
  • Control-subtracted figures within a few milliseconds or megabytes of zero are within the variation between processes.
  • RC and R6 values serialize the environments of their class generators, and S7 values carry their class object, which their saveRDS() sizes reflect.
  • R8 checks the bounds of a compact sequence (ALTREP) on its first and last elements, which R records as sorted and without NA, and leaves it compact; R8 Rust copies it once into Rust.

33.10 All measurements

GBM, vectors of 10

S3 S4 RC R6 S7 R8 R8 Rust
Construction, µs 24.3 149 287 50.6 120 4.06 4.96
First construction after load, µs 4175 4909 6203 5588 5109 4255 4244
Field read, µs 0.205 < 0.01 2.46 0.533 0.533 0.492 0.533
Field write, the system’s idiom, µs 24.6 80.3 46.7 23.6 21.6 2.21 2.66
Field write, the changed field and the rules, µs 3.94 4.51 12.1 3.16 21.5 2.21 2.67
Field write, every field again, µs 24.5 80.2 46.6 23.5 103 4.72 6.11
Field write keeping the original, µs 369 54
Whole-value validation, µs 22.1 78.1 35.7 22.5 81 2.42 2.62
Dispatch, µs 0.41 0.41 2.38 0.246 1.6 0.246 0.533
JSON write, µs 4.72 5.04
JSON read, µs 9.59 7.71
Memory per value, B 744 1,064 4,880 4,056 1,176 680 440
Memory per value held in Rust, B 146
saveRDS() size, B 295 317 104,547 3,037 6,375 332 427

Wide20

S3 S4 RC R6 S7 R8 R8 Rust
Construction, f1 supplied, µs 28.3 147 494 62.9 167 3.49 4.06
Construction, every field by default, µs 27.9 146 494 62.7 164 3.28 3.94
Field write, the changed field, µs 3.4 3.81 10.2 2.66 18.1 1.97 2.26
Memory per value, B 1,656 2,360 9,432 5,760 2,472 1,560 472
Memory per value held in Rust, B 160
saveRDS() size, B 171 200 103,512 3,069 3,934 216 318

GBMRun

S3 S4 RC R6 S7 R8
Construction, µs 228 620 1580 361 629 112
Memory per value, B 2,872 4,296 24,968 22,328 5,192 3,592
saveRDS() size, B 522 555 37,111 6,166 10,994 582

GBM with vectors of 10^6

S3 S4 RC R6 S7 R8 R8 Rust
Construction, ms 2.16 2.36 2.46 2.2 2.33 0.918 0.881
Construction, compact feature indices, ms 2 2.2 2.32 2.04 2.06 0.574 0.684
Field write, the system’s idiom, ms 2.17 2.28 2.27 2.17 0.0221 0.00234 0.00266
Field write, the changed field and the rules, ms 0.00406 0.00463 0.0124 0.00328 0.0217 0.00225 0.00262
Field write, every field again, ms 2.17 2.28 2.26 2.16 2.34 0.881 0.889
Field write keeping the original, ms 0.373 0.0542
Whole-value validation, ms 2.16 2.28 2.23 2.15 2.31 0.876 0.9
Whole-value validation, compact feature indices, ms 1.96 2.09 2.13 2.05 2.02 0.568 0.854
Peak memory beyond the input, MB 3.77 4 4.19 3.84 4.2 0.281 -0.578

200 GBM types

S3 S4 RC R6 S7 R8
Declaring 200 types in a session, s 0.023 0.09 0.944 0.117 0.09 0.03
Memory per type in R, B 4,134 5,525 48,428 38,032 26,432 3,630
Memory per type in Rust, B 0 0 0 0 0 2,965
loadNamespace(), s 0.001 0.012 0.059 0.002 0.003 0.004
Load, every binding read, s 0.003 0.013 0.08 0.209 0.231 0.006
Load, every binding read, a value of each type, s 0.01 0.057 0.179 0.222 0.265 0.009
Cons cells after loading 8,193 69,458 523,704 17,506 31,511 8,503
Cons cells, every binding read 20,004 74,869 861,283 1,953,657 1,459,172 27,121
Cons cells, a value of each type 22,482 91,185 894,561 1,964,647 1,470,555 29,806
Full gc() beyond the control, ms -1 2 19 34 40 0
Peak memory beyond the control, MB 0.828 17.9 131 190 162 6.09
© 2026 E.D. Gennatas