Re: Question: deep - shallow - move semantics
Peter Gottschling <[email protected]> Thu, 21 Jul 2005 10:15:27 -0500
| Newsgroups | gmane.comp.lib.mtl.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi, To avoid misunderstandings I start with repeating what you certainly know. Deep copy has no side effects but is expensive. Shallow copy is fast but isn't really a copy to be honest. It is more an aliasing and if you change the copy the original will be also changed and this is not always what you want. Most swap implementations of data structures pointing to some constructs in memory are done with shallow copy but there it is okay since the two constructs in memory are referred by one object each. Swapping only the pointers does not establish any aliasing. The idea behind move semantics is that an object that is computed in a function or by an operator or by any expression only exist once. Assigning this to a variable in C++ is done by a deep copy and the temporary object in the function/operator/expression is thrown away. Since this temporary object isn't used by anybody else (after deletion :-D) it can be copied shallowly in the assignment. The same can be done for intermediate results, e.g., summing three vectors (or matrices) a+b+c the temporary vector containing the result of a+b can be shallowly copied to the 1st operand of the second addition. Assignments from one variable to another are copied deeply to avoid the side effects. David Abrahams proposed this to the C++ standard committee and hopefully C++ compiler will be able to do it one day. The MTL should have this mechanism earlier. Unfortunately, the current implementation of MTL does not provide move semantics. Normal assignments of vectors and matrices are shallow copies. Deep copies are provided in the 'copy' command. I would in your position starting by implementing everything with the copy command (instead of assignments) and later replace some deep copies with shallow ones by being very careful which aliasing can cause trouble. Good luck! Peter On Jul 21, 2005, at 3:21 AM, 杨 铭 wrote: > Hi all, > > I'm a newbie (both of C++ and Matrix Computation). > > I'm currently working on a small project which contains some matrix > manipulation. I decide to write a matrix class. I have read some code > of MTL, and it's a great work, I think. > What bothers me is how to avoid deep copy. Now MTL uses a reference > counting stratage, as I know. But the plan of future version say that > deep copy semantics > will be adopt ( see > http://boost-consulting.com/projects/mtl4/libs/numeric/mtl/doc/html/ > plan.html ) > > After implementing a simple reference counting based matrix class, I > realized that shallow copy semantics is quite confusing but alleviates > the efficiency problem. > > My question is how to ensure high performance in the forthcoming MTL > 4? (Sorry, I found section 1.7.2 of the plan is hard to understand, > especially > "move semantics"). > > And would you like to give me some advise about my matrix class? > Should > I use shallow copy semantics or a deep one? > > thank you! > > _________________________________________________________________ > 享用世界上最大的电子邮件系统— MSN Hotmail。 http://www.hotmail.com > _______________________________________________ > This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/ ------------ Peter Gottschling Research Associate Open Systems Laboratory Indiana University 301i Lindley Hall Bloomington, IN 47405 Tel.: +1 812 855-8898 Fax: +1 812 856 0853 http://www.osl.iu.edu/~pgottsch _______________________________________________ This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/