From mtmorgan at fhcrc.org  Mon Apr  5 03:17:01 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Sun, 04 Apr 2010 18:17:01 -0700
Subject: [Rd] full copy on assignment?
In-Reply-To: <20100405002734.GA28962@laura>
References: <mailman.13.1270375206.14388.r-devel@r-project.org>
	<20100405002734.GA28962@laura>
Message-ID: <4BB93A0D.7080807@fhcrc.org>

On 04/04/2010 05:27 PM, Norm Matloff wrote:
> 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

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

> 
> 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
> 
>> Message: 4
>> Date: Sat, 03 Apr 2010 17:54:58 -0700
>> From: John Chambers <jmc at r-project.org>
>> To: r-devel at r-project.org
>> Subject: Re: [Rd] full copy on assignment?
> ...
> ...
>> How often does y get duplicated? Hopefully not a million times.  One can 
>> look at this in gdb, by trapping calls to duplicate1.  The answer is:  
>> just once, to ensure that the object is local.  Then the duplicated 
>> version has only one reference and the primitive replacement doesn't 
>> copy it.
> ...
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



[Rd] full copy on assignment?
Martin Morgan