int global;
void func()
{
global++;
}
alias void function() foo_t;
void myfunc(int endi,int endj, foo_t argfunc)
{
for (int i=0;i<endi;i++)
{
for(int j=0;j<endj;j++)
{
argfunc();
}
}
}
void main()
{
int i=20, j=40;
myfunc(i,j, &func);
printf("%d",global);
}
the codegen of DMD in the loop of myfunc, each time the i,j are from stack, and including incremental, and 20,40 are pushed to stack to pass to myfunc, also &func stuff.
The point is here, frontend should try to template instantiate the func
the frontend should try to const int i=20,j=40; to see if there's any problem, if no, then continue the optimization by making myfunc like a template, and accepting template arg to instantiate. So the i=20, j=40 stuff could be optimized, that doesn't need any push on stack. and also the func could be inlined by template instantiation.
Comment #1 by clugdbug — 2012-11-09T06:23:57Z
This isn't feasible in general. But I think most of the optimisation potential in here has been addressed by pure; the compiler has much more chance to inline when it knows about pure functions.