|
Prev: std::deque typically faster then std::list for push_back(), front(), pop_front()?
Next: template function ambiguity
From: Jeffrey Baker on 12 Jan 2008 18:50 I was reading Post subject "class returning member function", Dated -12/ 27-28 /07 and found it not to be of value. Return Value Optimization RVO seems to do no better than a bitwise assignment. Except the bitwise is more efficient if that is what RVO is to accomplish. What is the value or need of RVO? Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Juha Nieminen on 13 Jan 2008 04:01 Jeffrey Baker wrote: > I was reading Post subject "class returning member function", Dated -12/ > 27-28 /07 and found it not to be of value. > Return Value Optimization RVO seems to do no better than a bitwise > assignment. Except the bitwise is more efficient if that is what RVO is to > accomplish. What is the value or need of RVO? Without return value optimization the function would have to construct a temporary object (which will be used as the return value) and then the caller of that function would have to make a copy constructor call in order to copy the contents of that temporary object to the destination object. With return value optimization the return value is constructed directly onto the destination object. No temporary object is created nor the copy constructor is called. This has obvious advantages if making a copy constructor call is a heavy operation (eg. if the object is an enormous deep-copying array), plus it saves memory (during the copy constructor call there has to be two objects taking that much memory). (Note, however, that you can't *trust* that RVO is always used. There are certain situations where the compiler cannot do it and the temporary object is created and the copy constructor called.) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Carl Barron on 13 Jan 2008 04:12 In article <13ojbelkf9rsd1d(a)corp.supernews.com>, Jeffrey Baker <tbirdjb2(a)earthlink.net> wrote: > I was reading Post subject "class returning member function", Dated -12/ > 27-28 /07 and found it not to be of value. > Return Value Optimization RVO seems to do no better than a bitwise > assignment. Except the bitwise is more efficient if that is what RVO is to > accomplish. What is the value or need of RVO? > RVO uses the fact the compiler knows where it will store a tempory returned from the function and the function always returns a copy of the same instance of the class returned, so it creates that object where it would be copied to, and avoids one copy and destruct. This is not bitwise copy. There is no requirement that it be performed, even if it is possible to do so. RVO eliminates a copy std::string foo() { std::string a('a',1000000); return a; } void test() { std::string b = foo(); // use b } RVO here eliminates a copy of at least 1000000 chars, surely different than just copying internal pointers to the heap and then deleting the ptrs passing the ptrs to deallocated memory [ub] back to the caller of the function. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 13 Jan 2008 04:11 Jeffrey Baker wrote: > I was reading Post subject "class returning member function", Dated -12/ > 27-28 /07 and found it not to be of value. > Return Value Optimization RVO seems to do no better than a bitwise > assignment. Except the bitwise is more efficient if that is what RVO is to > accomplish. What is the value or need of RVO? That's not it. Consider following code being executed: object foo() { object o; ... return o; } object x = foo(); One could imagine that this code causes an object to be constructed, then being returned from a function (which creates a temporary, i.e. a copy of the original). This temporary is then used to create the local object 'x' in the calling code, i.e. again copied. RVO now has several possible effects to this. Firstly, the temporary value returned from the function and the local object inside the function can be the same. This work because the lifetime of one ends when the others begins and because one is a copy of the other. Since the memory where this object is passed can be known at the start of the function and also that always the same object is returned, it is conceivable that the local object is already created in that place. Secondly, also the temporary returned from the function and the local object 'x' can be the same for exactly the same reasons, just that it's a transfer of the temporary to 'x'. So, in fact there is no copying going on, not even bitwise copying. Bitwise copying wouldn't work anyway, because objects can contain backpointers to themselves. Also, it is so much more simple to avoid copying than optimising it. ;) Uli -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeffrey Baker on 17 Jan 2008 16:47
Thanks, there is one question on the practical side. The code is below. If I understand it then it would be the function test() that is called to directly place object a into object b without a copy being created. This reduces memory usage and creates faster code. Is this the same whether it is in object form or in C form? Class vs. main()? Jeff RVO eliminates a copy std::string foo() { std::string a('a',1000000); return a; } void test() { std::string b = foo(); // use b } "Jeffrey Baker" <tbirdjb2(a)earthlink.net> wrote in message news:13ojbelkf9rsd1d(a)corp.supernews.com... : Jeff : : : : -- : -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |