When D feels unfinished: union initialization and NRVO
Mathias Lang
pro.mathias.lang at gmail.com
Thu Mar 19 17:38:42 UTC 2020
On Thursday, 19 March 2020 at 13:34:11 UTC, kinke wrote:
>
> Another simple workaround:
>
> import core.lifetime : move;
>
> ...
> case Good.Baguette:
> Foo f = { type_: type, f1: typeof(Foo.f1)("Hello World") };
> return move(f);
> ...
From my tests, it seems to do NRVO on LDC, so I suppose you
recognize it ?
On DMD, it just does a blit, which can still be expensive and
breaks interior pointers.
Another major advantage, which I also realized recently, is that
having NRVO all the way means that the stack usage is bounded.
Beside the obvious embedded code, it matters a lot when using
Fibers (we have a Vibe.d style handling of connection, with 1
connection == 1 fiber).
I also found out that one of the earlier case I mentioned
(https://gist.github.com/Geod24/61ef0d8c57c3916cd3dd7611eac8234e#file-nrvo_struct_ctor-d) only does NRVO on LDC. DMD (and GDC 9.3.0) just silently move it.
More information about the Digitalmars-d
mailing list