开发者

What does `retracemem` do in R?

开发者 https://www.devze.com 2023-04-03 16:26 出处:网络
A开发者_开发技巧fter answering about tracemem recently, I learned about retracemem.The help for ?retracemem and the example therein leave me unenlightened.

A开发者_开发技巧fter answering about tracemem recently, I learned about retracemem. The help for ?retracemem and the example therein leave me unenlightened.

What does retracemem actually do and why? It doesn't do what I thought it would do, i.e. point one object to the memory location of another, at least as far as I can tell.

I tried a wizardly invocation like .Internal(inspect()), which performs no magic for me:

> a = 1:10
> b = a[-1]
> .Internal(inspect(a))
@00000000087AE578 13 INTSXP g0c4 [NAM(2)] (len=10, tl=23336) 1,2,3,4,5,...
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...
> retracemem(b,retracemem(a))
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...


I think retracemem() just lets you tag a variable copy, which wouldn't produce a tracemem statement (such as a b above which is really just a copy of a sans the first element), as being derived from the same source variable, so you can continue to watch the copies/partial copies propagate and see that they derive from the same source.

For example, how does A's memory get copied/propagated:

> A <- 1:10
> tracemem(A)
[1] "<0x100a2a978>"
> B <- A                                # Assignment to B doesn't make copy
> C <- A + 1                            # Assignment to C makes copy, alters it
tracemem[0x100a2a978 -> 0x1020ebbf0]: 
> D <- C + 1                            # Assignment to D makes copy, alters it
tracemem[0x1020ebbf0 -> 0x1020ebc98]: 
> E <- B + 1                            # Assignment to E makes copy, alters it
tracemem[0x100a2a978 -> 0x1020a4208]: 
> F <- A[-1]                            # Assignment to F doesn't make copy?
> G <- F + 1                            # Even after altering it?
> retracemem(F, retracemem(A))          # Hint to R that F is really A derived
tracemem[<0x100a2a978> -> 0x1009c5910]: 
> G <- F + 1                            # Reassignment to G makes copy, alters it
tracemem[0x1009c5910 -> 0x1020a4748]: 

But then again, I could be entirely wrong...

0

精彩评论

暂无评论...
验证码 换一张
取 消