Write Barrier: was: [Rd] function-like macros undefined

Vadim Ogranovich vograno at evafunds.com
Wed Mar 16 19:34:56 CET 2005


Thank you to Duncan Murdoch for pointing to
I have a couple of questions in this regard:

* suppose that inside a C function I have a SEXP vector x of integers
and I want to increment each element by one. I understand that

int * xIPtr = INTEGER(x);
int i;

for (i=0; i<LENGTH(x); ++i)
     SET_VECTOR_ELT(x, i, xIPtr[i]+1);

is the recommended way of doing it. However it seems that only the very
first call to SET_VECTOR_ELT, i.e. the one that corresponds to i=0, is
strictly necessary. For example, and this is my question, the following
should be perfectly safe:

SET_VECTOR_ELT(x, 0, xIPtr[0]);

for (i=0; i<LENGTH(x); ++i)

Admittedly this looks a bit odd and breaks if LENGTH(x) is zero, but it
illustrates the point.

* Now, if the above variation is safe, maybe there is a macro that
simply marks atomic SEXP-s, i.g. integers and doubles, for modification?

* The "Write Barrier" document has a section "Changing the
Representation of String Vectors". Is this something which is in works,
or planned, for future versions? It would be great if it were, this
should give R considerable speed boost.


More information about the R-devel mailing list