Creeping Bloat in Phobos

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 28 04:46:11 PDT 2014


28-Sep-2014 00:57, Walter Bright пишет:
>  From time to time, I take a break from bugs and enhancements and just
> look at what some piece of code is actually doing. Sometimes, I'm
> appalled. Phobos, for example, should be a lean and mean fighting machine:
>
>
> http://www.nbcnews.com/id/38545625/ns/technology_and_science-science/t/king-tuts-chariots-were-formula-one-cars/#.VCceNmd0xjs
>
>
> Instead, we have something more akin to:
>
>
> http://untappedcities.com/2012/10/31/roulez-carrosses-carriages-of-versailles-arrive-in-arras/
>
>
> More specifically, I looked at std.file.copy():
>
>    https://github.com/D-Programming-Language/phobos/blob/master/std/file.d
>
> Which is 3 lines of code:
>
>    void copy(in char[] from, in char[] to) {
>          immutable result = CopyFileW(from.tempCStringW(),
> to.tempCStringW(), false);
>          if (!result)
>              throw new FileException(to.idup);
>    }
>
> Compiling this code for Windows produces the rather awful:

In all honesty - 2 RAII structs w/o inlining + setting up exception 
frame + creating and allocating an exception + idup-ing a string does 
account to about this much.


>
> _D3std4file4copyFxAaxAaZv       comdat
>          assume  CS:_D3std4file4copyFxAaxAaZv
> L0:             push    EBP
>                  mov     EBP,ESP
>                  mov     EDX,FS:__except_list
>                  push    0FFFFFFFFh
>                  lea     EAX,-0220h[EBP]
>                  push    offset _D3std4file4copyFxAaxAaZv[0106h]
>                  push    EDX
>                  mov     FS:__except_list,ESP
>                  sub     ESP,8
>                  sub     ESP,041Ch
>                  push    0
>                  push    dword ptr 0Ch[EBP]
>                  push    dword ptr 8[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCSÇàÆTuTaZÇìÆFNbNixAaZSÇ┬├3Res
>                  mov     dword ptr -4[EBP],0
>                  lea     EAX,-0220h[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCStringTuTaZ11tempCStringFNbNixAaZ3Res3ptrMxFNaNbNdNiNfZPxu
>
>                  push    EAX
>                  lea     EAX,-0430h[EBP]
>                  push    dword ptr 014h[EBP]
>                  push    dword ptr 010h[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCSÇàÆTuTaZÇìÆFNbNixAaZSÇ┬├3Res
>                  mov     dword ptr -4[EBP],1
>                  lea     EAX,-0430h[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCStringTuTaZ11tempCStringFNbNixAaZ3Res3ptrMxFNaNbNdNiNfZPxu
>
>                  push    EAX
>                  call    dword ptr __imp__CopyFileW at 12
>                  mov     -01Ch[EBP],EAX
>                  mov     dword ptr -4[EBP],0
>                  call    near ptr L83
>                  jmp short       L8F
> L83:            lea     EAX,-0220h[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCStringTuTaZ11tempCStringFNbNixAaZ3Res6__dtorMFNbNiZv
>
>                  ret
> L8F:            mov     dword ptr -4[EBP],0FFFFFFFFh
>                  call    near ptr L9D
>                  jmp short       LA9
> L9D:            lea     EAX,-0430h[EBP]
>                  call    near ptr
> _D3std8internal7cstring21__T11tempCStringTuTaZ11tempCStringFNbNixAaZ3Res6__dtorMFNbNiZv
>
>                  ret
> LA9:            cmp     dword ptr -01Ch[EBP],0
>                  jne     LF3
>                  mov     ECX,offset
> FLAT:_D3std4file13FileException7__ClassZ
>                  push    ECX
>                  call    near ptr __d_newclass
>                  add     ESP,4
>                  push    dword ptr 0Ch[EBP]
>                  mov     -018h[EBP],EAX
>                  push    dword ptr 8[EBP]
>                  call    near ptr
> _D6object12__T4idupTxaZ4idupFNaNbNdNfAxaZAya
>                  push    EDX
>                  push    EAX
>                  call    dword ptr __imp__GetLastError at 0
>                  push    EAX
>                  push    dword ptr _D3std4file13FileException6__vtblZ[02Ch]
>                  push    dword ptr _D3std4file13FileException6__vtblZ[028h]
>                  push    095Dh
>                  mov     EAX,-018h[EBP]
>                  call    near ptr
> _D3std4file13FileException6__ctorMFNfxAakAyakZC3std4file13FileException
>                  push    EAX
>                  call    near ptr __d_throwc
> LF3:            mov     ECX,-0Ch[EBP]
>                  mov     FS:__except_list,ECX
>                  mov     ESP,EBP
>                  pop     EBP
>                  ret     010h
>                  mov     EAX,offset
> FLAT:_D3std4file13FileException6__vtblZ[0310h]
>                  jmp     near ptr __d_framehandler
>
> which is TWICE as much generated code as for D1's copy(), which does the
> same thing. No, it is not because D2's compiler sux. It's because it has
> become encrustified with gee-gaws, jewels, decorations, and other crap.
>
> To scrape the barnacles off, I've filed:
>
> https://issues.dlang.org/show_bug.cgi?id=13541
> https://issues.dlang.org/show_bug.cgi?id=13542
> https://issues.dlang.org/show_bug.cgi?id=13543
> https://issues.dlang.org/show_bug.cgi?id=13544
>
> I'm sure there's much more in std.file (and elsewhere) that can be done.
> Guys, when developing Phobos/Druntime code, please look at the assembler
> once in a while and see what is being wrought. You may be appalled, too.
>
>
>


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list