[R] R copies for no apparent reason

peter dalgaard pdalgd at gmail.com
Fri Mar 3 10:37:24 CET 2017


Right. A semi-short explanation is that R doesn't do full reference counting (*), hence copying happens when you modify an object that _at any previous point_ has been known by two or more names (incl. same name in different environments). 

In the present case, there has been local variable x inside foo(), and global variable x.

-pd

(*) ...yet. Luke Tierney has been working on this; I have temporarily forgotten how much of his work is included in released versions of R.

On 03 Mar 2017, at 00:46 , Bert Gunter <bgunter.4567 at gmail.com> wrote:

> Your assumptions are wrong.
> 
> a full discussion and answer to your question can be found here:
> http://adv-r.had.co.nz/memory.html
> 
> This *is* complex and probably off topic for this list.
> 
> Cheers,
> Bert
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Thu, Mar 2, 2017 at 7:35 AM, Jiucang Hao <jiucang at goraltrading.com> wrote:
>> Hi Everyone,
>> 
>> 
>> 
>> Some R function will make R copy the object AFTER the function call, like
>> nrow, while some others don't, like sum. For example the following code:
>> 
>> x = as.double(1:1e8)
>> 
>> system.time(x[1] <- 100)
>> 
>> y = sum(x)
>> 
>> system.time(x[1] <- 200)     ## Fast (takes 0s), after calling sum
>> 
>> foo = function(x) {
>> 
>>    return(sum(x))
>> 
>> }
>> 
>> y = foo(x)
>> 
>> system.time(x[1] <- 300)     ## Slow (takes 0.35s), after calling foo
>> 
>> Calling foo is NOT slow, because x isn't copied. However, changing x again
>> is very slow, as x is copied. My guess is that calling foo will leave a
>> reference to x, so when changing it after, R makes another copy.
>> 
>> Any one knows why R does this? Even when the function doesn't change x at
>> all? Thanks.
>> 
>> 
>> 
>> 
>> 
>> Regards,
>> 
>> JiuCang Hao
>> 
>> 
>>        [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list