[Rd] reference class internals
matloff at cs.ucdavis.edu
Fri Jan 10 04:53:56 CET 2014
Thanks, Hadley and Simon.
The reason I asked today was that when reference classes first came out,
it had appeared to me that there is no peformance advantage to using
reference classes, that it was mainly a style issue (encapsulation,
etc.). Unless I'm missing something, both of you have confirmed my
original impression, correct?
On Thu, Jan 09, 2014 at 09:44:10PM -0500, Simon Urbanek wrote:
> On Jan 9, 2014, at 6:20 PM, Norm Matloff <matloff at cs.ucdavis.edu> wrote:
> > Bottom line: Really no different from the case of ordinary vectors that are not in reference classes, right? In other words, not true pass-by-reference.
> The pass-by-reference applies to the object itself, not necessarily to anything you obtain by calling a function on the object (like extracting a part from it). Vectors are not reference-semantics objects so regular rules apply.
> If you pass a reference semantics object to a function, the function can modify the object. If you pass any other object, the contents are guaranteed to not be touched. Reference-semantics objects in R are literally passed by reference (same C pointer), so yes, it is true pass-by-reference.
> (*) - technically, there is a thin non-refernce wrapper around the instances of reference classes, because there are things you don't want to happen to your ref-semantics instance - e.g. you don't want unclass(x) to destroy x and all instances of it (which it would do if there was no wrapper). But the actual payload of the object is a true ref-semantics object - an environment - that is always passed by reference.
> > Norm
> > On Thu, Jan 09, 2014 at 04:43:44PM -0600, Hadley Wickham wrote:
> >> It's a bit of a simplification, reference classes are wrappers around
> >> environments. So if modifying a value in an environment would create
> >> a copy, then modifying the same value in a reference class will also
> >> create a copy.
> >> The situation with modifying a vector is a bit complicated as it will
> >> sometimes be modified in place and sometimes be duplicated and
> >> modified (depending on whether its NAMED attribute is 1 or 2, and
> >> exactly how you're modifying it).
> >> Hadley
> >> On Thu, Jan 9, 2014 at 4:33 PM, Norm Matloff <matloff at cs.ucdavis.edu> wrote:
> >>> I have a question about reference classes, which someone here
> >>> undoubtedly can answer immediately, saving me hours of wading through
> >>> indecipherable internal code. :-) Thanks in advance.
> >>> Reference class data is mutable, fine, but in what sense? Is it really
> >>> physical, or is it just a view given to the programmer?
> >>> If for instance I have vector as a field in a reference class, and I
> >>> change one element of the vector, is it really true that the change is
> >>> guaranteed to be made in-place, no copying, no memory reallocation etc?
> >>> Norm
> >>> ______________________________________________
> >>> R-devel at r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >> --
> >> http://had.co.nz/
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel