digitalmars.D - Is NRVO part of the spec?
- Peter Alexander (9/9) Feb 07 2015 I'm writing a blog post about why we don't need rvalue references
- Daniel Murphy (4/9) Feb 07 2015 NRVO isn't required for correct semantics, as structs can be moved with
- Andrei Alexandrescu (2/12) Feb 07 2015 It is required for structs that disable postblit. -- Andrei
- Peter Alexander (28/47) Feb 07 2015 NRVO specifically means that a pointer to the destination object
- Daniel Murphy (3/6) Feb 07 2015 IIRC they only require that no copies are made. They can still be moved...
- Andrei Alexandrescu (28/35) Feb 07 2015 Exactly - as you just said in the other post, the spec must clarify when...
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (10/50) Feb 07 2015 I think this needs to be phrased differently: Any returned value
- deadalnix (3/43) Feb 07 2015 Perfect. Clear and reasonable.
- Vlad Levenfeld (3/3) Feb 07 2015 auto var = (rvalue);
- Peter Alexander (13/15) Feb 07 2015 Yes, you're right. I suppose what I mean is that it should be
- Daniel Murphy (4/14) Feb 07 2015 Yeah, the spec needs to guarantee no copies. Kenji Hara has done some w...
I'm writing a blog post about why we don't need rvalue references in D. It seems that we rely on NRVO being performed, not just as an optimization, but for correct semantics (e.g. for objects without destructors or postblits). This doesn't appear to be documented anywhere. Is it meant to be part of the spec? Relevant issues: See: https://issues.dlang.org/show_bug.cgi?id=10372 https://issues.dlang.org/show_bug.cgi?id=12180
Feb 07 2015
"Peter Alexander" wrote in message news:uiqnamficseklfowmkyf forum.dlang.org...I'm writing a blog post about why we don't need rvalue references in D. It seems that we rely on NRVO being performed, not just as an optimization, but for correct semantics (e.g. for objects without destructors or postblits). This doesn't appear to be documented anywhere. Is it meant to be part of the spec?NRVO isn't required for correct semantics, as structs can be moved with bitcopy.
Feb 07 2015
On 2/7/15 6:35 AM, Daniel Murphy wrote:"Peter Alexander" wrote in message news:uiqnamficseklfowmkyf forum.dlang.org...It is required for structs that disable postblit. -- AndreiI'm writing a blog post about why we don't need rvalue references in D. It seems that we rely on NRVO being performed, not just as an optimization, but for correct semantics (e.g. for objects without destructors or postblits). This doesn't appear to be documented anywhere. Is it meant to be part of the spec?NRVO isn't required for correct semantics, as structs can be moved with bitcopy.
Feb 07 2015
On Saturday, 7 February 2015 at 15:02:43 UTC, Andrei Alexandrescu wrote:On 2/7/15 6:35 AM, Daniel Murphy wrote:NRVO specifically means that a pointer to the destination object is passed to the function, and the returned object is constructed in place. The in place construction isn't required. What is required is that the local is moved. e.g. S foo() { S s; return s; } S s = foo(); With NRVO becomes: void foo(ref S dst) { dst = S(); } S s = void; foo(s); But this isn't necessary. Would also be valid to just do: void foo(ref S dst) { S s; move(dst, s); // do the memcpys } S s; foo(s); This distinction matters because NRVO cannot be performed when foo may return two different objects, but we can still move and avoid postblit."Peter Alexander" wrote in message news:uiqnamficseklfowmkyf forum.dlang.org...It is required for structs that disable postblit. -- AndreiI'm writing a blog post about why we don't need rvalue references in D. It seems that we rely on NRVO being performed, not just as an optimization, but for correct semantics (e.g. for objects without destructors or postblits). This doesn't appear to be documented anywhere. Is it meant to be part of the spec?NRVO isn't required for correct semantics, as structs can be moved with bitcopy.
Feb 07 2015
"Andrei Alexandrescu" wrote in message news:mb59ej$2j7s$1 digitalmars.com...IIRC they only require that no copies are made. They can still be moved.NRVO isn't required for correct semantics, as structs can be moved with bitcopy.It is required for structs that disable postblit. -- Andrei
Feb 07 2015
On 2/7/15 8:02 AM, Daniel Murphy wrote:"Andrei Alexandrescu" wrote in message news:mb59ej$2j7s$1 digitalmars.com...Exactly - as you just said in the other post, the spec must clarify when things are guaranteed to be moved and not copied. That includes: 1. URVO: returning an rvalue does not entail a copy. 2. Last return of a function local variable does not entail a copy. That's actually more than NRVO because NRVO requires the same local be returned from all paths. Example: T fun(bool b) { if (b) { T a; return a; } T b; return b; } This should work if T is noncopyable. It may be less efficient than it could though. 3. The more complicated/ambitious cases involve the last use of a value. Consider: T fun() { T a; T b = a; return b; } Even though the code ostensibly makes a copy, it's the last use of a so that could be a move. I think (3) could be used for optimization but it's too much of a headache to put in the language definition. We do need to have (1) and (2) covered. AndreiIIRC they only require that no copies are made. They can still be moved.NRVO isn't required for correct semantics, as structs can be moved with bitcopy.It is required for structs that disable postblit. -- Andrei
Feb 07 2015
On Saturday, 7 February 2015 at 16:10:48 UTC, Andrei Alexandrescu wrote:On 2/7/15 8:02 AM, Daniel Murphy wrote:I think this needs to be phrased differently: Any returned value can be moved iff none of the destructors, scope(exit)'s and finallys that are run as part of the cleanup can possibly access the value's original location. (I assume that the move happens _before_ the destructors are called. This is reasonable because otherwise said destructors could modify the returned value "in flight".) This formulation also encompasses rvalues."Andrei Alexandrescu" wrote in message news:mb59ej$2j7s$1 digitalmars.com...Exactly - as you just said in the other post, the spec must clarify when things are guaranteed to be moved and not copied. That includes: 1. URVO: returning an rvalue does not entail a copy. 2. Last return of a function local variable does not entail a copy.IIRC they only require that no copies are made. They can still be moved.NRVO isn't required for correct semantics, as structs can be moved with bitcopy.It is required for structs that disable postblit. -- AndreiThat's actually more than NRVO because NRVO requires the same local be returned from all paths. Example: T fun(bool b) { if (b) { T a; return a; } T b; return b; } This should work if T is noncopyable. It may be less efficient than it could though. 3. The more complicated/ambitious cases involve the last use of a value. Consider: T fun() { T a; T b = a; return b; } Even though the code ostensibly makes a copy, it's the last use of a so that could be a move.This is arguably different, because it doesn't involve a return.I think (3) could be used for optimization but it's too much of a headache to put in the language definition. We do need to have (1) and (2) covered. Andrei
Feb 07 2015
On Saturday, 7 February 2015 at 16:10:48 UTC, Andrei Alexandrescu wrote:On 2/7/15 8:02 AM, Daniel Murphy wrote:Perfect. Clear and reasonable."Andrei Alexandrescu" wrote in message news:mb59ej$2j7s$1 digitalmars.com...Exactly - as you just said in the other post, the spec must clarify when things are guaranteed to be moved and not copied. That includes: 1. URVO: returning an rvalue does not entail a copy. 2. Last return of a function local variable does not entail a copy. That's actually more than NRVO because NRVO requires the same local be returned from all paths. Example: T fun(bool b) { if (b) { T a; return a; } T b; return b; } This should work if T is noncopyable. It may be less efficient than it could though. 3. The more complicated/ambitious cases involve the last use of a value. Consider: T fun() { T a; T b = a; return b; } Even though the code ostensibly makes a copy, it's the last use of a so that could be a move. I think (3) could be used for optimization but it's too much of a headache to put in the language definition. We do need to have (1) and (2) covered. AndreiIIRC they only require that no copies are made. They can still be moved.NRVO isn't required for correct semantics, as structs can be moved with bitcopy.It is required for structs that disable postblit. -- Andrei
Feb 07 2015
auto var = (rvalue); fails to compile if typeof(rvalue) has disabled the postblit. Why isn't a move issued in this instance?
Feb 07 2015
On Saturday, 7 February 2015 at 14:46:55 UTC, Daniel Murphy wrote:NRVO isn't required for correct semantics, as structs can be moved with bitcopy.Yes, you're right. I suppose what I mean is that it should be guaranteed that returning a local Lvalue by value should always be moved to the caller destination, rather than copied then destroyed. S foo() { S s; return s; } S s = foo(); // no destructors or postblits should be called here The spec needs to guarantee this, otherwise unary std.algorithm.move isn't guaranteed to work for non-copyable types.
Feb 07 2015
"Peter Alexander" wrote in message news:gverkczeotvadwmdowdl forum.dlang.org...Yes, you're right. I suppose what I mean is that it should be guaranteed that returning a local Lvalue by value should always be moved to the caller destination, rather than copied then destroyed. S foo() { S s; return s; } S s = foo(); // no destructors or postblits should be called here The spec needs to guarantee this, otherwise unary std.algorithm.move isn't guaranteed to work for non-copyable types.Yeah, the spec needs to guarantee no copies. Kenji Hara has done some work on this.
Feb 07 2015