Until v3.5 (I think), R had a poor-man's reference count facility called NAMED. In a nutshell, an object's NAMED attributed can hold 3 values:
- 0 - the object is not named (i.e., not referenced and can be garbage collected)
- 1 - the object is referenced by a single name (and so can be modified in place)
- 2 - a.k.a "many": the object is referenced by many names. If it changes - needs to be copied. If a name is removed the NAMED attribute would not decrement.
(some more details here).
I'm trying to view this attribute from R, and am experimenting both with pryr::refs(x)
and .Internal(inspect(x))
. I can't understand the results of either.
tst <- function(x) {
print(pryr::refs(x)) # 0
.Internal(inspect(x)) # 2
x[5] <- as.integer(14)
print(pryr::refs(x)) # 1
.Internal(inspect(x)) # 1
y<-x
print(pryr::refs(x)) # 2
.Internal(inspect(x)) # 2
}
a <- 1:100
tst(a)
# [1] 0
# @5646a302a020 13 INTSXP g0c7 [NAM(2)] (len=100, tl=0) 1,2,3,4,5,...
# [1] 1
# @5646ab296810 13 INTSXP g0c7 [NAM(1)] (len=100, tl=0) 1,2,3,4,14,...
# [1] 2
# @5646ab296810 13 INTSXP g0c7 [NAM(2)] (len=100, tl=0) 1,2,3,4,14,...
Can anyone shed some light on what's going on here? Is there a better way to inspect an object's references?