[R] Avoiding a memory copy by [[

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 23 17:23:09 CEST 2006


On Tue, 23 May 2006, Matthew Dowle wrote:

>
> Hi,
>
> n = 10000000
> L = list(a=integer(n), b=integer(n))
>
> L[[2]][1:10]  gives me the first 10 items of the 2nd vector in the list L.
> It works fine.  However it appears to copy the entire L[[2]] vector in
> memory first, before subsetting it.  It seems reasonable that "[[" can't
> know that all that is to be done is to do [1:10] on the result and therefore
> a copy in memory of the entire vector L[[2]] is not required.  Only a new
> vector length 10 need be created.  I see why [[ needs to make a copy in
> general.
>
> L[[c(2,1)]]  gives me the 1st item of the 2nd vector in the list L.  It
> works fine,  and does not appear to copy L[[2]] in memory first.  Its much
> faster as n grows large.
>
> But I need more than 1 element of the vector ....  L[[c(2,1:10)]]   fails
> with "Error: recursive indexing failed at level 2"

Note that [[ ]] is documented to only ever return one element, so this is 
invalid.

> Is there a way I can obtain the first 10 items of L[[2]] without a memory
> copy of L[[2]]  ?

Use .Call

-- 
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