[R] Copy on assignment and .Internal(inspect())

Carlos Pita carlosjosepita at gmail.com
Thu Jan 24 04:38:10 CET 2013


Hi,

I would like to know if it's ok to use .Internal(inspect(x)) in order
to detect vector copying.

Take for example the following silly code:

f <- function() {
  x = seq(10)
  print(.Internal(inspect(x)))
  for(i in seq(10)) {
    x[i] <- x[i] + 1
    print(.Internal(inspect(x)))
  }
}

The output of f() was:

@bd7acf0 13 INTSXP g0c4 [NAM(1)] (len=10, tl=0) 1,2,3,4,5,...
 [1]  1  2  3  4  5  6  7  8  9 10
@bdd6f80 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,2,3,4,5,...
 [1]  2  2  3  4  5  6  7  8  9 10
@ba66278 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,3,4,5,...
 [1]  2  3  3  4  5  6  7  8  9 10
@ba661e0 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,4,5,...
 [1]  2  3  4  4  5  6  7  8  9 10
@ba65ee8 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,5,...
 [1]  2  3  4  5  5  6  7  8  9 10
@ba65e50 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  6  7  8  9 10
@ba65db8 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  7  7  8  9 10
@ba65c88 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  7  8  8  9 10
@ba6a228 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  7  8  9  9 10
@ba6a190 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  7  8  9 10 10
@ba6a0f8 14 REALSXP g0c6 [NAM(1)] (len=10, tl=0) 2,3,4,5,6,...
 [1]  2  3  4  5  6  7  8  9 10 11

Notice that the memory reference is different each time. But according
to http://r.789695.n4.nabble.com/full-copy-on-assignment-td1750555.html
I (possibly a mistake on my part) understand that some optimization
should be taking place.

Is right to conclude from the output above that the entire vector is
being copied each time or is just some kind of "shallow copy" (maybe
some kind of view of the vector but not the vector itself is being
copied). Obviously I'm not familiarized with r internals.

Best regards
--
Carlos



More information about the R-help mailing list