When D feels unfinished: union initialization and NRVO

Mathias Lang pro.mathias.lang at gmail.com
Fri Mar 20 13:32:24 UTC 2020


On Thursday, 19 March 2020 at 17:57:46 UTC, kinke wrote:
>
> Yeah, I'd suggest not to look at how this is currently 
> implemented (Walter doesn't seem to see the advantage of a move 
> compiler intrinsic). With a proper optimizer, you shouldn't 
> have to care (see LDC result).
> Wrt. interior pointers, that's in the spec and could bite you 
> in different places where moving is implicit.
>
>> 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.
>
> I've been working on improving things in this regard, 
> especially with v1.19.

Thanks a lot for that. I started to add some NRVO tests to our 
CI, and had to disable them for DMD. We're developing on Mac OSX 
and I've been debating for a while whether we should drop DMD or 
not, because it's just too much effort for us to maintain 
compatibility for it.

The interior pointer thing I was aware of, but upon searching the 
spec, I can't find anything about it. There is a mention of 
moving garbage collection, but that should only be valid for 
object that belongs to the GC. And event if there was a moving 
garbage collector, I doubt we could use it with C++ object. "move 
construction dlang" and "interior pointer dlang" both yield a few 
interesting discussion, but nothing in the spec (No hit on "move" 
  in https://dlang.org/spec/struct.html).

And from what I remember, the "no interior pointer" rule was 
mostly there to prevent us from entering the same complexity as 
C++ when it comes to copy / move constructors. However that 
position is not really tenable if we want to properly interface 
with C++.

For reference, the tests I added are here 
(https://github.com/Geod24/agora/blob/e765b5b53453c928a7488ea616e90bcd1b93b5b2/source/agora/test/NRVO.d) and the actual serializer implementation is here (https://github.com/Geod24/agora/blob/e765b5b53453c928a7488ea616e90bcd1b93b5b2/source/agora/common/Serializer.d). At this point, I'm considering just writing a template that transforms a type to another one with a specific field wrapped with one of those NRVO-checking struct, so I can systematically detect any place where a move is performed instead of NRVO in the serializer code.


More information about the Digitalmars-d mailing list