|
Ublas : |
Subject: [ublas] [patch] Move Semantics for uBlas
From: Nasos Iliopoulos (nasos_i_at_[hidden])
Date: 2009-09-14 22:59:35
Dear all,
In response to the discussion on move semantics in this list I am sending this patch, that implements move semantics in uBlas as currently supported by most compilers, along with a test program. As you will find out the additions are minimal.
An immediate effect of this patch is the elimination of the need for noalias in types vector<T> and matrix<T>, when assigned to the same type. Although this patch implements move semantics for bounded_ and c_ (vector and matrix) types, they don't have any effect at those types, due to their underlying storage. I included it for possible future exploitation.
In the test example (main.cpp) two tests are defined, one for vectors and one for matrices. The aim of this example is to print the pointers of the storage of each of the containers, before and after the assignment to a temporary object. When move semantics are enabled, the vector<T> and matrix<T> storage is moved from the temporary and no copy is performed.
If move semantics are supported by your compiler you will get an output like the following:
matrix<double> --------------------------------------------------------------------
Temporary pointer r: 0x94790c0
Pointer (must be equal to temp. pointer if move semantics are enabled) : 0x94790c0
Notes:
- It should be no surprise to see matrices and vectors been passed by VALUE, the compiler takes care and either moves (if the underlying code does not modify the object), or copies (if the underlying code modifies the object).
- There might be some space for some improvements (like clearing the data, before swaping)
- Move semantics don't eliminate temporaries. They rather move their storage around so no copies are performed.
- MSVC does no implement Named Return Value Optimization in debug mode. So if you build in debug with this compiler you might get different behaviour than a release build. Also see:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483229
- Enabling move semantics is done via #define BOOST_UBLAS_MOVE_SEMANTICS.
- All unit tests run successfully.
- There is plenty of room for optimizations when c++0x standard is out, taking advantage of rvalue references. (I have a sweet vector implementation using that).
- If you enable move semantics and your compiler does not support them, the operation will just be as passing by const reference.
Your valuable feedback would be much appreciated.
Interesting links.
[1] http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
[2] http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx
[3] http://cpp-next.com/archive/2009/09/move-it-with-rvalue-references/
Best
Nasos Iliopoulos
_________________________________________________________________
Ready for Fall shows? Use Bing to find helpful ratings and reviews on digital tv's.
http://www.bing.com/shopping/search?q=digital+tv's&form=MSHNCB&publ=WLHMTAG&crea=TEXT_MSHNCB_Vertical_Shopping_DigitalTVs_1x1