Thread overview
Inline optimization question
Jun 07, 2003
Dario
Jun 07, 2003
Dario
Jun 07, 2003
Walter
Jun 08, 2003
Dario
Jun 08, 2003
Walter
June 07, 2003
Consider this code:
        void loop(int[] a, int function(int) f)
        {
            for(uint i=0; i<a.length; i++)
                a[i] = f(a[i]);
        }
        int main()
        {
            int[] a = getAnArray();
            loop(a, function int(int a) { return a+1; });
            return 0;
        }
Is the compiler able to optimize this to become:
        int main()
        {
            int[] a = getAnArray();
            for(uint i=0; i<a.length; i++)
                a[i] = a[i]+1;
            return 0;
        }
?


June 07, 2003
I tried to use obj2asm on the object file but
nothing seems to be inlined.
loop() isn't inlined in main() and the pointer
isn't inlined in loop().

I understand that it is a difficult task to inline
everything, but why isn't loop() inlined in main()?


June 07, 2003
Did you try the -inline flag?

"Dario" <supdar@yahoo.com> wrote in message news:bbshga$1phi$1@digitaldaemon.com...
> I tried to use obj2asm on the object file but
> nothing seems to be inlined.
> loop() isn't inlined in main() and the pointer
> isn't inlined in loop().
>
> I understand that it is a difficult task to inline
> everything, but why isn't loop() inlined in main()?
>
>


June 08, 2003
module test;
void loop(int[] a, int function(int) f)
{
    for(uint i=0; i<a.length; i++)
        a[i] = f(a[i]);
}
void main()
{
    int[] a = new int[10];
    loop(a, function int(int b) { return b+1; });
    return 0;
}
_________________________________________

dmd -c -release -inline -O test.d
obj2asm test.obj
_________________________________________

_TEXT segment dword use32 public 'CODE' ;size is 0
_TEXT ends
_DATA segment para use32 public 'DATA' ;size is 0
_DATA ends
CONST segment para use32 public 'CONST' ;size is 0
CONST ends
_BSS segment para use32 public 'BSS' ;size is 0
_BSS ends
FLAT group
 extrn _Dtest_loop_FAiPFiZiZv
includelib phobos.lib
 extrn _main
 extrn __acrtused_con
 extrn __Dmain
 extrn _Dtest_main__FiZi
 extrn __d_new
_Dtest_loop_FAiPFiZiZv COMDAT flags=x0 attr=x0 align=x0
__Dmain COMDAT flags=x0 attr=x0 align=x0
_Dtest_main__FiZi COMDAT flags=x0 attr=x0 align=x0

_TEXT segment
 assume CS:_TEXT
_TEXT ends
_DATA segment
_DATA ends
CONST segment
CONST ends
_BSS segment
_BSS ends
_Dtest_loop_FAiPFiZiZv comdat
 assume CS:_Dtest_loop_FAiPFiZiZv
  push EAX
  push EAX
  push EBX
  xor EBX,EBX
  push ESI
  push EDI
            ;move function pointer in EDI
  mov EDI,EAX
  cmp 018h[ESP],EBX
  je L30
  mov 010h[ESP],EBX
  mov EDX,01Ch[ESP]
  mov EBX,018h[ESP]
  mov ESI,EDX
  mov EBX,010h[ESP]
L21:  mov EAX,[EBX*4][ESI]
            ;call function (it's not inlined)
  call EDI
  mov [EBX*4][ESI],EAX
  inc EBX
  cmp EBX,018h[ESP]
  jb L21
L30:  pop EDI
  pop ESI
  pop EBX
  add ESP,8
  ret 8
_Dtest_loop_FAiPFiZiZv ends
__Dmain comdat
 assume CS:__Dmain
L0:  push EAX
  push 4
  push 0Ah
  call near ptr __d_new
  add ESP,8
  push EDX
  push EAX
            ;pass function pointer
  mov EAX,offset FLAT:_Dtest_main__FiZi
            ;call function (it's not inlined)
  call near ptr _Dtest_loop_FAiPFiZiZv
  xor EAX,EAX
  pop ECX
  ret
__Dmain ends
_Dtest_main__FiZi comdat
 assume CS:_Dtest_main__FiZi
  inc EAX
  ret
_Dtest_main__FiZi ends
 end



June 08, 2003
"Dario" <supdar@yahoo.com> wrote in message news:bbshga$1phi$1@digitaldaemon.com...
> I understand that it is a difficult task to inline
> everything, but why isn't loop() inlined in main()?

Most inlines are done that resolve to expressions. Loops are another level of complexity entirely. It usually adds nothing to inline a loop anyway because the time spent in the loop far exceeds the time spent doing a function call.