From bates@stat.wisc.edu  Fri Sep 15 18:47:28 2000
From: bates@stat.wisc.edu (Douglas Bates)
Date: 15 Sep 2000 12:47:28 -0500
Subject: [Rd] classed
In-Reply-To: Paul Gilbert's message of "Fri, 15 Sep 2000 11:53:04 -0400"
References: <3.0.32.20000915142129.0129d2d0@pophost.nsw.cmis.csiro.au> <20000915165809.I24267@ftoomsh.progsoc.uts.edu.au> <39C245E0.A2B36FA6@bank-banque-canada.ca>
Message-ID: <6rn1h9mtsf.fsf@franz.stat.wisc.edu>

I reached a similar conclusion to Thomas Lumley.  Here is the response
I wrote before I got a chance to read Thomas's response.

Paul Gilbert <pgilbert@bank-banque-canada.ca> writes:

> >If it doesn't use copy-on-write then the result will be additional
> >slowdown when all your data is copied again and again (I guess it depends
> >on how big the object is).
> 
> I've often wondered about this. Any comments from someone who really knows the
> internals of R? When I first implemented it I did some testing and didn't notice
> any really big slowdown and I do use a lot of fairly big (multi MB size)
> objects. But notice that the difference is really in the function call, not in
> the copying of objects. A function using classed would end
> 
>   classed(x, "whatever") }
> 
> rather than
> 
>  class(x) <- "whatever"
>  x}

Assignment functions like "class<-" are allowed to modify their first
argument in place.  That's the whole reason for using constructions
like foo[] <- bar, which is really a call to "[<-" with arguments foo
and bar, instead of foo <- bar.  It saves on copying.

An R function like classed(x, "cname") cannot modify its
arguments so it must return a copy.

I think the internal C code in src/main/attrib.c indicates that
do_classgets does not copy the first argument unless it happens to be
a named object.  (Does anyone know why a named object has to be copied?)

SEXP do_classgets(SEXP call, SEXP op, SEXP args, SEXP env)
{
    checkArity(op, args);
    if (NAMED(CAR(args)) == 2) SETCAR(args, duplicate(CAR(args)));
    if (length(CADR(args)) == 0) SETCADR(args, R_NilValue);
    setAttrib(CAR(args), R_ClassSymbol, CADR(args));
    return CAR(args);
}

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] classed
Douglas Bates