[R] Execution of R code

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 25 14:12:36 CEST 2003


Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:

> I am not sure I fully understand the Qs.
> 
> There are two phases.
> 
> 1) The source code is parsed.
> 2) The parsed code is evaluated.
> 
> If you run code from source() or a file or the command line, it is
> parsed and evaluated.  However, evaluating a function assignment makes an 
> function object containing the parsed code for the body of a function.
> 
> Running code a second time is often faster because of caching of memory
> (in the chip's caches and in RAM ratehr than VM). In S-PLUS there are more
> layers of caching going on: objects are retrieved from disc and (usually)
> cached in memory, and memory allocated for objects can be re-used rather
> than re-allocated.
> 
> There is no form of pre-compiling to intermediate code on first use (as 
> some Java implementations use), although things like that are in Luke
> Tierney's long-term plans.
> 
> I hope that actually answers your questions.

One might add that although we don't byte-compile like in Java and
emacs-lisp, the parse tree storage that we use is somewhat more
pre-cooked than the tokenized storage of the ROM BASIC found on early
PCs and their precursors. 

One often considers the parsing stage as two processes: Lexical
analysis (the tokenizer) which recognises elementary items such as
keywords, operators, variable names, and constants; and the actual
code tree generation which knows about syntactical structures like for
loops, functions, and compound expressions. 

A code tree for a simple expression like

while ( i < 10 ) i <- i + 1

could be represented as

      while 
     /     \
    <       <-
   / \     /  \
  i   10  i    +
              / \
             i   1

(apologies to those with proportional screen fonts...) In this
representation, everything is basically functions and arguments: "while"
has two arguments: the loop condition and the body, and those are
calls to a comparison and an assignment function respectively, and so
forth. 

In compiled languages, parsing is followed by a step that converts the
code tree to machine instructions, but in languages like R it is
easier to interpret the tree directly. One particular aspect of R-like
languages is that you can replace or modify functions programmatically
in between running them, which means that you won't get the gain of an
up-front optimization effort unless you impose special restrictions.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list