copy constructor and return value optimization

R

Rahul

While reading "Efficient C++" by Dov Bulka I came across the follown
statement
"In addition, you must also define a copy constructor to "turn on" the
Return Value Optimization(RVO). If the
class involved does not have a copy constructor defined, the RVO is
quietly turned off."

I searched a lot on the groups but still could not find any
satisfactory answer which explains this.
My doubt is "Even if we don't define a copy constructor then compiler
defines one automatically for us". So in this case also compiler
should do optimization. Then why does the book says "you must define"


I know if copy constructor is provate then you can not write
statements like
Complex c = c1+ c1;
which means compiler has no oppertunity to apply RVO.

But I am confused with the case when "we have compiler provided copy
constructor". When i checked with VC++ 7 then it did RVO with the
compiler generated copy constructor also.

Please let me know if i mis-interperted the book's statement, or
please explain me the above case.

Regards
 
T

Thomas Grund

While reading "Efficient C++" by Dov Bulka I came across the follown
statement
"In addition, you must also define a copy constructor to "turn on" the
Return Value Optimization(RVO). If the
class involved does not have a copy constructor defined, the RVO is
quietly turned off."

I searched a lot on the groups but still could not find any
satisfactory answer which explains this.
My doubt is "Even if we don't define a copy constructor then compiler
defines one automatically for us". So in this case also compiler
should do optimization. Then why does the book says "you must define"


I know if copy constructor is provate then you can not write
statements like
Complex c = c1+ c1;
which means compiler has no oppertunity to apply RVO.

But I am confused with the case when "we have compiler provided copy
constructor". When i checked with VC++ 7 then it did RVO with the
compiler generated copy constructor also.

Please let me know if i mis-interperted the book's statement, or
please explain me the above case.

Regards


Hi,

My experience is that in some cases you must define even trivial copy
constructors or assignment operators by hand (only for efficiency
reasons).

The following example is 3 to 4 times faster (VC71) if you uncomment the
copy constructor and =operator.

For explanations please ask again or take a look at the generated
assembler code.

Thomas




#include <iostream>

struct Vector3
{
Vector3(double a, double b, double c)
{
d[0] = a; d[1] = b; d[2] = c;
}

/*
Vector3(const Vector3 & o)
{
d[0] = o.d[0]; d[1] = o.d[1]; d[2] = o.d[2];
}

void operator=(const Vector3 & o)
{
d[0] = o.d[0]; d[1] = o.d[1]; d[2] = o.d[2];
}
*/
double d[3];
};

Vector3 operator+(const Vector3 & a, const Vector3 & b)
{
return Vector3(a.d[0]+b.d[0],a.d[1]+b.d[1],a.d[2]+b.d[2]);
}

int main()
{
Vector3 a(1,2,3);
Vector3 b(0,0,0);

for (int i=0; i<1000000000; i++)
b=b+a;

std::cout << b.d[0] << " " << b.d[1] << " " << b.d[2] << '\n';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top