crbcc is an R package that compiles R code to bytecode using a C implementation.
It is intended as a fast, drop-in alternative to the base compiler package, for cases in which
a reduced compilation time could be beneficial, eg. in research.
However using crbcc for minor performance improvements inside R's JIT pipeline is also possible.
This project was developed as part of a bachelor's thesis at FIT CTU.
When possible, crbcc mirrors the architecture and logic used in the original GNU-R compiler package by Luke Tierney. The original compiler Noweb document is available at https://homepage.cs.uiowa.edu/~luke/R/compiler/compiler.pdf, or distributed as an R base package, available under the GPL-2 license. Most documentation provided in the document is relevant to crbcc as well.
- 1:1 compatible output with current GNU-R compiler;
cmpfuntested on a corpus consisting of around 339,000 lines of code - 30x improvement on wall clock time when compiling base and recommended R packages
- Compile closures with
crbcc::cmpfun() - Compile generic language objects with
crbcc::compile() - Compile full source files with
crbcc::cmpfile() - Load compiled files with
crbcc::loadcmp()
- R >= 4.5.0
- Build toolchain for compiling native C code
R CMD INSTALL .or with the provided Make target:
make installlibrary(crbcc)
f <- function(x) {
y <- x + 1
y * 2
}
cf <- cmpfun(f, options = list(optimize = 2))
cf(10)library(crbcc)
cmpfile("script.R", "script.Rc")
loadcmp("script.Rc", envir = .GlobalEnv)The compiler accepts options as a named list:
optimize(integer): optimization level (default2)suppressAll(logical): suppress warnings (defaultTRUE)suppressNoSuperAssignVar(logical): suppress missing super-assignment variable warningssuppressUndefined(logicalorcharacter):TRUE: suppress all undefined variable/function warningsFALSE: suppress nonecharacter(): suppress specific names
Example:
opts <- list(
optimize = 2,
suppressAll = FALSE,
suppressUndefined = c("pi", "letters")
)
cf <- cmpfun(function(x) x + pi, options = opts)Contributions are welcome.
- Open an issue first for bugs, regressions, or feature proposals, especially if the compiler crashed on some expression or returned different results from GNU-R
- Keep pull requests focused and small when possible.
- Do not modify tests. Compatibility with GNU R compiler behavior is expected to remain 1:1.
- Run package checks before submitting (no new warnings or errors should rise):
R CMD check .- Warning output is not guaranteed to be 1:1 with GNU R's
compilerpackage. cmpfile()currently does not support a functionalverbosemode (the argument exists but is not implemented).- Saving and loading functionality is not compatible with the GNU-R compiler, since it uses an .Internal call to manage the files.