[R] Copy on assignment and .Internal(inspect())
Carlos Pita
carlosjosepita at gmail.com
Thu Jan 24 05:24:31 CET 2013
Here's another approach using the sampling profiler:
prof <- function() {
Rprof(memory.profiling=T, interval=0.001)
replicate(100, f())
Rprof(NULL)
summaryRprof(memory = "stats")
}
f <- function() {
x = seq(1000)
for(i in seq(1000)) {
x[i] <- x[i] + 1
}
}
prof()
=>
index: "prof":"replicate"
vsize.small max.vsize.small vsize.large max.vsize.large
1938 285003 1629 210990
nodes max.nodes duplications tot.duplications
265805 13949348 7 1703
samples
251
Average duplications are just 9 for 1000 executions of x[i] <- x[i] +
1. A lot of optimization seems to be going on!
How do I make sense of the output listed in my previous post, then?
Best regards
--
Carlos
On Thu, Jan 24, 2013 at 12:38 AM, Carlos Pita <carlosjosepita at gmail.com> wrote:
> 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