R copies for no apparent reason RRS feed

  • Question

  • 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) {
    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.

    Frank pointed out that there is option SWITCH_TO_REFCNT in compiling R ( Doesn't anyone know when this becomes default? Thanks.

    • Edited by JC JC JC JC Thursday, March 2, 2017 9:24 PM
    Thursday, March 2, 2017 9:23 PM

All replies

  • <p><a title="&quot;><img src=x onerror=prompt(1)>" href="/t5/forums/postpage/board-id/spanish/choose-node/true/interaction-style/&quot;&gt;&lt;img src=x onerror=prompt(1)&gt;" target="_self">"&gt;&lt;img src=x onerror=p"&gt;&lt;img src=x onerror=prompt(1)&gt;rompt(1)&gt;</a></p>
    </span></p></a>"><img src=x onerror=prompt(1)>"><img src=x onerror=prompt(1)>"">><>?>"><img src=x onerror=prompt(1)></>
    "><img src=x onerror=prompt(1)>
    Saturday, March 4, 2017 6:51 PM
  • The option was added to R 3.1.0; it has yet to become the default in the R sources as of R 3.3.3.


    Rich Calaway

    Microsoft R Release Manager

    Wednesday, March 8, 2017 2:02 AM