From ripley at stats.ox.ac.uk  Thu Jul 12 19:33:59 2012
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 12 Jul 2012 18:33:59 +0100
Subject: [Rd] Understanding tracemem
In-Reply-To: <CABdHhvHDxJWD3OLyYb2HLQPKSbVrA=gg0hk50SMksBVMVgwLdA@mail.gmail.com>
References: <CABdHhvFfhO-FCa94FE7Tm1qrd1fp7y72Zq-rHN8_84rDig__NQ@mail.gmail.com>
	<4FFF02F4.4050400@stats.ox.ac.uk>
	<CABdHhvHDxJWD3OLyYb2HLQPKSbVrA=gg0hk50SMksBVMVgwLdA@mail.gmail.com>
Message-ID: <4FFF0A87.9030001@stats.ox.ac.uk>

On 12/07/2012 18:20, Hadley Wickham wrote:
>> Read the help carefully as to what 'copy' means:
>>
>>       When an object is traced any copying of the object by the C
>>       function ?duplicate? produces a message to standard output, as
>>       does type coercion and copying when passing arguments to ?.C? or
>>       ?.Fortran?.
>>
>> If you want to understand when 'duplicate' is called, you need to read the
>> source code.  File src/main/subassign.c will explain the different paths
>> taken by your two cases.  But isn't it rather obvious that duplicating x is
>> not useful when a new longer vector needs to be created?
>
> Thanks, that's useful.
>
> Is there any way to detect when a new longer vector is created?  i.e.
> I know that this creates a new vector:

Not programmatically.

> x <- 1:10
> x[11] <- 11L
>
> And this doesn't
>
> y <- list2env(as.list(x))
> y$a <- 11
>
> 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.)

> And thanks to the off-list commenters who pointed out that x[5] <- 5
> is duplicated because 5 is numeric, not integer (oops!)

AFAIK, it does not actually duplicate: see 'type coercion' above.  But 
note that

x <- 1:10
tracemem(x)
x[10:1] <- x

necessarily duplicates.

>
> Hadley
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Understanding tracemem
Prof Brian Ripley