Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing the number of constructor calls

At work we have a class with an expensive constructor so we would like it to be called as few times as possible. We looked through the uses of it and tried to make the code more RVO friendly so to say.

However we found a quirk in the g++ compiler where we didn't understand what happened.

Please consider the two implementations of operator+

const Imaginary Imaginary::operator+(const Imaginary& rhs) const
{
    Imaginary tmp(*this);
    tmp.append(rhs);
    return tmp;
}

and

const Imaginary Imaginary::operator+(const Imaginary& rhs) const
{
    return Imaginary(*this).append(rhs);
}

I have put print-outs in the the various constructors and with the following little program

int main(int argc, char* argv[])
{
    Imaginary x(1, 1);
    Imaginary y(2, 1);

    Imaginary c = x + y;
    return 0;
}

I get this print out with the first implementation of operator+

int/int ctor
int/int ctor
Copy ctor

And I get the following when the second variant of operator+ is in use

int/int ctor
int/int ctor
Copy ctor
Copy ctor

Here we see that g++ is able to optimize away one call to the copy constructor in one case but not the latter and to my surprise, it managed to do it with the more clumsy implementation where I saved it to a temporary.

Now I could've understood it more if it was the other way around but appearantly it isn't and now I am hoping that maybe one you could enlighten me on this subject.

I should probably add that when we add --no-elide-constructors as a flag to g++ I get the following print out

int/int ctor
int/int ctor
Copy ctor
Copy ctor
Copy ctor

Regards, Mattias

like image 390
Mattias Borgstrom Avatar asked Sep 06 '11 13:09

Mattias Borgstrom


1 Answers

If the compiler cannot inline append, then it cannot determine that the return value is target object. Then it doesn't know that the temporary is being returned, and cannot construct it in place.

You would have the same behavior with:

Imaginary tmp(*this);
return tmp.append(rhs);

If the return value of append is opaque to the compiler (defined in another compilation unit), it prevents the optimization.

like image 171
Ben Voigt Avatar answered Oct 18 '22 12:10

Ben Voigt