R Java Oracle
R Java Oracle
What we do…
assignments, array super-assignments, and attribute extraction and assignment. Expres-
sions also include values, u, and partially reduced function calls, ⌫(a), which are not
used in the surface syntax of the language but are needed during evaluation. The pa-
rameters of a function declaration, denoted by f, can be either variables or variables
with a default value, an expression e. Symmetrical arguments of calls, denoted a, are
expressions which may be named by a symbol. We use the notation a to denote the
H::= ; | H[◆/F ]
stack, S, as shown in Fig. 2. For simplicity, the heap dif-
ferentiates between three kinds of addresses: frames, ◆,
| H[ /e ] | H[ /⌫] promises, , and data objects, ⌫. The notation H[◆/F ]
[N UM ] [S TR ] [F UN ]
⌫ fresh ↵ = ? ? ⌫ fresh ↵ = ? ? ⌫ fresh ↵ = ? ?
[E XP ] [F ORCE P] H 0 = H[⌫/num[n]↵ ] H 0 = H[⌫/str[s]↵ ] H 0 = H[⌫/ f.e, ↵ ]
0 0 0 n ; H ! ⌫; H 0 s ; H ! ⌫; H 0 function(f) e ; H ! ⌫; H 0
e ;H ! e ;H H( ) = e [F IND ] [G ET P]
⇤ S; H = C[e0 ] ⇤ S; H 0 ⇤ S; H = e 0 ⇤ C[ ]
(H, x) = u H( ) = ⌫
C[e] C[ ] ⇤ S; H x ; H ! u; H ; H ! ⌫; H 0
[A SS ]
0 0 0
cpy(H, ⌫) = H , ⌫ =◆⇤ H(◆) = F F = F [x/⌫ 0 ] 0
H 00 = H 0 [◆/F 0 ]
[F ORCE F] [G ET F] x < ⌫ ; H ! ⌫; H 00
getfun(H, , x) = getfun(H, , x) = ⌫ cpy(H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ 0 assign(x, ⌫ 0 , 0
[DA SS ]
, H 0 ) = H 00
x << ⌫ ; H ! ⌫; H 00
C[x(a)] ⇤ S; H = ⇤ C[x(a)] ⇤ S; H C[x(a)] ⇤ S; H = C[⌫(a)] ⇤ S; H [G ET ]
(H, x) = ⌫ 0 readn(⌫, H) = m get(⌫ 0 , m, H) = ⌫ 00 , H 0
[I NV F] x[[⌫]] ; H ! ⌫ 00 ; H 0
C ::= [] | x < C | x[[C]] | x[[e]] < C | x[[C]] < ⌫ | {C; e} | {⌫; C} cpy(H, ⌫ 00 ) = H 0 , ⌫ 000 H 0 (⌫) = ⌫? ⌫? index(⌫ 0 , ⌫?
0
, H 0 ) = ? reads(⌫ 0 , H 0 ) = s
0 0
H 0 (⌫? ) = gen[⌫]↵ H 0 (⌫? 0
) = str[s]↵ H 00 = H 0 [⌫? /gen[⌫⌫ 000 ]↵ ][⌫?
0
/str[ss]↵ ]
| attr(C, e) | attr(⌫, C) | attr(e, e) < C | attr(C, e) < ⌫ | attr(⌫, C) < ⌫ attr(⌫, ⌫ 0 ) < ⌫ 00 ; H ! ⌫ 00 ; H 00
[S ET B]
cpy(H, ⌫ 00 ) = H 0 , ⌫ 3 H 0 (⌫) = ? ? ⌫ 4 , ⌫ 5 fresh reads(⌫ 0 , H 0 ) = s
R ::= [] | {⌫; R} H 00 = H 0 [⌫ 4 /gen[⌫ 3 ]? ? ][⌫ 5 /str[s]? ? ]
attr(⌫, ⌫ 0 ) < ⌫ 00 ; H ! ⌫ 00 ; H 00
S−1
S−2
S−3
S−4
S−5
S−6
S−7
S−9
S−10
S−11
Python
S−12
R
Avg
Python and R, normalized to C
e created by pairlist().
ntioned above, they are C R User data R internal
10000
ee calls pairlist , the The programming language shootout
data
CRAN in code
pamounts
memory R must onlybe
usage heap
eight,
in C allocated
(calls to and garbage
malloc) and data allocated
of memory. Unlike C
sage
R in
oconductor C
allocation (calls
none.
is to
The
splitmalloc)
R
between and data allocated
vectors (which are typically
esstthem
be heap allocated
to represent code and garbage
1000
is mostly
e split between
used by vectors
the (which are
interpreter for,typically
e.g., arguments to
alls
passtoandmalloc)
processand func-data allocated
edhows
ll by that
the R
arguments. interpreter
allocates
It is inter- for, e.g.,
orders of argumentsmore
magnitude to data than
ween vectors (which are typically
allocates
o note that
cases,
terpreterthe orders
the
internal
for,
of
time spent
e.g.,
magnitude
data required
arguments
more
is
to moredata than
than the user data.
100
C/R
cating lists
ernal data isrequired
greater than is more than the user data.
mplemented
ders of by
magnitude a copy-on-write
more data (COW)
than mechanism. Thus,
e spent on vectors. Cons
y a copy-on-write
are
e 56shared
quired byte andand
islong,
more only
than (COW) usermechanism.
duplicated
takethe if there isThus,
data. actually a need
des only duplicated if there is actually a needalgorithm is
10
GB memory
-write on(COW)
average footprint.
in the
mechanism. Even Thus,
though the COW
ootprint.
ut37% Even is
ofif arguments
benchmarks.
icated there though are the
actually aCOW
[Link] algorithm is
other reason
uments are for the large
copied.
t() .
enisthough the COW data algorithm is
1
S−2
S−3
S−4
S−5
S−6
S−7
S−8
S−9
S−10
S−11
S−12
R User data R internal
be
Inof vectors
% C allocated in R User data R internal
Heap Allocated Memory
ly
conductor vignettes
R User data con- Fig.
R internal 8. Heap allocated memory (MB log scale). C vs. R.
y a single numeric value.
0000
he
where the
1.0
urn to more
0.9
grams. Fig. 7
n of execu-
ioconductor 0.8
th ProfileR.
0.7
mm
Builtin mm
mm
mm
mm
mm
alloc.c
a Biocon- [Link]
[Link]
[Link]
[Link]
[Link]
alloc.l
0.6
[Link]
[Link]
[Link]
[Link]
[Link]
key obser- External [Link]
[Link]
[Link]
[Link]
[Link] alloc.v
duplicate
duplicate
duplicate
duplicate
duplicate duplic
ry manage-
0.5
lookup
lookup
lookup
lookup
lookup lookup
n average of match
match
match
match
match match
Lookup external
external
external
external
external extern
0.4
special
special
special
special
special specia
me spent in Allocate vector
18.7%), al-
0.2
Allocate cons
3.6%), vec-
0.1
duplications
Garbage collection
lue seman-
0.0
nt in built-
d where the
1.0
Intel X5460. 3.16GHz, Linux 2.6.34. R 2.12.1, GCC v4.4.5
turn to more
0.9
ograms. Fig. 7
wn of execu-
0.8
Bioconductor
Bioconductor vignettes
with ProfileR.
0.7
mm
[Link]
ts a Biocon- [Link]
0.6
lookup
an average of match
external
0.4
special
time spent in
(18.7%), al-
0.2
(3.6%), vec-
0.1
duplications
value seman-
0.0
c(1,2,3) + x[1:3]
with(fd,carb*den) [Link]
x[[Link](x)] <- 0 <-function(data,exp,...)
eval(substitute(exp),
data,
[Link])))
Functions
q<-function(x=5)x*x*x
q()
q(2)
with(fd,carb*den) [Link]
q(x=4) <-function(data,exp,...)
eval(substitute(exp),
data,
p<-function(x=5,...,y=x+1) [Link])))
d an existing data structure to operate on, thus they are always side effecting.
ey account for 22% of all side effects and 12% of all assignments.
R symbol lookup is 1G
ensitive. This feature, Position
Keyword
either Lisp nor Scheme
s exercised in less than 1M
Variadic
f(1, 2)
unction name lookups.
even though this num-
1K
, the number of sym-
lly checked is 3.6 on
The only symbols for 1
s feature actually mat- 1G
f(x=1,y=2)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20−39
the Bioconductor vi-
c and file, both pop- 1M f(y=1,x=2)
bles names and built-in
1K
f(2,x=1)
rs. The R function
n syntax is expressive
expressivity is widely 1 f(x=1,2)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20−39
99% of the calls, at
1G
255+
nction parameters by
milarly, variadic param-
to be called with large Fig. 12. Histogram of the number of function arguments
in Bioconductor. (Log scale)
Promises
assert<-function(C,P)
if (C) print(P)
10
8
f(x=1,y=2)
f(y=1,x=2)
6
f(2,x=1)
4
f(x=1,2)
2
c(1,2,3,4)
0
80 85 90 95 100
x[12] <- F
F ; e
{e ; F}
Scoping
c <- 42 c <- 42
d <- c
c(1,2,3) d(1,2,3)
less than 0.05% context sensitive
function name lookups
f(y)
with(fd,carb*den) [Link]
assert(y[[1]]==5) <-function(data,exp,...)
eval(substitute(exp),
data,
f<-function(b){b[[1]]<-0} [Link])))
[F IND ] [G ET P]
(H, x) = u H( ) = ⌫
Assignment
x ; H ! u; H ; H ! ⌫; H 0
[A SS ]
= H 0, ⌫0 = ◆ ⇤ 0 H(◆) = F F 0 = F [x/⌫ 0 ] H 00 = H 0 [◆/F 0 ]
x < ⌫ ; H ! ⌫; H 00
[DA SS ]
x [ 42
py(H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ ] assign(x,
<- ⌫ y, 0 0 0
, H 0 ) = H 00
x << ⌫ ; H ! ⌫; H 00
[G ET ]
(H, x) = ⌫ 0 readn(⌫, H) = m get(⌫ 0 , m, H) = ⌫ 00 , H 0
x[[⌫]] ; H ! ⌫ 00 ; H 0
[S ET L]
cpy(H, ⌫ 0 ) = H 0 , ⌫ 00 = ◆ ⇤ 0 ◆(H 0 , x) = ⌫ 000
readn(⌫, H 0 ) = m set(⌫ 000 , m, ⌫ 00 , H 0 ) = H 00
x[[⌫]] < ⌫ 0 ; H ! ⌫ 0 ; H 00
[S ET G]
⌫ 0 ) = H 0 , ⌫ 00 =◆⇤ 0
H 0 (◆) = F x 62 F 0
(H 0 , x) = ⌫ 000
py(H 0 , ⌫ 000 ) = H 00 , ⌫ 0000 F 0 = F [x/⌫ 0000 ] H 000 = H 00 [◆/F 0 ]
readn(⌫, H) = m set(⌫ 0000 , m, ⌫ 00 , H 000 ) = H 0000
[F IND ] [G ET P]
(H, x) = u H( ) = ⌫
Assignment x ; H ! u; H ; H ! ⌫; H 0
[A SS ]
cpy(H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ 0 H(◆) = F F 0 = F [x/⌫ 0 ] H 00 = H 0 [◆/F 0 ]
x < ⌫ ; H ! ⌫; H 00
[DA SS ]
y <- c(…)
cpy(H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ 0 assign(x, ⌫ 0 , 0
, H 0 ) = H 00
x << ⌫ ; H ! ⌫; H 00
f <-
(H, x) = ⌫ 0
function() { 0
readn(⌫, H) = m get(⌫ , m, H) = ⌫ , H 00
[G ET ]
0
x x[[⌫]]
[ 42 ; H ! ⌫]; H <- 00 0
y [S ET L]
cpy(H, ⌫ 0 ) = H 0 , ⌫ 00 = ◆ ⇤ 0 ◆(H 0 , x) = ⌫ 000
readn(⌫, H 0 ) = m set(⌫ 000 , m, ⌫ 00 , H 0 ) = H 00
x[[⌫]] < ⌫ 0 ; H ! ⌫ 0 ; H 00
[S ET G]
0 0 00 0 0 0
cpy(H, ⌫ ) = H , ⌫ =◆⇤ H (◆) = F x 62 F (H , x) = ⌫ 000
0
H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ 0 H(◆) = F F 0 = F [x/⌫ 0 ] H 00 = H 0 [◆
x < ⌫ ; H ! ⌫; H 00
[DA SS ]
cpy(H, ⌫) = H 0 , ⌫ 0 = ◆ ⇤ 0 assign(x, ⌫ 0 , 0
, H 0 ) = H 00
x << ⌫ ; H ! ⌫; H 00
[G ET ]
(H, x) = ⌫ 0 readn(⌫, H) = m get(⌫ 0 , m, H) = ⌫ 00 , H 0
x[[⌫]] ; H ! ⌫ 00 ; H 0
[S ET L]
cpy(H, ⌫ 0 ) = H 0 , ⌫ 00 =◆⇤ 0
◆(H 0 , x) = ⌫ 000
45% of assignments are definitions
[Link] <-function(x)print("??")
with(fd,carb*den) [Link]
me <- 42; who(me)
<-function(data,exp,...)
class(me)<-‘man’; eval(substitute(exp),
who(me)
data,
[Link])))
500
and, seem to S3 method redefinitions
al way. The
400
p (maximum
at either. The
300
surprisingly
, but reaches
uper-classes.
200
i.e., classes
are ignored.
100
f method re-
smaller than
t languages.
0
0
1
2
3
4
5
6
7
8
9
10
11−12
13−14
15−19
20−24
24−29
30−39
40−49
50−69
70−99
100−199
200−299
300−402
996
> 1000
the absence
class unions,
Fig. 16. S3 method redefinitions (on x axis).
Objects
setClass("P",representation(x="numeric",y="numeric"))
setClass("C",representation(color="character"))
setClass("CP",contains=c("P","C"))
setGeneric("add",
with(fd,carb*den)function(a, b) standardGeneric("add"))
[Link]
setMethod("add", signature("P","P"),
<-function(data,exp,...)
function(a, b) new("P", x=a@x+b @x, y=a@y+b@y))
eval(substitute(exp),
setMethod("add",signature("CP", "CP"),
function(a, b)new("CP",x=a@x+b@x,y=a@y+b @y,color=a@color)
data,
[Link])))
Object usage
Evaluating the Design of R 23
Built on JNI
OpenJDK, Native libraries
Graal, Truffle (C, Fortran…)
FastR Throughput (w/o JIT)
[Link]
Java cup
Interpretation
12 x+1 3
‘a’
Specialization
guard
f == o
pos_call
gen_call f <- function(a,b,c){a+c}
12 x+1 3
3 12 x+1
‘a’
Runtime specialization
class If {
RNode condE, trueB, falseB;
Object execute(Frame f) {
try {
val = [Link](frame);
} catch (UnexpectedResult e) {
cast = [Link](condE, [Link]());
replaceChild(condE, cast);
return execute(frame);
}
if (val == TRUE) return [Link](f);
if (val == FALSE) return [Link](f);
throw unexpectedNA();
}
Data types
• Memory footprint is
improved
Views
Java cup
a <- b+c +
...
o <- a*2 *
2
A <- function(i, j) {
1 / ((i + j) * (i + j + 1) / 2 + i + 1) }