From winstonchang1 at gmail.com  Fri Aug  5 19:27:57 2016
From: winstonchang1 at gmail.com (Winston Chang)
Date: Fri, 5 Aug 2016 12:27:57 -0500
Subject: [Rd] Extra copies of objects in environments when using $
	operator?
In-Reply-To: <alpine.DEB.2.10.1608051127550.2874@luke-Latitude>
References: <CAFOpNVE1=WeyQ-PN8hsafVwd7kVtkLubfdkxZhMmU7Ss-X++cQ@mail.gmail.com>
	<alpine.DEB.2.10.1608051127550.2874@luke-Latitude>
Message-ID: <CAFOpNVF7d=785BC9_8p2w2p2XXwqXh27gd7DgRrHYWZOQ-Ke6A@mail.gmail.com>

>
>
>> However, modifying a list contained in an environment *does* result in
>> a copy -- tracemem prints out some info when we do the assignment:
>>  e <- new.env(parent = emptyenv())
>>  e$x <- list(1)
>>  tracemem(e$x)
>>  # [1] "<0x1148c1708>"
>>  e$x[[1]] <- 2
>>  # tracemem[0x1148c1708 -> 0x11b2fc1b8]:
>>
>
> Currently e$x marks values as immutable if they have any references by
> setting NAMED to 2. You can see this with
>
> e <- new.env(parent = emptyenv())
>> e$x <- list(1)
>> .Internal(inspect(e))
>>
> @30b2498 04 ENVSXP g0c0 [NAM(1)] <0x30b2498>
> ENCLOS:
>   @2600e98 04 ENVSXP g0c0 [MARK,NAM(2)] <R_EmptyEnv>
> HASHTAB:
>   @2e41540 19 VECSXP g0c7 [] (len=29, tl=1)
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @30b3370 02 LISTSXP g0c0 []
>       TAG: @2637870 01 SYMSXP g0c0 [MARK,NAM(2)] "x"
>       @3569488 19 VECSXP g0c1 [NAM(1)] (len=1, tl=0)   ## <--- NAM = 1
>       @35694e8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 1
>     ...
>
>> e$x
>>
> [[1]]
> [1] 1
>
> .Internal(inspect(e))
>>
> @30b2498 04 ENVSXP g0c0 [NAM(1)] <0x30b2498>
> ENCLOS:
>   @2600e98 04 ENVSXP g0c0 [MARK,NAM(2)] <R_EmptyEnv>
> HASHTAB:
>   @2e41540 19 VECSXP g0c7 [] (len=29, tl=1)
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @25c9628 00 NILSXP g0c0 [MARK,NAM(2)]
>     @30b3370 02 LISTSXP g0c0 []
>       TAG: @2637870 01 SYMSXP g0c0 [MARK,NAM(2)] "x"
>       @3569488 19 VECSXP g0c1 [NAM(2)] (len=1, tl=0)   ## <--- NAM = 2
>       @35694e8 14 REALSXP g0c1 [NAM(2)] (len=1, tl=0) 1
>     ...
>
> It is not clear if this is needed or just done in an abundance of
> caution. If R is built to use reference counting for determining
> sharing information this does not happen, so this is likely to change
> and not force a copy by 3.4.0.
>

Excellent, that's great to hear!

-Winston

	[[alternative HTML version deleted]]



[Rd] Extra copies of objects in environments when using $ operator?
Winston Chang