[Rd] avoid copying big object passed into optimize()

From: Zepu Zhang <zepu.zhang_at_gmail.com>
Date: Wed, 09 Mar 2011 17:15:15 -0900

Hello list,

I have the following scenario:

f1 <- function(a)

     .... # doing things; may need 'a', but does not change 'a'.

     g <- function(x)
          sum(x + a)    # Say. Use 'a'; does not change 'a'.

     optimize(f = g, lower = 0, upper = 1)

f2 <- function()

    b <- runif(100000000000) # Create big object.

    f1(a = b)

My main concern is to reduce copying of the big object 'a'. Questions:

(1) In f1, 'a' never appears on the LHS of assignment. Is it passed by value
or by reference? Say the situation is simpler and more general: no optimization call in f1.

(2) Is there any difference, as far as copying of the big 'a' is concerned,
if 'g' is changed to

   g <- function(x, b) { sum(x + b) }
and called by

    optimize(f = g, lower = 0, upper = 1, b = a)

(3) Is 'a' passed into the C optimization function one-off, or again and
again across the C-R interface?

(4) Does it help if I remove the argument 'a' of 'f1', and let 'g' look for
it (of course it should be referred to as 'b' now) directly in the
environment of 'f2'?

(5) Any suggestions?

Many thanks for your help!


        [[alternative HTML version deleted]]

R-devel_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 10 Mar 2011 - 02:27:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Mar 2011 - 07:50:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive