Ben Deivide de Oliveira Batista
2021-Oct-07 22:46 UTC
[R] There is a relationship of the Modify-in-place optimisation of a object and the local variable `*tmp*`?
Dear R users, When modify-in-place of objects occurs, is there a local variable called `*tmp*`, behind the scenes R? Let's look at two examples to understand the question. Example 1 (R Language Definition) --------------------------------------------------> x <- 1:10 > tracemem(x)[1] "<000000000798F758>"> x[3:5] <- 13:15tracemem[0x000000000798f758 -> 0x0000000008207030]: The result of this command is as if the following had been executed `*tmp*` <- x x <- "[<-"(`*tmp*`, 3:5, value=13:15) rm(`*tmp*`) Conclusion: Here copy-on-modify occurs! Example 2 : Modify-in-place -----------------------> x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) > tracemem(x)[1] "<0000000008BFB818>"> x[3:5] <- 13:15Conclusion: Here modify-in-place occurs!For example 2, is there a local variable `*tmp*` for this case? If so, what would the syntactic representation look like, similar to example 1? -- Ben D?ivide de Oliveira Batista Prof. Estat?stica (DEFIM/CAP/UFSJ) P?gina pessoal/profissional: bendeivide.github.io P?gina Institucional: www.ufsj.edu.br/bendeivide [[alternative HTML version deleted]]
Duncan Murdoch
2021-Oct-08 09:58 UTC
[R] There is a relationship of the Modify-in-place optimisation of a object and the local variable `*tmp*`?
On 07/10/2021 6:46 p.m., Ben Deivide de Oliveira Batista wrote:> Dear R users, > > When modify-in-place of objects occurs, is there a local variable called > `*tmp*`, behind the scenes R? Let's look at two examples to understand the > question. > > Example 1 (R Language Definition) > -------------------------------------------------- > >> x <- 1:10 >> tracemem(x) > [1] "<000000000798F758>" >> x[3:5] <- 13:15 > tracemem[0x000000000798f758 -> 0x0000000008207030]: > > The result of this command is as if the following had been executed > `*tmp*` <- x > x <- "[<-"(`*tmp*`, 3:5, value=13:15) > rm(`*tmp*`) > > Conclusion: Here copy-on-modify occurs! > > > Example 2 : Modify-in-place > ----------------------- > >> x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >> tracemem(x) > [1] "<0000000008BFB818>" >> x[3:5] <- 13:15Conclusion: Here modify-in-place occurs! > For example 2, is there a local variable `*tmp*` for this case? If so, > what would the syntactic representation look like, similar to example 1?I think there is, and it would be the same as in 1. The memory operations are different because x is different: in 1, it is stored in a compact representation of 1:10, which needs to be expanded to a full array of doubles before the entries can be changed. In 2, it is already stored as an array of doubles so this isn't needed. Duncan Murdoch