David Simcha <> changed:

           What    |Removed                     |Added
                 CC|                            |

--- Comment #2 from David Simcha <>  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

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:
------- You are receiving this mail because: -------

Reply via email to