I am trying to figure out limits of R with respect to assign in place.
Canonically, only environments are fully pass-by-reference.
However, there is an optimization that allows assign-in-place in a
certain context:
vec = vector("numeric", 8)
vec[1] = 5
assign 5 to position 1 without changing the address of `vec`,
this can be verified either by first running tracemem(vec) or
calling `.Internal(inspect(vec))` before and after the assignment.
The mechanismus used to be NAMED, but was replaced by `reference counting`.
Unfortunately, I have failed to find the full documentation of it.
But the way it seems to be working is that if there is only a single
reference to the given object,
the object will be updated in place (if the types permit this).
However, this doesn't seem to be guaranteed, the following doesn't work
(at least on my computer):
env = new.env(parent = emptyenv()
env$vec = vector("numeric", 8)
env$vec[1] = 5 # so far so good, but
env2 = env # env is passed by reference
env$vec[2] = 6 # memory changed
Aliasing env as env2, normally safe thing for environments, seems to
"confuse"
the reference counter which stops performing the optimization despite
`vec` having REF(1).
---
1. Is there documentation of `reference counting`?
2. Is the demonstrated behaviour a bug?
3. I would guess that assign in place in this case is
implementation-specific detail and not specified behaviour, so one
shouldn't rely on it.
4. Is there way how to do this (i.e., fixed buffer) in base R without
relying on C with .Call?
The following pages mention this behaviour, but don't fully describe it:
*
https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Named-objects-and-copying
*
https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Rest-of-header
*
https://cran.r-project.org/doc/manuals/r-release/R-ints.html#Argument-evaluation
*
https://cran.r-project.org/doc/manuals/r-release/R-ints.html#g_t_002eInternal-vs-_002ePrimitive
Thanks,
Jirka
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel