[Issue 11394] NRVO should work for object field initialization in constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 8 00:57:26 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11394


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #4 from bearophile_hugs at eml.cc 2013-11-08 00:57:24 PST ---
(In reply to comment #3)
> Commit pushed to master at https://github.com/D-Programming-Language/dmd
> 
> https://github.com/D-Programming-Language/dmd/commit/6f80983044a55580e81e7f723e5b71f05650caa4
> fix Issue 11394 - NRVO should work for object field initialization in
> constructor


The asm of two constructors isn't equal, but the situation has improved and now
there is no call to the array copy:


import std.traits: Unqual;
struct Foo {
    int[3] arr;
    this(int x) pure nothrow {
        arr[0] = x;
        arr[1] = x + 1;
        arr[2] = x + 2;
    }
}
struct Bar {
    immutable int[3] arr;

    this(int x) pure nothrow {
        static Unqual!(typeof(arr)) make(in int x)
        pure nothrow {
            typeof(return) a;
            a[0] = x;
            a[1] = x + 1;
            a[2] = x + 2;
            return a;
        }

        this.arr = make(x); // NRVO works?
    }
}
void main() {
    auto f = Foo(5);
    auto b = Bar(5);
}


Foo.__ctor:
    push EBX
    mov EDX, EAX
    mov EBX, 8[ESP]
    push ESI
    lea ECX, 1[EBX]
    lea ESI, 2[EBX]
    mov [EDX], EBX
    mov 4[EDX], ECX
    mov 8[EDX], ESI
    pop ESI
    pop EBX
    ret 4

Bar.__ctor:
    push EBX
    mov EBX, 8[ESP]
    xor ECX, ECX
    push ESI
    mov EDX, EAX
    lea ESI, 1[EBX]
    push EDI
    lea EDI, 2[EBX]
    mov [EAX], ECX
    mov 4[EAX], ECX
    mov 8[EAX], ECX
    mov [EDX], EBX
    mov 4[EDX], ESI
    mov 8[EDX], EDI
    pop EDI
    pop ESI
    pop EBX
    ret 4

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


More information about the Digitalmars-d-bugs mailing list