[Rd] allocVector reference

Andrew Piskorski atp at piskorski.com
Thu Mar 15 13:01:49 CET 2007


On Wed, Mar 14, 2007 at 05:44:04PM -0000, Matthew Dowle wrote:
> 
> Hi,
> 
> I'm trying to write a function to return an R vector which points
> directly to a contiguous subset of another vector, without taking a
> copy.

Matthew, I don't know the answer to your question, but this all seems
related to support for references in R.  I've included my notes on R
references below.

Ah, I think Jens Oehlschlaegel's "ref" package is what you want:

  http://tolstoy.newcastle.edu.au/R/packages/04/0008.html

  Class refdata is a transparent wrapper to matrices and data.frames
  which allows for memory efficient nested subsetting. I.e. you can
  create a subset of a subset ... of a data.frame without duplicating
  the data in memory, instead only indexing information is duplicated.

> Since SEXPREC is a header followed by the data, rather than the
> header plus a pointer to the data, I'm not sure what I'm trying to
> do is possible.  Is there a way of doing this?  Similar in spirit to
> how the R assignment "x=y" does not copy y until x is modified,

Is you last statement above in fact true?  I was under the impression
that R does NOT do copy on write, that when you make a copy of a
variable, R immediately allocates memory and makes a deep copy of the
value.

But you're using old deprecated "=" for assignment, which is weird, so
maybe you mean when you pass named arguments to a function?  Function
args are evaluated lazily, and I think that is used (I don't know how
exactly) to give copy on write behavior - but only for function
arguments.

My (older) notes on R references:

R does not support reference variables and does not do copy on write -
when you copy a variable, it eagerly allocates all memory and makes a
deep copy.  (I believe S-Plus has the same behavior but have not
checked.)  This can be annoying...

There are however specific exceptions.  For example, R environments
are treated as references, NOT values like most everything else in R.
The July 2006 "attributes of environments" thread makes that clear:

  https://stat.ethz.ch/pipermail/r-devel/2006-July/038352.html

Jens Oehlschlaegel's ref package implements references for both R and
S-Plus:

  http://cran.r-project.org/src/contrib/Descriptions/ref.html
  http://www.maths.lth.se/help/R/.R/library/ref/html/00Index.html
  http://tolstoy.newcastle.edu.au/~rking/R/packages/04/0008.html

Henrik Bengtsson's R.oo package emulates reference variables via R
environments (but perhaps only in the context of his OO framework, I'm
not sure).

  http://www.braju.com/R/
  http://cran.r-project.org/src/contrib/Descriptions/R.oo.html
  http://www.maths.lth.se/help/R/R.oo/

Bengtsson also wrote a 2002 paper, "Implementing support for
references in R":

  http://www.maths.lth.se/help/R/ImplementingReferences/

Further out, see also Tierny's (mostly old) R development notes

  Notes on References, External Objects, or Mutable State for R:
    http://www.stat.uiowa.edu/~luke/R/references.html
  Simple References with Finalization:
    http://www.stat.uiowa.edu/~luke/R/simpleref.html
  Finalization and Weak References in R:
    http://www.stat.uiowa.edu/~luke/R/references/weakfinex.html

-- 
Andrew Piskorski <atp at piskorski.com>
http://www.piskorski.com/



More information about the R-devel mailing list