Compiler Optimization of copy constructor

Source: Internet
Author: User

Http://www.csdn.net/expert/topic/806/806937.xml? Temp =. 2044489.
In fact, I didn't figure out this issue when I saw <Deep Exploration>. In that post, I spoke nonsense,
Now I want to thoroughly understand this problem. Now I want to write down my understanding of some of the content in the book,
Thank you for your attention. For more information, see pages 60-75.
Problem description:
Copy constructor: the constructor called when one class object is used as the initial value of another class object.
This topic is to discuss the policy of the compiler when calling the copy constructor (how to optimize it to improve efficiency). Hou Jie calls it "program-converted Semantics" (program transformation semantics ).

For an example, see the following program section:

X Bar () // apparently, bar_x in this function is used to return its value, which is used to assign values to an object outside the function.
{
X bar_x; // constructor bar_x
... // Process bar_x
Return bar_x; // analysis function bar_x
}

Void Foo ()
{
// Copy constructor xx = bar ()
X xx = bar ();
//...
// Call destructor XX here
}
Why is this program segment optimized? How to optimize it?
Because the statement
X xx = bar ();
There are some improvements in the following two areas:
1. The function of constructing bar_x is only to return its value, which is used to assign values to objects outside the function.
2. Call the bar () function. A temporary object structure is returned, and a copy of the constructor is called.
To illustrate this problem, see how the return value of Bar () is copied from the local object bar_x,
(Two-Phase Conversion in cfront of stroustrup ):
1. First, add an additional parameter with the type of a reference of Class Object. This parameter will be used to place the returned values constructed by the copy;
2. Install a copy constructor call operation before the return command to treat the content of the object to be returned as the initial values of the new parameters.
In this way, the next conversion operation will rewrite the function so that it does not return any value.
In this way:
X Bar ()
{
X bar_x; // constructor bar_x
... // Process bar_x
Return bar_x; // analysis function bar_x
}
Converted:

// The C ++ pseudo code converted by the function;
// To reflect the call of copy constructor.

Void bar (X & _ result) // _ result is the additional parameter added.
{X bar_x;

Bar_x.x: X (); // call operation of the default constructor generated by the compiler

... // Process bar_x

_ Result. X: X (bar_x); // The copy constructor generated by the compiler calls the operation.

Return;
}

So the statement
X xx = bar ();
It is converted into the following two command sentences during compilation:

X xx; // note that the default constructor is not implemented, and XX is constructed in the bar () function.
Bar (XX );

As shown above, there are two problems in bar:
1. The function of constructing bar_x is only to return its value, which is used to assign values to objects outside the function.
2. Call the bar () function. A temporary object (Bar-x) is returned, and a copy of the constructor (_ result) is called.

So how can we improve it?
The focus is to suppress calling of the copy constructor.

Method 1: optimization at the user level is mainly because someone puts forward the constructor of "computing,
That is to say, the constructor has operations other than assigning values to member variables and directly calculates _ result. This is not discussed here.

Method 2: Optimization on the compiler layer. The Compiler replaces bar_x directly with _ result in the bar () function,
So before conversion:
X Bar ()
{
X bar_x; // constructor bar_x
... // Process bar_x
Return bar_x; // analysis function bar_x
}
After conversion:

Void bar (X & _ result)
{
_ Result. X: X (); // The default constructor is called.

... // Directly process _ result

Return;
}
Have you seen it? Without the bar_x object or the call to copy the constructor.

In this case, the program section at the beginning of the article:
X Bar ()
{
X bar_x; // constructor bar_x
... // Process bar_x
Return bar_x; // analysis function bar_x
}

Void Foo ()
{
// Copy constructor xx = bar ()
X xx = bar ();
//...
// Call destructor XX here
}
The pseudo code is as follows:

Void bar (X & _ result) // optimize it at the compiler Layer
{
// There is no definition of x xx, and _ result is directly substituted into the following expression
... // Directly process _ result
Return; // optimize the user loading layer on 65 pages
}

Void Foo ()
{
X xx_result; // No constructor is called.

Bar (xx_result); // construct and process xx_result in the bar () function.
// Analyze xx_result
}
Question: Do you admit that the speed is getting faster?
But the book says, "In this case, the symmetry is broken, and the program runs fast, but it is wrong."
Why?
What is symmetry?
Where is the error?

{
To: jinfeng_wang (only once a day), others can skip,
You think that the xx_result object is constructed in the statement (x xx_result;), but according to <Deep Exploration> p64 reciprocal
Row 6th specifically points out that xx_result is not constructed here but constructed in the bar () function body. You can see the above pseudo code, from p64.
}

I started to understand that symmetry refers to the symmetry between the number of constructor and destructor. Now, according to the above analysis,
The symmetry here clearly refers to the asymmetry between the constructor and the destructor.

Because
The constructor of xx_result is called in bar.
The Destructor is called at the end of Foo.
(Please refer to the above pseudo code ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.