[Issue 2008] Poor optimization of functions with ref parameters

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 15 21:06:19 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2008


David Simcha <dsimcha at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha at yahoo.com




--- Comment #2 from David Simcha <dsimcha at yahoo.com>  2009-05-15 21:06:18 PDT ---
This is true, and also applies to D2.  Apparently ref prevents inlining.  It's
somewhere in the comments in inline.c.  Why, I don't know.  The weird thing is
that apparently DMD inlines stuff with pointer parameters, and ref parameters
are just syntactic sugar for pointers.  Here's a test program and the
disassembly.

import std.stdio;

// Shouldn't this generate *exactly* the same code as ptrSwap?
void swap(T)(ref T a, ref T b) {
    T temp = a;
    a = b;
    b = temp;
}

void ptrSwap(T)(T* a, T* b) {
    T temp = *a;
    *a = *b;
    *b = temp;
}

void main() {
    uint a, b;
    swap(a, b);
    ptrSwap(&a, &b);
    writeln(a); // Keep DMD from optimizing out ptrSwap entirely.
}


Here's the disassembly of the relevant portion:

  COMDEF __Dmain
        push    eax                                     ; 
        push    eax                                     ; 
        xor     eax, eax                                ;
        push    ebx                                     ; 
        lea     ecx, [esp+4H]                           ; 
        mov     dword ptr [esp+4H], eax                 ; 
        mov     dword ptr [esp+8H], eax                 ; 
        push    ecx                                     ; 
        lea     eax, [esp+0CH]                          ; 
        call    _D5test711__T4swapTkZ4swapFKkKkZv       ; 
        mov     edx, dword ptr [esp+4H]                 ; 
        mov     ebx, dword ptr [esp+8H]                 ; 
        mov     dword ptr [esp+4H], ebx                 ; 
        mov     eax, offset FLAT:_main                  ; 
        mov     dword ptr [esp+8H], edx                 ; 
        push    ebx                                     ;
        push    10                                      ;
        call    _D3std5stdio4File14__T5writeTkTaZ5writeMFkaZv;
        xor     eax, eax                                ; 
        pop     ebx                                     ; 
        add     esp, 8                                  ;
        ret                                             ; 
__Dmain ENDP

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list