Package 'crbcc'

Title: C R Bytecode Compiler
Description: Implements a bytecode compiler for 'R' in 'C', targeting functional parity with the base 'compiler' package, while focusing on better performance. The architecture mostly mirrors the original 'GNU-R' compiler as described in <https://homepage.cs.uiowa.edu/~luke/R/compiler/compiler.pdf>.
Authors: Josef Malý [aut, cre], Filip Křikava [aut, ths], Faculty of Information Technology, CTU in Prague [cph]
Maintainer: Josef Malý <[email protected]>
License: GPL (>= 2)
Version: 0.2.0
Built: 2026-11-21 19:55:47 UTC
Source: https://github.com/PRL-PRG/crbcc

Help Index


Compile an R Source File

Description

Compiles all top-level expressions in an R source file to bytecode and saves the result to an output file.

Usage

cmpfile(infile, outfile, ascii = FALSE, env = .GlobalEnv,
        verbose = FALSE, options = NULL, version = NULL)

Arguments

infile

Path to the input R source file.

outfile

Path to the output file. If missing, defaults to the input file name with extension replaced by .Rc.

ascii

Logical; whether to write an ASCII file.

env

The top-level environment in which to evaluate expressions.

verbose

Logical; if TRUE, print the progress of the compilation.

options

List of compilation options.

version

Optional file format version.

Value

Invisible NULL. Output is written to file as a side effect.

Examples

tmp_r <- tempfile(fileext = ".R")
tmp_rc <- tempfile(fileext = ".Rc")
writeLines("x <- 1; x + 1", tmp_r)
cmpfile(tmp_r, tmp_rc)

Compile an R Function to Bytecode

Description

Compiles an R closure to bytecode using the C-based compiler backend.

Usage

cmpfun(f, options = NULL)

Arguments

f

A closure to compile to bytecode. If f is a builtin or special function, cmpfun returns it without a change.

options

A named list of compilation options. If options is NULL, cmpfun uses the default options. As in compiler::cmpfun, options can also be a different object with names, for example a named vector.

Value

A bytecode-compiled function.

Examples

f <- function(x) x + 1
cf <- cmpfun(f)
cf(10)

Compile a Generic Language Object

Description

Compiles a generic R language object (such as an expression or function) to bytecode using the C-based compiler backend.

Usage

compile(e, env = .GlobalEnv, options = NULL, srcref = NULL)

Arguments

e

An R language object to compile.

env

The environment in which to evaluate the object.

options

A list of compilation options, or NULL.

srcref

Optional source reference information.

Value

The compiled object, or invisible NULL if compilation is only for side effect.

Examples

expr <- quote({ x <- 1; x + 2 })
compile(expr)

Install Packages with crbcc

Description

R CMD INSTALL compiles the closures of a package to byte code. The R CMD INSTALL hook makes it use crbcc for this compilation. The hook is an R_PROFILE_USER file, which R reads at the start of each R process.

r_cmd_install_hook_path returns the path of the hook.

enable_r_cmd_install_hook sets the environment variable R_PROFILE_USER of this R session to the hook. The R processes that this R session starts read this variable. Thus install.packages() compiles the packages with crbcc. enable_r_cmd_install_hook(FALSE) sets R_PROFILE_USER back to its previous value.

Usage

r_cmd_install_hook_path()
enable_r_cmd_install_hook(enable = TRUE)

Arguments

enable

TRUE enables the hook. FALSE disables it.

Details

crbcc compiles only the packages that R installs from source. A binary package is already compiled. On Windows and with the R for macOS from CRAN, install.packages() installs binary packages by default. To install from source, use type = "source".

If crbcc cannot compile a closure, the hook compiles it with the compiler package.

When the hook is enabled, R does not read the usual .Rprofile file in the R processes that this R session starts.

Value

r_cmd_install_hook_path returns a path. enable_r_cmd_install_hook returns the previous state, TRUE or FALSE, invisibly.

Examples

r_cmd_install_hook_path()

## Not run: 
enable_r_cmd_install_hook()
install.packages("MASS", type = "source")
enable_r_cmd_install_hook(FALSE)

## End(Not run)

## From a shell:
## R_PROFILE_USER=$(Rscript -e 'cat(crbcc::r_cmd_install_hook_path())') \
##   R CMD INSTALL foo_1.0.tar.gz

Load a Compiled R File

Description

Loads bytecode compiled by cmpfile into an environment.

Usage

loadcmp(file, envir)

Arguments

file

Path to the compiled .Rc file.

envir

The environment to load into, use .GlobalEnv to load the code into current global session

Value

Invisible NULL.

Examples

tmp_r <- tempfile(fileext = ".R")
tmp_rc <- tempfile(fileext = ".Rc")
writeLines("f <- function(x) x * 2", tmp_r)
cmpfile(tmp_r, tmp_rc)
loadcmp(tmp_rc, .GlobalEnv)
f(3)