Skip to content

full copy on assignment?

2 messages · Norm Matloff, Martin Morgan

#
Thanks very much.

By the way, I tried setting a GDB breakpoint at duplicate1(), with the
following:

  > x <- 1:10000000
  > x[3] <- 8
  > x[33] <- 88

I found that duplicate1() was called on both of the latter two lines.
I was a bit surprised, since change-on-write would seem to imply that
copying would be done in that second line but NOT on the third.
Moreover, system.time() gave 0.284 user time for the second and 0 on
the third.  YET duplicate1() WAS called on the third, and in stepping
through the code, there didn't seem to be an immediate exit.

Thanks to both John and Duncan for their comment on the fact that using
[<- directly is a very different situation.  That's not what I asked,
but the comment is useful to me for other reasons.

Norm
...
...
...
#
On 04/04/2010 05:27 PM, Norm Matloff wrote:
Here's how I investigated this, with the last line somewhat surprising

  R -d gdb
  gdb> r
  ... cntrl-C
  gdb> break duplicate1
  gdb> commands
  > call Rf_PrintValue(s)
  > c
  > end
  gdb> c

  then

  x=5:1
  x[1L] = 1L # no copy
  x[1L] = 10 # type coercion, new alloc but no copy
  x[1] = 20 # copy of index (!)

Martin