[R] storage.mode and argument duplication

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 22 19:07:20 CET 2004


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:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list