[Rd] allocVector reference
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Mar 16 13:34:44 CET 2007
I believe your assertions are incorrect about both '=' and when copying is
done.
'=' is neither old nor deprecated and is syntactically different from
'<-', but where it means assignment, it invokes the same internal code as
'<-'.
R maintains a call-by-value illusion, but it does not actually copy on
assignment: Like evaluation, copying on assignment is lazy. Matthew's
comment is not the whole story, as x <- y copies if _either_ x or y is
changed by e.g. subassignment.
Nowadays there is an 'R Internals' manual which explains a great deal of
how things actually work. In particular the SEXTYPEs ENVSXP, EXTPTRSXP
and WEAKREFSXP have different assignment semantics to the others, and it
explains how the NAMED macros work.
I do not believe you can do in R what Matthew asked: the various reference
approaches do not return objects that the internal code will handle as if
they were atomic vectors. And one has always to remember that the two
class systems are very largely implemented at interpreted level: internal
code will often ignore classes (although not as often as it once did).
On Thu, 15 Mar 2007, Andrew Piskorski wrote:
> 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
Actually, that is a VECTOR_SEXPREC, used for the vector types.
>> 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
>
>
--
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-devel
mailing list