Skip to content

storage.mode and argument duplication

2 messages · Paul Gilbert, Brian Ripley

#
I have a function which passes an element of a fairly large list to a 
fortran call. In a function, when I use storage.mode on an element of a 
list that is in the function argument, does this force the whole list to 
be duplicated? More specifically, which of these should result in less 
extra copying?

foo <- function(x)
   {storage.mode(x$one) <- "double"
    .Fortran("foo", x$one, result=as.double(rep(0,3)))["result"]
   }

or

foo <- function(x)
   {.Fortran("foo", as.double(x$one),
              result=as.double(rep(0,3)))["result"]
   }

Thanks,
Paul Gilbert
#
In R, storage.mode<- is an interpreted function that calls as.double (in 
your case) and copies attributes across so it is definitely worse than the 
second.

Is x$one usually double?  If so then

.Fortran("foo", if(is.double(x$one)) x$one else as.double(x$one), ...)

would save a copy.

Does your Fortran code change that argument?  As is .Fortran will make a
copy on the way in and on the way out. I think you should be able to use
DUP=FALSE, but if x$one is changed you need the as.double to make a copy.
I would not be worrying about anything up I had avoided the copies from
.Fortran.  My inclination would be to write a .Call wrapper to get full
control.
On Wed, 21 Jan 2004, Paul Gilbert wrote: