This is because R keeps track of the names of an object, until there are 2
names. Thus, once it reaches 2, it can no longer decrement the named count.
In this example, 'a' reaches 2 names ('a' and 'b'), thus R does not know
that 'a' only has one name at the end.

Luke has added reference counting to R 3.1 to get around these types of
problems. If you want to try it out, make the necessary change in
Rinternals.h and recompile. With reference counting, R knows that 'a' only
has one reference and avoids the copy.

> a <- seq.int(10)
> a[1:4] <- 4:1
> b <- a
> .Internal(inspect(a))
@2b71608 13 INTSXP g0c4 [REF(2)] (len=10, tl=0) 4,3,2,1,5,...
> b[1:4] <- 1:4
> .Internal(inspect(b))
@2b715a0 13 INTSXP g0c4 [REF(1)] (len=10, tl=0) 1,2,3,4,5,...
> .Internal(inspect(a))
@2b71608 13 INTSXP g0c4 [REF(1)] (len=10, tl=0) 4,3,2,1,5,...
> a[1:4] <- 1:4
> .Internal(inspect(a))
@2b71608 13 INTSXP g0c4 [REF(1)] (len=10, tl=0) 1,2,3,4,5,...



On Mon, Jun 2, 2014 at 9:31 AM, Dénes Tóth <toth.de...@ttk.mta.hu> wrote:

>
> Hi,
>
> Please consider the following code:
>
> a <- seq.int(10)  # create a
> tracemem(a)
> a[1:4] <- 4:1   # no internal copy
> b <- a          # no internal copy
> b[1:4] <- 1:4   # copy, b is not a any more
> a[1:4] <- 1:4   # copy, but why?
>
> With results:
> > a <- seq.int(10)
> > tracemem(a)
> [1] "<0x1792bc0>"
> > a[1:4] <- 4:1
> > b <- a
> > b[1:4] <- 1:4
> tracemem[0x1792bc0 -> 0x1792b58]:
> > a[1:4] <- 1:4
> tracemem[0x1792bc0 -> 0x1792af0]:
>
>
> ##
>
> Could you provide a brief explanation or point me to a source why R needs
> a copy in the final step?
>
>
> Best,
>   Denes
>
>
>
> > sessionInfo()
> R version 3.1.0 (2014-04-10)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to