A custom roxygen2 roclet that scans for @jit tags on function definitions
and emits a registry of functions to wrap in jit() at package build
time.
Mark a function with #' @jit (optionally #' @jit static = c(...)) to
request that the function be replaced in the package namespace by
jit(f, backend = "auto", static = c(...)).
Setting up the roclet in a package
Add
anvlto your package'sImports.Activate the roclet in the
Roxygenfield ofDESCRIPTION:Add
R/jit-registry.Rto theCollatefield, beforeR/zzz.R.devtools::document()will (re)generate this file on every run.In
R/zzz.R, apply the registry at the top level (right next to.onLoad), so the wrapped functions are byte-compiled during install:.onLoad <- function(libname, pkgname) { ... } anvl::apply_jit_registry(.jit_registry)Tag the functions you want jitted:
#' @export #' @jit static = c("flag") my_fun <- function(x, flag) if (flag) x + 1 else x * 2Run
devtools::document(). The first run requires that the previous install of your package already exports the roclet's pieces; on a fresh setup, omit the custom roclet fromRoxygen, rundocument()once, install, and then re-enable it. Subsequent runs work as normal.
Tag syntax
#' @jit— no static arguments.#' @jit static = c("a", "b")— names of formals to treat as static.#' @jit static = c(1L, 2L)— positions of formals to treat as static.#' @jit static = 2:5— the value is evaluated as R, so ranges and any other expression yielding a character/integer vector are allowed.#' @jit static 2:5— terse form; the=may be omitted.
Anything other than a static = ... argument is rejected.