www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5254] New: Low performance code with struct constructor

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

           Summary: Low performance code with struct constructor
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: performance
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-11-21 18:50:20 PST ---
This D2 test program contains a Foo struct that is a minimized version of a
common 3D vector struct. 

A static if switches between a normal constructor and no constructor with
default initialization of the tree coordiates.


enum bool use_ctor = true;
struct Foo {
    static if (use_ctor) {
        double x, y, z;

        pure nothrow this(double x_, double y_, double z_) {
            this.x = x_;
            this.y = y_;
            this.z = z_;
        }
    } else {
        double x=0.0, y=0.0, z=0.0;
    }

    Foo muls(double s) {
        return Foo(this.x*s, this.y*s, this.z*s);
    }
}
void main() {}



I have compiled the two versions using DMD 2.050 with dmd -O -release -inline

The resulting asm shows a significant difference. The full program that uses
the 3D vectors too shows a different performance in the cases:

-----------------------

use_ctor=true:

_D4test3Foo6__ctorMFNaNbNcdddZS4test3Foo    comdat
    assume  CS:_D4test3Foo6__ctorMFNaNbNcdddZS4test3Foo
        mov ECX,EAX
        fld qword ptr 014h[ESP]
        fld qword ptr 0Ch[ESP]
        fld qword ptr 4[ESP]
        fxch    ST2
        fstp    qword ptr [ECX]
        fstp    qword ptr 8[ECX]
        fstp    qword ptr 010h[ECX]
        ret 018h
_D4test3Foo6__ctorMFNaNbNcdddZS4test3Foo    ends
_D4test3Foo4mulsMFdZS4test3Foo  comdat
    assume  CS:_D4test3Foo4mulsMFdZS4test3Foo
        sub ESP,028h
        mov ECX,EAX
        push    ESI
        mov ESI,offset FLAT:_D4test3Foo6__initZ
        push    EDI
        lea EDI,8[ESP]
        movsd
        movsd
        movsd
        movsd
        movsd
        movsd
        fld qword ptr 8[ECX]
        fld qword ptr 010h[ECX]
        fxch    ST1
        fmul    qword ptr 038h[ESP]
        lea ESI,8[ESP]
        mov EDI,034h[ESP]
        fxch    ST1
        fmul    qword ptr 038h[ESP]
        fld qword ptr [ECX]
        fmul    qword ptr 038h[ESP]
        fxch    ST2
        fstp    qword ptr 020h[ESP]
        fxch    ST1
        fld qword ptr 020h[ESP]
        fxch    ST2
        fstp    qword ptr 028h[ESP]
        fxch    ST1
        fld qword ptr 028h[ESP]
        fxch    ST2
        fstp    qword ptr 8[ESP]
        fstp    qword ptr 010h[ESP]
        fstp    qword ptr 018h[ESP]
        movsd
        movsd
        movsd
        movsd
        movsd
        movsd
        mov EAX,034h[ESP]
        pop EDI
        pop ESI
        add ESP,028h
        ret 0Ch
_D4test3Foo4mulsMFdZS4test3Foo  ends

-----------------------

use_ctor=false:

_D4test3Foo4mulsMFdZS4test3Foo  comdat
    assume  CS:_D4test3Foo4mulsMFdZS4test3Foo
        mov ECX,EAX
        mov EDX,4[ESP]
        mov EAX,EDX
        fld qword ptr [ECX]
        fmul    qword ptr 8[ESP]
        fstp    qword ptr [EDX]
        fld qword ptr 8[ECX]
        fmul    qword ptr 8[ESP]
        fstp    qword ptr 8[EDX]
        fld qword ptr 010h[ECX]
        fmul    qword ptr 8[ESP]
        fstp    qword ptr 010h[EDX]
        ret 0Ch
_D4test3Foo4mulsMFdZS4test3Foo  ends

-----------------------

Using LDC1:

_D7path13e2V34mulsMFdZS7path13e2V3:
    movsd   8(%esp), %xmm0
    movapd  %xmm0, %xmm1
    mulsd   16(%eax), %xmm1
    movapd  %xmm0, %xmm2
    mulsd   8(%eax), %xmm2
    mulsd   (%eax), %xmm0
    movl    4(%esp), %eax
    movsd   %xmm0, (%eax)
    movsd   %xmm2, 8(%eax)
    movsd   %xmm1, 16(%eax)
    ret $12

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