From murdoch at stats.uwo.ca  Fri Sep 28 15:46:39 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Fri, 28 Sep 2007 09:46:39 -0400
Subject: [Rd] modifying large R objects in place
In-Reply-To: <20070928114516.GA22736@cs.cas.cz>
References: <20070926092924.GA9879@cs.cas.cz>	<7098abec0709261052q3a6b01e2v562f9a88ed81c513@mail.gmail.com>	<20070927131317.GB7605@cs.cas.cz>	<Pine.LNX.4.64.0709271502300.6922@gannet.stats.ox.ac.uk>	<20070927203800.GA27247@cs.cas.cz>
	<46FC3122.2010803@biostat.ku.dk> <20070928114516.GA22736@cs.cas.cz>
Message-ID: <46FD05BF.6060005@stats.uwo.ca>

On 9/28/2007 7:45 AM, Petr Savicky wrote:
> On Fri, Sep 28, 2007 at 12:39:30AM +0200, Peter Dalgaard wrote:
  ...
>> Longer-term, I still have some hope for better reference counting, but 
>> the semantics of environments make it really ugly -- an environment can 
>> contain an object that contains the environment, a simple example being 
>> 
>> f <- function()
>>    g <- function() 0
>> f()
>> 
>> At the end of f(), we should decide whether to destroy f's evaluation 
>> environment. In the present example, what we need to be able to see is 
>> that this would remove all refences to g and that the reference from g 
>> to f can therefore be ignored.  Complete logic for sorting this out is 
>> basically equivalent to a new garbage collector, and one can suspect 
>> that applying the logic upon every function return is going to be 
>> terribly inefficient. However, partial heuristics might apply.
> 
> I have to say that I do not understand the example very much.
> What is the input and output of f? Is g inside only defined or
> also used?

f has no input; it's output is the function g, whose environment is the 
evaluation environment of f.  g is never used, but it is returned as the 
value of f.  Thus we have the loop:

g refers to the environment.
the environment contains g.

Even though the result of f() was never saved, two things (the 
environment and g) got created and each would have non-zero reference 
count.

In a more complicated situation you might want to save the result of the 
function and then modify it.  But because of the loop above, you would 
always think there's another reference to the object, so every in-place 
modification would require a copy first.

Duncan Murdoch



[Rd] modifying large R objects in place
Duncan Murdoch