copy constructor and return value optimization

Discussion in 'C++' started by Rahul, Jul 5, 2008.

  1. Rahul

    Rahul Guest

    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
     
    Rahul, Jul 5, 2008
    #1
    1. Advertising

  2. Rahul

    Thomas Grund Guest

    On Sat, 5 Jul 2008, Rahul wrote:

    > 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';
    }
     
    Thomas Grund, Jul 7, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    343
    Victor Bazarov
    Jul 29, 2005
  2. SzH
    Replies:
    10
    Views:
    660
    James Kanze
    Apr 26, 2007
  3. sanjay
    Replies:
    1
    Views:
    386
    sanjay
    Oct 9, 2008
  4. Jan Lellmann
    Replies:
    7
    Views:
    884
    Jan Lellmann
    Nov 9, 2009
  5. cinsk
    Replies:
    35
    Views:
    2,662
    James Kanze
    Oct 11, 2010
Loading...

Share This Page