Rev 74413 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/compiler/man/compile.Rd% Part of the R package, https://www.R-project.org% Copyright 2011-2017 R Core Team% Distributed under GPL 2 or later\name{compile}\alias{compile}\alias{cmpfun}\alias{cmpfile}\alias{loadcmp}\alias{disassemble}\alias{enableJIT}\alias{compilePKGS}\alias{getCompilerOption}\alias{setCompilerOptions}\title{Byte Code Compiler}\usage{cmpfun(f, options = NULL)compile(e, env = .GlobalEnv, options = NULL, srcref = NULL)cmpfile(infile, outfile, ascii = FALSE, env = .GlobalEnv,verbose = FALSE, options = NULL)loadcmp(file, envir = .GlobalEnv, chdir = FALSE)disassemble(code)enableJIT(level)compilePKGS(enable)getCompilerOption(name, options)setCompilerOptions(...)}\arguments{\item{f}{a closure.}\item{options}{list of named compiler options: see \sQuote{Details}.}\item{env}{the top level environment for the compiling.}\item{srcref}{initial source reference for the expression.}\item{file,infile,outfile}{pathnames; outfile defaults to infile witha \file{.Rc} extension in place of any existing extension.}\item{ascii}{logical; should the compiled file be saved in ascii format?}\item{verbose}{logical; should the compiler show what is being compiled?}\item{envir}{environment to evaluate loaded expressions in.}\item{chdir}{logical; change directory before evaluation?}\item{code}{byte code expression or compiled closure}\item{e}{expression to compile.}\item{level}{integer; the JIT level to use (\code{0} to \code{3}).}\item{enable}{logical; enable compiling packages if \code{TRUE}.}\item{name}{character string; name of option to return.}\item{...}{named compiler options to set.}}\description{These functions provide an interface to a byte code compiler for \R.}\details{The function \code{cmpfun} compiles the body of a closure andreturns a new closure with the same formals and the body replaced bythe compiled body expression.\code{compile} compiles an expression into a byte code object; theobject can then be evaluated with \code{eval}.\code{cmpfile} parses the expressions in \code{infile}, compilesthem, and writes the compiled expressions to \code{outfile}. If\code{outfile} is not provided, it is formed from \code{infile} byreplacing or appending a \code{.Rc} suffix.\code{loadcmp} is used to load compiled files. It is similar to\code{sys.source}, except that its default loading environment is theglobal environment rather than the base environment.\code{disassemble} produces a printed representation of the codethat may be useful to give a hint of what is going on.\code{enableJIT} enables or disables just-in-time (JIT)compilation. JIT is disabled if the argument is 0. If \code{level} is1 then larger closures are compiled before their first use. If\code{level} is 2, then some small closures are also compiled beforetheir second use. If \code{level} is 3 then in additionall top level loops are compiled before they are executed. JIT level3 requires the compiler option \code{optimize} to be 2 or 3. The JITlevel can also be selected by starting \R with the environmentvariable \code{R_ENABLE_JIT} set to one of these values. Calling\code{enableJIT} with a negative argument returns the current JITlevel. The default JIT level is \code{3}.\code{compilePKGS} enables or disables compiling packages when theyare installed. This requires that the package uses lazy loading ascompilation occurs as functions are written to the lazy loading database. This can also be enabled by starting \R with the environmentvariable \code{_R_COMPILE_PKGS_} set to a positive integer value.This should not be enabled outside package installation, because itcauses any serialized function to be compiled, which comes withtime and space overhead. \code{R_COMPILE_PKGS} can be used, instead,to instruct \code{INSTALL} to enable/disable compilation of packagesduring installation.Currently the compiler warns about a variety of things. It doesthis by using \code{cat} to print messages. Eventually this shoulduse the condition handling mechanism.The \code{options} argument can be used to control compiler operation.There are currently four options: \code{optimize}, \code{suppressAll},\code{suppressUndefined}, and \code{suppressNoSuperAssignVar}.\code{optimize} specifies the optimization level, an integer from \code{0}to \code{3} (the current out-of-the-box default is \code{2}).\code{suppressAll} should be a scalar logical; if \code{TRUE} no messageswill be shown (this is the default). \code{suppressUndefined} can be\code{TRUE} to suppress all messages about undefined variables, or it canbe a character vector of the names of variables for which messages shouldnot be shown. \code{suppressNoSuperAssignVar} can be \code{TRUE} tosuppress messages about super assignments to a variable for which nobinding is visible at compile time. During compilation of packages,\code{suppressAll} is currently \code{FALSE}, \code{suppressUndefined} is\code{TRUE} and \code{suppressNoSuperAssignVar} is \code{TRUE}.\code{getCompilerOption} returns the value of the specified option.The default value is returned unless a value is supplied in the\code{options} argument; the \code{options} argument is primarily forinternal use. \code{setCompilerOption} sets the default optionvalues. Options to set are identified by argument names, e.g.\code{setCompilerOptions(suppressAll = TRUE, optimize = 3)}.It returns a named list of the previous values.Calling the compiler a byte code compiler is actually a bit of amisnomer: the external representation of code objects currently uses\code{int} operands, and when compiled with \code{gcc} the internalrepresentation is actually threaded code rather than byte code.}\author{Luke Tierney}\examples{oldJIT <- enableJIT(0)# a simple examplef <- function(x) x+1fc <- cmpfun(f)fc(2)disassemble(fc)# old R version of lapplyla1 <- function(X, FUN, ...) {FUN <- match.fun(FUN)if (!is.list(X))X <- as.list(X)rval <- vector("list", length(X))for(i in seq(along = X))rval[i] <- list(FUN(X[[i]], ...))names(rval) <- names(X) # keep `names' !return(rval)}# a small variationla2 <- function(X, FUN, ...) {FUN <- match.fun(FUN)if (!is.list(X))X <- as.list(X)rval <- vector("list", length(X))for(i in seq(along = X)) {v <- FUN(X[[i]], ...)if (is.null(v)) rval[i] <- list(v)else rval[[i]] <- v}names(rval) <- names(X) # keep `names' !return(rval)}# Compiled versionsla1c <- cmpfun(la1)la2c <- cmpfun(la2)# some timingsx <- 1:10y <- 1:100\donttest{system.time(for (i in 1:10000) lapply(x, is.null))system.time(for (i in 1:10000) la1(x, is.null))system.time(for (i in 1:10000) la1c(x, is.null))system.time(for (i in 1:10000) la2(x, is.null))system.time(for (i in 1:10000) la2c(x, is.null))system.time(for (i in 1:1000) lapply(y, is.null))system.time(for (i in 1:1000) la1(y, is.null))system.time(for (i in 1:1000) la1c(y, is.null))system.time(for (i in 1:1000) la2(y, is.null))system.time(for (i in 1:1000) la2c(y, is.null))}\testonly{for (i in 1:10000) lapply(x, is.null)for (i in 1:10000) la1(x, is.null)for (i in 1:10000) la1c(x, is.null)for (i in 1:10000) la2(x, is.null)for (i in 1:10000) la2c(x, is.null)for (i in 1:1000) lapply(y, is.null)for (i in 1:1000) la1(y, is.null)for (i in 1:1000) la1c(y, is.null)for (i in 1:1000) la2(y, is.null)for (i in 1:1000) la2c(y, is.null)}enableJIT(oldJIT)}\keyword{programming}