I've also defined an ALTREP type and it did not work either. I guess this
might be a bug? Or did I miss something?
2. Wrapper objects in ALTREP
If the duplicate function is defined to return the object itself:
SEXP vector_dulplicate(SEXP x, Rboolean deep) {
return(x);
}
In R an ALTREP object will behave like an environment (pass-by-reference).
However, if we do something like(pseudo code):
n=100
It seems like the object alt2 automatically gets wrapped by R. Although at
the R level it seems fine because there are no differences between alt1 and
alt2, if we define a C function as:
SEXP C_peekSharedMemory(SEXP x) {
return(R_altrep_data1(x));
}
and call it in R to get the internal data structure of an ALTREP object.
C_peekSharedMemory(alt1)
C_peekSharedMemory(alt2)
The first one correctly returns its internal data structure, but the second
one returns the ALTREP object it wraps since the wrapper itself is an
ALTREP. This behavior is unexpected. Since the dulplicate function returns
the object itself, I will expect alt1 and alt2 should be the same object.
Even if they are essentially not the same, calling the same function should
at least return the same result. Other than that, It seems like R does not
always wrap an ALTREP object. If we change n from 100 to 10 and check the
internal again, alt2 will not get wrapped. This makes the problem even more
difficult since we cannot predict when would the wrapper appear.
Here is the source code for the wrapper:
https://github.com/wch/r-source/blob/trunk/src/main/altclasses.c#L1399
Here is a working example if one can build the sharedObject package from
https://github.com/Jiefei-Wang/sharedObject
n=100