automatic function call after closing block

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Sep 20 11:41:39 PDT 2008


On Sat, Sep 20, 2008 at 2:40 PM, Jarrett Billingsley
<jarrett.billingsley at gmail.com> wrote:
> On Sat, Sep 20, 2008 at 1:48 PM, Saaa <empty at needmail.com> wrote:
>> :D
>> Looks cool!
>> No way I could have come up with that.
>> Same question (as I have no clue how this internally works):
>> Is there a performance penalty?
>>
>
> No more than any other method that uses delegates; it's just a bit of
> syntax abuse to call a function.
>
> Delegates work by passing an implicit context pointer to the nested
> function as a parameter.  Nested functions use a pointer to the stack
> frame of the enclosing function as the context pointer, so they can
> access the local variables of the enclosing function.  There is a
> performance penalty for calling delegates just as there is for calling
> functions through function pointers: it may cause a processor pipeline
> stall.  However with the preponderance of object-oriented programming
> in which calling virtual methods involves calling functions through
> pointers, many modern processors have been so heavily optimized for it
> that the performance difference from hard-coding the function address
> into the code is negligible.  Not nonexistent, but definitely better
> than it used to be.
>
> And besides -- you're calling that delegate once, so it'll be just a
> constant time overhead.
>

I suppose I should also mention that if you're using D2, delegates
will secretly allocate outer functions' local variables on the heap if
nested functions access them.  There is currently no way in D2 to
achieve the efficient D1 behavior of always keeping the stack frame on
the stack.


More information about the Digitalmars-d-learn mailing list