Jesse,
>I don't know that much about this stuff, but my understanding is that rvalue refereces and move semantics allow a library to overload the rvalue assignment to do a this kind of pointer swapping.  But this stuff is probably very difficult to do.

That's the trick data().swap(..) does, it takes the passed by value data and swaps its pointer with the object calling it <--I can discuss a bit more about that but it may get too technical (i.e. std::swap(..,..) vs container.swap(...) ). Also the compiler should be (in my case is) responsible to take care of the value passed using copy elision. If it is an rvalue it will pass it in without copying; or copy it if it is not - which is what we want.

I did some tests and if the you are interested I can prepare to post them. The memory in those tests certainly shows half memory usage with move semantics for a large vector. Tracing back gives no indication of the existence of two containers and the performance is increased by about 20-30%. Also there is no indication of shallow copy (left and right side containers remain two distinct objects with distinct storage).

Very Best
Nasos Iliopoulos
 



From: jesseperla@gmail.com
Date: Thu, 10 Sep 2009 17:09:38 -0400
To: ublas@lists.boost.org
Subject: Re: [ublas] Move Semantics


On Thu, Sep 10, 2009 at 10:04 AM, Nasos Iliopoulos <nasos_i@hotmail.com> wrote:
Wouldn't changing vectors' operator = allow that?
From:

BOOST_UBLAS_INLINE

vector &operator = (const vector &v) {

data () = v.data ();

return *this;

}


to

// pass by value for move semantics

BOOST_UBLAS_INLINE

vector &operator = (vector v) {

data().swap(v.data());

return *this;
}

I believe the above is called shallow copy and changes the current semantics of ublas (see http://www.osl.iu.edu/research/mtl/mtl4/doc/shallow_copy_problems.html) .  For example:
matrix<double> A(2,2);
//..... set A....

matrix<double> B = A;

Now they point at the same thing, not a copy/move.


Also, what about rvalues.  e.g.:

matrix<double> A = 2 * B;


And last, since this is using expression templates, the "data" vector may not exist, and will frequently not have the same type.


I don't know that much about this stuff, but my understanding is that rvalue refereces and move semantics allow a library to overload the rvalue assignment to do a this kind of pointer swapping.  But this stuff is probably very difficult to do.

-Jesse



Windows Live: Make it easier for your friends to see what you’re up to on Facebook. Find out more.