library(R8)
options(r8.namespace = "demo")1 Introduction
The examples of this guide come from machine learning with rtemis. Most build on one case: a resampler splits the cases of a dataset into training and test sets, by k-fold cross-validation, stratified subsampling, or the bootstrap, each with its own settings. A few types and one function run through the chapters:
| Type or function | Kind | Holds or does | Introduced in |
|---|---|---|---|
GBM |
struct | Settings of a gradient boosting machine | Structs |
Resampler |
enum | One variant per resampling method, each with its own fields | Enums |
Interval |
struct | Two ends, with a rule between them | Rules |
Resamples |
struct | The training cases that a resampler drew, with a rule | Rules |
Window |
struct | An Interval with a label |
Extending a struct |
resample() |
generic, or trait method | Draws the resamples of a Resampler |
Generics and methods, Traits and impls |
1.1 An R8 struct
A struct is declared in R, and its declaration, its checks, and its dispatch are in Rust (Figure 1.1). A value is an R list to R code, whose fields $ reads and whose writes give new values. Its type and the hash of its declaration are in the object where R code does not reach.
1.2 How R8 works
R8 is an R package whose work runs in Rust (Figure 1.2). Its R functions declare types, traits, and impls, construct values, and read, write, and print them; each passes to the binding crate r8-r in one .Call. The binding keeps the registry, which holds the intermediate representation (IR) of every declared type, trait, and impl, and reads a value’s R objects in place through RView. The IR is R8’s versioned description of a declaration, the same whether the type is declared in R or in Rust: a struct’s fields with their types and constraints, an enum’s variants, the rules, a trait’s methods, and the type’s version. R8 checks declarations, validates values, and writes JSON documents and schemas from it; r8declaration(T) returns the IR of T as a JSON document. The core crate rtemis-r8 checks declarations, validates values against their types and rules, and writes canonical forms, hashes, JSON documents, and text. A package with compiled code adds its types and methods through an exports table, which R8 registers when the package loads.
Types between R and Rust follows one declaration and one construction through these parts.
1.3 Two styles
R8 declares behavior in two styles, which lower to the same kind of declaration and can be mixed in one package. Each style has its own part of this guide, and both parts give Resampler the same resample():
| Reader | Part | Declares |
|---|---|---|
| Writes R | Methods, R style | A generic with generic(), and its method for a type with implement(generic, Type) <- function(...) |
| Writes Rust, or groups several methods under one name | Traits, Rust style | A trait with new_trait(), and its methods for a type with impl() |
The parts Types and Packages apply to both styles.
1.4 Values in R or in Rust
A type keeps its values in R or in Rust. The choice is separate from the style of its methods: a type whose methods are declared in the Rust style can keep its values in R, and the types of this guide do.
An R-stored type is declared in R, with new_struct() or new_enum(). Each value is an R list of its fields, whose attributes are names and class; its type and its declaration are in the object itself. x$name returns the field’s R object. x$name <- v checks the new field and the rules that read it, and returns a new list that shares the unchanged fields with the old value. The type’s methods are R functions.
A Rust-stored type is declared in Rust, with #[derive(R8)] and #[r8(storage = "rust")], in a package that compiles Rust; R8 writes the type’s R declaration from the Rust one. Each value is a Rust struct, which R holds through a handle. The first x$name asks the struct for the field and keeps the R object it returns: a vector field is the R vector the value was built from, or a view of a vector in Rust’s memory. x$name <- v checks the new field and the rules that read it, as for an R-stored value, and builds a new struct. Methods written in Rust receive the struct itself as &self; methods written in R work as for an R-stored type. R8 counts the memory the structs hold and runs R’s garbage collector as it grows.
name and values, as an R-stored and as a Rust-stored value.
| R-stored | Rust-stored | |
|---|---|---|
| Declared | in R: new_struct(), new_enum() |
in Rust: #[derive(R8)] with #[r8(storage = "rust")] |
| A value is | an R list of the fields | a handle to a Rust struct |
| Field types | every R8 field type | doubles, integers, logicals, strings, options of them, and vectors of doubles, integers, logicals, or raw bytes |
x$name |
the field’s R object | read from the struct once, then kept |
| Methods | in R | in R or in Rust |
| Package | any R package | a package that compiles Rust |
saveRDS() writes |
the list | the struct’s bytes; a value read back needs the type’s package |
Validation, conversion, equality, hashing, printing, JSON documents, and mirai and future workers behave the same for both.
R8’s own operations run in compiled Rust for both kinds of value. Construction, validation, conversion, equality, hashing, and JSON are Rust code, which reads an R-stored value’s fields in place, without copying them. Keeping the values in Rust therefore does not shorten these operations, except a JSON read. It adds steps where values pass between R and Rust, each a fixed cost that does not grow with the size of the fields (Figure 1.5):
- A new value, from construction or a field write, is a new Rust struct and an R object that holds it: R8 allocates the struct, the holding object, and a list for the fields R reads, and registers a finalizer that frees the struct when R collects the value. A new R-stored value is one R list.
- A JSON read checks and normalizes the document, as for an R-stored value. The type’s Rust code then reads the parsed document into the struct through R8’s reader, and no R list is built, so a JSON read of a Rust-stored value takes less time than of an R-stored one.
In the benchmarks, a field write takes 2.2 µs on an R-stored value and 2.7 µs on a Rust-stored one, with fields of 10 elements and of 10^6 elements alike; construction takes 4.1 µs and 5.0 µs, and a JSON read 11 µs and 9.0 µs, since a Rust-stored type’s codec reads the parsed document in place and builds no R list. A Rust-stored type gains in the work of the type’s own methods: a method written in Rust runs as compiled code over the struct’s fields, and pays the fixed costs once per call.
An R-stored type fits a type whose work happens in R, and is the default:
- The package has no compiled code.
- A field holds a date, a date-time, a factor, a matrix, an array, a sparse matrix, a table, a map, a JSON value, an R object, or a value of another named type.
- Values are written field by field in large numbers, each write paying the fixed cost above, about 0.5 µs in the benchmarks.
- A large field arrives as a compact sequence, such as
1:n: an R-stored value keeps the sequence and reads it region by region, and a Rust-stored value copies it once into Rust’s memory.
A Rust-stored type fits a type whose work happens in Rust:
- The type’s methods are written in Rust. A Rust method reads the struct’s fields in place and converts nothing from R on each call; a method written in Rust requires a Rust-stored type.
- Rust computes the fields. A vector that Rust allocates stays in Rust’s memory, and R reads it as a view, so a large result reaches R without a copy.
- The type is defined in a Rust crate. The Rust struct is the one definition, and R8 writes the R declaration from it.
- A program holds many small values. In the benchmarks, a value of 12 fields takes 440 B in R and 146 B in Rust as a Rust-stored value, and 680 B as an R-stored one.
- Values are read from JSON documents in large numbers. The codec reads the parsed document into the Rust struct, which takes 9.0 µs for the benchmarks’ value of 12 fields, against 11 µs for an R-stored value.
Impls and types in Rust builds a package with a Rust-stored type.
1.5 Namespaces
Every R8 type belongs to a namespace, written before its name: demo::Resampler. A package’s types belong to the namespace in its DESCRIPTION field Config/R8/namespace, which defaults to the package name. Code outside a package declares into the namespace of the option r8.namespace; each chapter starts with:
A namespace has one owner, the first package to declare into it; a declaration by another package into the same namespace is an error. A package’s namespace is its name by default, so two packages that declare a type of the same name do not collide.