GC for pure functions -- implementation ideas

Don nospam at nospam.com
Fri Apr 15 21:56:14 PDT 2011


dsimcha wrote:
> On 4/15/2011 5:01 PM, Sean Kelly wrote:
>> On Apr 15, 2011, at 1:12 PM, Don wrote:
>>>
>>> Create a pure heap for each thread. This is a heap which can only be
>>> used by pure functions. I present some simplistic code, with the
>>> simplest possible implementation: just a big block of memory with a 
>>> thread local 'stack pointer' which points to the first free slot.
>>
>> It's a good idea.  dsimcha was already going to polish his TempAlloc, 
>> wasn't he?  Seems like this is nearly the same thing.
> 
> Yeah, I never formalized it at all, but that's roughly what TempAlloc 
> accomplishes.  My other concern is, what happens in the case of the 
> following code:
> 
> uint nonLeaky() pure {
>     foreach(i; 0..42) {
>          auto arr = new uint[666];
>          // do stuff
>     }
> 
>     return 8675309;
> }
> 
> In this case the arr instance from every loop iteration is retained 
> until nonLeaky() returns, whether it's referenced or not.  Granted, this 
> is a silly example, but I imagine there are cases where stuff like this 
> happens in practice.

Yes. I'm not sure what the best strategy is if you run out of memory 
inside purealloc.
The simple strategy would just be switch to the normal heap once the 
pure heap is full. Then, the gc would have to check for a full pure 
heap. If the pure heap is full, it should scan everything from the last 
heapptr to the end of the pure heap, as well as everything else it 
scans. But you still get the entire size of the pure heap as an 
instant-gc region.


More information about the Digitalmars-d mailing list