D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5254 - Low performance code with struct constructor
Summary: Low performance code with struct constructor
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: performance, SIMD
Depends on:
Blocks:
 
Reported: 2010-11-21 18:50 UTC by bearophile_hugs
Modified: 2024-12-13 17:54 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-11-21 18:50:20 UTC
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
Comment 1 dlangBugzillaToGithub 2024-12-13 17:54:12 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18317

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB