www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5777] New: Move semantics require full spec NRVO

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777

           Summary: Move semantics require full spec NRVO
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hara.pg gmail.com



DMD supports Named Return Value Optimization(NRVO), but now this 
doesn't work for struct that has  disabled postblit and user-defined 
destructor (e.g. std.typecons.scoped).

This looks trivial, but serious problem.

1. Need of incorporating NRVO into the language specifications
  D does not contain rvalue seantinc in its type system. 
  ('ref' is storage class, not part of type. This means casting 
  is useless for this purpose, unlike C++.)
  Except for only one, This means D has no way that convert from lvalue 
  to rvalue in current language spec.

  The one way is NRVO. This is not only trivial optimization, but also 
  core of move semantics in D.
  ----
  T f(){
    T ret;        // &ret == hidden pointer, and *hidden is 'move target'.
                // In this example, share memory of t.
    ret = ...
    return ret;    // NRVO.
  }

  T t = f();    // t's memory address is taken hidden pointer
  ---- 

  Moving object caller to callee is now D supports, but reversed flow is 
  limitation. To full support moving callee to caller through return value, 
  NRVO is required.

2. std.algorithm.move problem
  If Issue 4499 will be fixed, unary std.algorithm.move() will not work 
  on noncopyable/has expplicit destructor object.

  To give correct behavior, NRVO spec update is need.

3. ABI problem
  In http://digitalmars.com/d/2.0/abi.html 'Return Value' term,
  struct its size is less than 8 byte is returned using registers.

  Issue 4500 is scoped!T and self-referenced class problem, but the way 
  resolving it will need true NRVO. That has following spec: Any size object 
  returned through hidden pointer,
  if the object has elaborate destructor at least.

  See http://d.puremagic.com/issues/show_bug.cgi?id=4500#c5 . Attachment code 
  includes workaround for this issue, But it's too ugly.

Do you think?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777




Related issues:

On return statement, copy construction should work correctly.
- Issue 4437 - copy construction bug with "return this;"
- Issue 4499 - calls to  disabled postblit are emitted

Struct literal/constructor call should be rvalue.
- Issue 5178 - StructLiteral should not be lvalue
- Issue 5769 - struct elaborate constructor should make rvalue

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777




Created an attachment (id=935)
3 Patches

These patches has 3 points.

1. Allow NRVO for user-defined destructor object.
2. Eliminate destructor call of return value that non-copyable object in NRVO
function.
3. On assignment (e1 = e2) if e2 is non-copyable and prvalue, this expression
generates following code:
   e1.__dtor();
   e1 = e2;  // bit copy

This is much experimental, but I hope that it helps the language evolution.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777




On trunk dmd, this code cannot compile because of fixing bug4499.
----
import std.algorithm;

struct S
{
   disable this(this){}
  ~this(){}
}

void main()
{
  S s1;
  S s2 = move(s1);
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------

           obsolete|                            |



Created an attachment (id=963)
2 patches

Update patches.

Changes:


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 05 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5777


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



12:58:17 PDT ---
https://github.com/D-Programming-Language/dmd/commit/c3491ad84c24d353555cf2c913ede7ae46440ce0

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