C++ programming rules 365 one day (358) copy elision (return value optimization NVO and named return value optimization NRVO)

Reference: https://en.cppreference.com/w/cpp/language/copy_elision

Elision is an English word that means to omit, abridge or ignore. In C++, Copy elision is a kind of compiler optimization technology, which can avoid unnecessary copy and move operations, thereby improving the performance and efficiency of the program.

The goal of Copy elision is to reduce the use of temporary variables, that is, to avoid copying multiple objects when a function returns, especially when the type of the return value is the same as the type of the defined object, and even a temporary object does not need to be created. This improves runtime efficiency.

Copy elision was introduced in the C++11 standard and is enabled by default, but there are some exceptions, such as when the constructor contains side effects (side effects), copy elision may not happen.

Copy elision is different from move semantics (move semantics). Move semantics emphasizes the use of rvalue references to transfer resource ownership in order to reduce object copying and improve efficiency, while copy elision focuses more on code optimization during compilation.

Optimizations that force the compiler to implement

When returning a prvalue:

T f()
{
    
    
    return T();
}
 
f(); // only one call to default constructor of T

This optimization is called NVO, and RVO is a more basic optimization. As long as the return value is a temporary object and the object is not named, the compiler must use RVO optimization to avoid unnecessary copy or move operations.

When initializing an object, the compiler must also optimize if the initializer is a prvalue.

T x = T(); 

The above two are optimizations that the compiler forces to do.

Optimization is not mandatory

In C++ programming, it is often necessary to return an object from a function. When the returned object is a non-volatile object with automatic storage duration and satisfies the following conditions:

  • is not a function parameter or a catch clause parameter;
  • The class type it belongs to (ignoring the cv modifier) ​​is the same as the function return value type.

At this time, the compiler will try to perform a copy omission optimization called " NRVO " (Named Return Value Optimization, named return value optimization). This optimization allows the compiler to avoid performing additional copy or move operations when returning the object, and directly construct the object at the specified memory location to improve program performance.

It should be noted that NRVO is an optional optimization method, not mandatory. Therefore, even if all the above conditions are met, the compiler may still choose to perform a copy or move operation instead of using NRVO optimization.

NRVO is just an extension of RVO. It optimizes a named non-volatile object with automatic storage duration. If a function returns this named object, the compiler will try to use NRVO to avoid object copying or moving.

Guess you like

Origin blog.csdn.net/HandsomeHong/article/details/130051131