| 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 |
Compiles all top-level expressions in an R source file to bytecode and saves the result to an output file.
cmpfile(infile, outfile, ascii = FALSE, env = .GlobalEnv, verbose = FALSE, options = NULL, version = NULL)cmpfile(infile, outfile, ascii = FALSE, env = .GlobalEnv, verbose = FALSE, options = NULL, version = NULL)
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 |
ascii |
Logical; whether to write an ASCII file. |
env |
The top-level environment in which to evaluate expressions. |
verbose |
Logical; if |
options |
List of compilation options. |
version |
Optional file format version. |
Invisible NULL. Output is written to file as a side effect.
tmp_r <- tempfile(fileext = ".R") tmp_rc <- tempfile(fileext = ".Rc") writeLines("x <- 1; x + 1", tmp_r) cmpfile(tmp_r, tmp_rc)tmp_r <- tempfile(fileext = ".R") tmp_rc <- tempfile(fileext = ".Rc") writeLines("x <- 1; x + 1", tmp_r) cmpfile(tmp_r, tmp_rc)
Compiles an R closure to bytecode using the C-based compiler backend.
cmpfun(f, options = NULL)cmpfun(f, options = NULL)
f |
A closure to compile to bytecode. If |
options |
A named list of compilation options. If |
A bytecode-compiled function.
f <- function(x) x + 1 cf <- cmpfun(f) cf(10)f <- function(x) x + 1 cf <- cmpfun(f) cf(10)
Compiles a generic R language object (such as an expression or function) to bytecode using the C-based compiler backend.
compile(e, env = .GlobalEnv, options = NULL, srcref = NULL)compile(e, env = .GlobalEnv, options = NULL, srcref = NULL)
e |
An R language object to compile. |
env |
The environment in which to evaluate the object. |
options |
A list of compilation options, or |
srcref |
Optional source reference information. |
The compiled object, or invisible NULL if compilation is only for side effect.
expr <- quote({ x <- 1; x + 2 }) compile(expr)expr <- quote({ x <- 1; x + 2 }) compile(expr)
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.
r_cmd_install_hook_path() enable_r_cmd_install_hook(enable = TRUE)r_cmd_install_hook_path() enable_r_cmd_install_hook(enable = TRUE)
enable |
|
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.
r_cmd_install_hook_path returns a path.
enable_r_cmd_install_hook returns the previous state, TRUE or
FALSE, invisibly.
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.gzr_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
Loads bytecode compiled by cmpfile into an environment.
loadcmp(file, envir)loadcmp(file, envir)
file |
Path to the compiled |
envir |
The environment to load into, use .GlobalEnv to load the code into current global session |
Invisible NULL.
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)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)