Purely stack-based generators

Norbert Nemec Norbert at Nemec-online.de
Fri Mar 19 02:29:15 PDT 2010


Walter Bright wrote:
> Norbert Nemec wrote:
>> Regan Heath wrote:
>>> So, these generators are essentially inline functions?
>>
>> No. They may be inlined, but this is just an optimization just like 
>> for  regular functions. If they are not inlined, these generators are 
>> simply functions with a special calling convention that allows them to 
>> retain their local data and execution point on the stack in between 
>> calls.
>>
>> I did work out the assembly code for this some time ago and it worked 
>> nicely. Very similar to what is needed for co-routines, but it is 
>> possible to do everything on the stack.
> 
> How is that different from a local delegate that refers to local 
> variables of the function it is lexically nested in?

It is pretty much the same in the trivial case of one loop having one 
generator in the loop header. The difference shows up in more complex cases:

* Try to design a "filter" (a generator that takes another generator as 
argument) - may be possible but probably ugly to implement.

* Try to run a loop that pulls its values pairwise from two independent 
generators - I don't see a way to do this without serious mental acrobatic.

Apart from that, I guess that the runtime overhead without inlining is 
somewhat different. Would have to investigate that in more detail.



More information about the Digitalmars-d mailing list