From hadley at rice.edu  Thu Jul 12 19:46:42 2012
From: hadley at rice.edu (Hadley Wickham)
Date: Thu, 12 Jul 2012 10:46:42 -0700
Subject: [Rd] Understanding tracemem
In-Reply-To: <4FFF0A87.9030001@stats.ox.ac.uk>
References: <CABdHhvFfhO-FCa94FE7Tm1qrd1fp7y72Zq-rHN8_84rDig__NQ@mail.gmail.com>
	<4FFF02F4.4050400@stats.ox.ac.uk>
	<CABdHhvHDxJWD3OLyYb2HLQPKSbVrA=gg0hk50SMksBVMVgwLdA@mail.gmail.com>
	<4FFF0A87.9030001@stats.ox.ac.uk>
Message-ID: <CABdHhvHnS1zxORys7s2v1TB45qUjhkHxHqJ5COvRvGG+enyLkw@mail.gmail.com>

>> But does this?
>>
>> z <- as.list(x)
>> z$a <- 11
>
> Yes of course, as z is now of length 11.  There is no provision in R to
> extend a vector except by creating a new one.  (Well, there is at C level
> but I think it is not currently used.)

I guess a better example is

z <- list(a = 1:1e6, b = runif(1e6))
z$c <- 1

The list gets copied, but do a and b, or does the new list point to
the existing locations?  The following test suggests that it's a deep
copy.

x <- 1:1e7
z <- list(a = x)

system.time(replicate(100, z$b <- 1L)) / 100
# ~ 0.05s
system.time(replicate(100, x[1e6 + 1L] <- 1L)) / 100
# ~ 0.04s

Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] Understanding tracemem
Hadley Wickham