[phobos] std.algorithm.sort slow as molasses

David Simcha dsimcha at gmail.com
Sat Jul 3 07:13:34 PDT 2010


On 7/2/2010 6:10 PM, Jonathan M Davis wrote:
>
> DMD doesn't inline functions that could throw? Isn't that rather restrictive?
> I'm assuming that that's functions with an actual throw statement rather than
> all functions that aren't nothrow, since that would _really_ be restrictive. But
> still, you could have a really small function that threw if a condition failed
> but otherwise just returned a value, and I would have thought that that would be
> worth inlining. I'm far from an expert once you're dealing with stuff that low-
> level, so there could be a very good reason that functions that could throw
> aren't inlined, but it does strike me as rather restrictive if you can't inline
> such functions.
>
> - Jonathan M Davis
>    

Yeah, I've started looking into this a little more.  DMD will not inline 
anything with a throw statement, though it will inline functions that 
are not nothrow.  However, in practice calling enforce() seems to 
prevent inlining of any function that uses enforce() because enforce() 
also has a lazy parameter, which isn't always visible because it has a 
default.  For example:

import std.contracts : enforce;

void main(string[] args) {
     getNum();
}

__gshared bool dummy = true;

uint getNum() {
     enforce(dummy);
     return 1;
}


The fact that getNum() isn't inlined seems ridiculous at first, until 
you see the disassembly of it.  enforce() is inlined because it doesn't 
contain a throw statement.  std.contracts.bailOut() actually throws the 
exception.  Here's the horribly complicated disassembly of getNum():

_D5test96getNumFZk PROC NEAR
;  COMDEF _D5test96getNumFZk
         sub     esp, 24                                 ; 0000 _ 83. EC, 18
         xor     eax, eax                                ; 0003 _ 31. C0
         mov     ecx, offset 
FLAT:_D5test96getNumFZk14__dgliteral610MFZAxa; 0005 _ B9, 00000000(segrel)
         push    ebx                                     ; 000A _ 53
         mov     dl, byte ptr [_D5test95dummyb]          ; 000B _ 8A. 
15, 00000000(segrel)
         xor     dl, 01H                                 ; 0011 _ 80. F2, 01
         mov     dword ptr [esp+4H], eax                 ; 0014 _ 89. 44 
24, 04
         mov     dword ptr [esp+8H], ecx                 ; 0018 _ 89. 4C 
24, 08
         jz      ?_007                                   ; 001C _ 74, 21
         mov     edx, ecx                                ; 001E _ 8B. D1
         push    dword ptr [?_003]                       ; 0020 _ FF. 
35, 00000014(segrel)
         push    dword ptr [?_002]                       ; 0026 _ FF. 
35, 00000010(segrel)
         push    10                                      ; 002C _ 6A, 0A
         mov     eax, dword ptr [esp+10H]                ; 002E _ 8B. 44 
24, 10
         mov     ebx, dword ptr [esp+10H]                ; 0032 _ 8B. 5C 
24, 10
         call    edx                                     ; 0036 _ FF. D2
         push    edx                                     ; 0038 _ 52
         push    eax                                     ; 0039 _ 50
         call    _D3std9contracts7bailOutFAyaixAaZv      ; 003A _ E8, 
00000000(rel)
?_007:  mov     eax, 1                                  ; 003F _ B8, 
00000001
         pop     ebx                                     ; 0044 _ 5B
         add     esp, 24                                 ; 0045 _ 83. C4, 18
         ret                                             ; 0048 _ C3
_D5test96getNumFZk ENDP



More information about the phobos mailing list