Array reverse

bearophile bearophileHUGS at lycos.com
Tue Nov 27 16:19:32 PST 2012


Walter Bright:

Sorry for the late reply.

> You've got array bounds checking turned on for 
> algorithm.reverse, and you avoided that in your code by using 
> unchecked pointers.

Right, sorry. This is compiled with the latest DMD 2.061alpha 
with -O -release -inline -noboundscheck. I have used the same 
program source code as above.

Now the calls inside std.algorithm.reverse are fully inlined, 
below there are no "call" istrunctions.

But the number of instructions performed in the loop is rather 
different.

dpaste is currently down and I don't have gdc2/dc2 installed 
here, so I can't show the asm from those other compilers.

I have also done benchmarks, and I have seen performance 
difference in real code between std.algorithm.reverse and a 
reverseArr function.


reverseArr:
         push EBX
         mov ECX, 0Ch[ESP]
         mov EBX, 0Ch[ESP]
         push ESI
         mov ESI, 0Ch[ESP]
         lea ESI, -4[ESI*4][ECX]
         cmp ECX, ESI
         jae L2B
L19:    mov EAX, [EBX]
         mov EDX, [ESI]
         mov [EBX], EDX
         add EBX, 4
         mov [ESI], EAX
         add ESI, 0FFFFFFFCh
         cmp EBX, ESI
         jb  L19
L2B:    pop ESI
         pop EBX
         ret 8


std.algorithm.reverse:
         sub ESP, 014h
         push EBX
         push ESI
         cmp dword ptr 020h[ESP], 0
         je  L76
LC:     mov EAX, 020h[ESP]
         mov EDX, 024h[ESP]
         mov EBX, 020h[ESP]
         mov 0Ch[ESP], EDX
         mov ECX, 0Ch[ESP]
         mov ESI, [ECX]
         mov 014h[ESP], EDX
         mov EDX, 014h[ESP]
         lea EBX, -4[EBX*4][EDX]
         mov 8[ESP], EAX
         mov EDX, [EBX]
         mov [ECX], EDX
         mov ECX, 020h[ESP]
         mov EDX, 024h[ESP]
         mov 010h[ESP], EAX
         lea EAX, -1[ECX]
         add EDX, 4
         mov 020h[ESP], EAX
         mov [EBX], ESI
         mov 024h[ESP], EDX
         cmp dword ptr 020h[ESP], 0
         je  L76
         mov EBX, 020h[ESP]
         lea ESI, -1[EBX]
         mov ECX, 024h[ESP]
         mov 020h[ESP], ESI
         mov 024h[ESP], ECX
         cmp dword ptr 020h[ESP], 0
         jne LC
L76:    pop ESI
         pop EBX
         add ESP, 014h
         ret 8

Bye,
bearophile


More information about the Digitalmars-d mailing list