Safer casts

Yigal Chripun yigal100 at gmail.com
Tue May 13 01:14:35 PDT 2008


Dee Girl wrote:

>> I didn't know that Walter is working on making inlining delegates 
>> possible, that this is indeed great news. :)
> 
> I think this is great but it is much easy said than done. Inlining an
> indirect call means the callee must be inlined too. Or at least
> duplicated. There is complicated analysis that shows which indirect
> calls can be inlined that way. Not all can.
> 
OK. so what?
either way, It's the compiler/linker job to make optimizations. it sure
is more capable and has more info than I am.
I'm not implementing the D compiler, I'm using it. my POV is that of the
user. I believe Walter is a genius compiler writer and that if it's
possible he'll make it happen.

>> to answer your questions: as I understand it, a delegate is
>> internally a struct with two pointers: a context pointer and a
>> function pointer. the context pointer has the address of the outer
>> function for nested functions, or the this pointer for methods. so,
>> when passing a delegate you pass this struct. based on that, I
>> think that the answer to your second question is that the call is
>> indirect.
> 
> This answer is incorrect. The call is direct. I clarify: this is not
> matter of optimization or tricks. It is matter of principle. It is
> coming from the very clever way Walter instantiates templates with
> alias arguments. He specializes the template at the call site. I wish
> I had his idea for my project but not it is too late ^_^
> 
> I think this is very important feature of the D programming language.
I'm confused by your terminology. anyway, this is not really that
important to me, again I trust Walter to make the compiler produce the
best possible code.
I meant that the sort instance calls the delegate anyway and does not
inline it. I'm not sure what you mean in direct vs. indirect and why
this is important. I thought that the important thing for performance
was whether the call gets inlined or not.
I'm saying here that both options (the template instance and the regular
 function) do the same regarding inlining the delegate.
> 
> 
>> this is true for both cases, otherwise that trick of using a
>> sort!() instance inside a mySort function wouldn't work. all you
>> pass to the template version is just a D symbol, in our case the 
>> name of the delegate. in both cases you need to call that delegate.
>> 
> 
> In the example I wrote it is not the name of the delegate. It is the
> name of nested function. It is anonymous but still has internal name.
> 

see Janice's reply. effectively this is the same thing.

> 
>> you can pass regular function pointers to the template too (they
>> are written either with the function reserved word, or with the C
>> syntax) and those are simpler to inline. you can overload a
>> non-templated sort function to receive those as well.
> 
> If a pointer to function is passed then template function is
> instantiated with that pointer type. Discussion on when that can be
> inlined takes to another subject.
> 
>> I hope, some day function pointers would be implicitly castable to 
>> delegates (just make the context pointer contain null) and this
>> will remove the need for providing two identical functions (one for
>> delegates and one for function pointers) btw, another way of
>> changing the delegate for a specific sort!() instance is simply to
>> assign it; no need for a wrapper function. delegate bool(int a, int
>> b) dg; dg = aDelegate; sort!(dg)(array); dg = otherDelegate; 
>> sort!(dg)(array); // the same instance should be used.
> 
> That is correct. The same instance will be used. Thank you, Dee Girl
> 



More information about the Digitalmars-d mailing list