Another feature/optimization request (also: macro proposal)
janderson
askme at me.com
Wed Jan 2 21:04:11 PST 2008
downs wrote:
> Okay, this is more of a compiler than language feature request.
>
> While writing the OpenGL API for dglut, I found myself repeatedly using the following idiom:
>
>> void glDoStuff(GLenum foo, int bar, void delegate()[] dgs...) {
>> auto oldState=glGetStateFooBar(foo);
>> scope(exit) glStateFooBar(foo, oldState);
>> glStateFooBar(foo, bar);
>>
>> foreach (dg; dgs) dg();
>> }
>>
>
> Seeing as this, worst-case, comes down to two function call layers, just to do some easier OpenGL stuff, I decided to test how this common situation gets inlined.
>
> For this, I dumped EBP during several parts of the code.
>
>> import std.stdio;
>> void test(void delegate()[] dgs...) {
>> writefln("test: ", getESP());
>> foreach (dg; dgs) dg();
>> }
>>
>> void *getESP() {
>> void *res;
>> asm { mov res, EBP; }
>> return res;
>> }
>>
>> void main() {
>> writefln("main: ", getESP());
>> test({ writefln("dg: ", getESP()); });
>> }
>>
>
> The result: DMD doesn't inline at all. GDC inlines test, but not the delegate.
>
> Because this might cause significant slowdowns, I'd like to request that the language spec guarantee the following:
>
> that in a case such as,
>
>> auto foo={}; foo();
>> // or
>> auto bar=[{}];
>> foreach (b; bar) b(); // foreach can get unrolled at compile-time because the array is static
>>
>
> foo (and b) always get inlined.
>
> Note that I propose no kind of complex value tracking, just this one, simple case.
>
> The effect of this will be to place user-defined control structures taking delegate parameters on the same level as built-in statements.
>
I think that would be best however I think it should be done with
templates (I'm not sure if its already possible?):
ie
void glDoStuff(void delegate()[] dgs)(GLenum foo, int bar)
{
auto oldState=glGetStateFooBar(foo);
scope(exit) glStateFooBar(foo, oldState);
glStateFooBar(foo, bar);
foreach (dg; dgs) dg();
}
>
[snip]
>
-Joel
More information about the Digitalmars-d
mailing list