Closed dselivanov closed 7 years ago
This is a consequence of how R does subset assignment, as in a[1] <- 2
. It creates a *tmp*
variable. See:
Here's a minimal example:
# ----- Using `$<-` assignment ------
e <- new.env()
e$x <- 1
tracemem(e$x)
# [1] "<0x10d6a0af8>"
x_copy <- e$x
e$x <- 2 # No tracemem output
x_copy <- e$x
e$x <- 3 # No tracemem output
# ----- Using two levels of subset assignment ------
e <- new.env()
e$x <- 1
tracemem(e$x)
# [1] "<0x10d29e758>"
x_copy <- e$x[1]
e$x[1] <- 2
# tracemem[0x10d29e758 -> 0x10d29e9f8]:
x_copy <- e$x[1]
e$x[1] <- 3
# tracemem[0x10d29e9f8 -> 0x10d29ebd8]:
I believe that the byte compiler (which is going to be enabled by default on future versions of R, for packages on CRAN) avoids creating *tmp*
, but I don't know for sure.
I faced unexpected behavior with
R6
classes. For some reason during update of internal fields R makes unnecessary copy of the field (where modification should be in place).