Temporary Variables: Chasing Temporaries Away - Return Value Optimization
(Page 2 of 5 )
The official term for what we are aiming to achieve is ‘Return Value Optimization.’ This is a technique where you tell the compiler that it is safe to kill the temporary object needed for the returned value.
All you need to do is return a constructor argument and the compiler will take care of the rest. When an object is created directly inside a statement, without being assigned to a variable, we are looking at a constructor argument.
Lets build a sample portion of the Vector3 class as I did in the first article to visualize the compiler’s actions. We’ll implement the -= operator, the + operator and the - operator. The -= operator will demonstrate the efficiency of references, the + operator will show what happens when we don’t facilitate for return value optimization and the – operator will show the effect of return value optimization.
Our goal is to make:
Vector3 nme2ply=player.GetPos() – nmeIt->GetPos();
As efficient as:
Vector3 nme2ply(player.GetPos());
nme2ply -= nmeIt->GetPos();
To achieve this we’ll refactor the test class ‘MyClass’ into a Vector3 test class.
A Vector3 test class#include <stdio.h>
class Vector3 {
char const mName[80];
public:
float mX, mY, mZ;
Vector3(char const *name, float x=0.f, float y=0.f, float z=0.f)
: mX(x), mY(y), mZ(z)
{
(void)sprintf(mName, “%s”, name);
(void)printf(“%s – Constructed.\n”, mName);
}
Vector3(Vector3 const &other)
: mX(other.mX), mY(other.mY), mZ(other.mZ)
{
(void)sprintf(mName, “’copy of %s’”, other.mName);
(void)printf(“%s – Copy Constructed.\n”, mName);
}
~Vector3()
{
(void)printf(“%s – Destructed called.\n”, mName);
}
Vector3& operator= (Vector3 const &rhs)
{
if (this != &rhs) {
mX = rhs.mX;
mY = rhs.mY;
mZ = rhs.mZ;
}
(void)printf(“%s – Assigned to %s.\n”, rhs.mName, mName);
return *this;
}
Vector3& operator-=(Vector3 const &rhs)
{
mX -= rhs.mX;
mY -= rhs.mY;
mZ -= rhs.mZ;
(void)printf(“%s -= %s finished.\n”, mName, rhs.mName);
return *this;
}
Vector3 const operator+ (Vector3 const &rhs) const
{
Vector3 result(mX+rhs.mX, mY+rhs.mY, mZ+rhs.mZ);
(void)printf(“%s + %s finished.\n”, mName, rhs.mName);
return result;
}
Vector3 const operator- (Vector3 const &rhs) const
{
(void)printf(“%s - %s finished.\n”, mName, rhs.mName);
return Vector3(“ctor arg”, mX-rhs.mX, mY-rhs.mY, mZ-rhs.mZ);
}
void print() const
{
(void)printf(“%s at (%.1f %.1f %.1f)\n”, mName, mX,mY,mZ);
}
};
You can see that operator+ uses a local variable, which is then returned by value, while the operator- uses what we now recognize as a constructor argument. We expect operator-= to be nothing less than the most efficient… but what better way than to verify this by running some code?
Note that normally it is recommended that you use std::string instead of char buffers. Here I thought it would keep the class a bit more compact, and sometimes it is just easier to use printf and sprintf instead of STL streams. Please don’t send me any emails about possible buffer overflows, as this is simply sample code. ;)
Next: Vector3 test >>
More C++ Articles
More By J. Nakamura