[phobos] std.algorithm.sort slow as molasses

David Simcha dsimcha at gmail.com
Fri Jul 2 13:57:33 PDT 2010


I have gotten around to doing some disassembly reading, and apparently I was
wrong.  DMD does inline nested functions passed as templates, even when they
access the outer scope, at least in trivial cases.  D code:

import std.c.stdio;

void main(string[] args) {
    uint num = args.length;  // Make sure this isn't const folded.

    bool comp(uint otherNum) {
        return otherNum < num;
    }

    // Need to print to keep this from being optimized out entirely.
    printf("%d", evalPred!comp(300));
}

bool evalPred(alias pred)(uint num) {
    // This line is to make sure that this function doesn't get inlined into
    // main.  DMD doesn't inline functions that could throw.
    if(num == uint.max) {
        throw new Exception("");
    }

    return pred(num);
}


Disassembly of evalPred!comp:

_D4test4mainFAAyaZv45__T8evalPredS29_D4test4mainFAAyaZv4compMFkZbZ8evalPredMFkZb
PROC NEAR
;  COMDEF
_D4test4mainFAAyaZv45__T8evalPredS29_D4test4mainFAAyaZv4compMFkZbZ8evalPredMFkZb
        push    eax                                     ; 0000 _ 50
        cmp     dword ptr [esp+8H], -1                  ; 0001 _ 83. 7C 24,
08, FF
        jnz     ?_006                                   ; 0006 _ 75, 27
        mov     ecx, offset FLAT:_D9Exception7__ClassZ  ; 0008 _ B9,
00000000(segrel)
        push    ecx                                     ; 000D _ 51
        call    __d_newclass                            ; 000E _ E8,
00000000(rel)
        add     esp, 4                                  ; 0013 _ 83. C4, 04
        push    dword ptr [?_004]                       ; 0016 _ FF. 35,
0000000C(segrel)
        push    dword ptr [?_003]                       ; 001C _ FF. 35,
00000008(segrel)
        push    0                                       ; 0022 _ 6A, 00
        call
_D6object9Exception6__ctorMFAyaC6object9ThrowableZC9Exception; 0024 _ E8,
00000000(rel)
        push    eax                                     ; 0029 _ 50
        call    __d_throw at 4                             ; 002A _ E8,
00000000(rel)
?_006:  mov     eax, dword ptr [esp]                    ; 002F _ 8B. 04 24
        mov     edx, dword ptr [eax]                    ; 0032 _ 8B. 10
        mov     eax, 1                                  ; 0034 _ B8,
00000001
        cmp     edx, dword ptr [esp+8H]                 ; 0039 _ 3B. 54 24,
08
        ja      ?_007                                   ; 003D _ 77, 02
        xor     eax, eax                                ; 003F _ 31. C0
?_007:  pop     ecx                                     ; 0041 _ 59
        ret     4                                       ; 0042 _ C2, 0004
_D4test4mainFAAyaZv45__T8evalPredS29_D4test4mainFAAyaZv4compMFkZbZ8evalPredMFkZb
ENDP
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100702/90a1c56d/attachment.html>


More information about the phobos mailing list