Return value optimization
Encyclopedia
Return value optimization, or simply RVO, is a compiler optimization
technique that involves eliminating the temporary object
created to hold a function
's return value. In C++
, it is particularly notable for being allowed to change the observable behaviour of the resulting program
.
to perform any optimization, as long as the resulting executable
exhibits the same observable behaviour as if all the requirements of the standard have been fulfilled. This is commonly referred to as the as-if rule. The term return value optimization refers to a special clause in the C++ standard that allows an implementation to omit a copy operation resulting from a return statement
, even if the copy constructor
has side effects
, something that is not permitted by the as-if rule alone.
The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor
has a visible side effect (printing text). The first copy that may be eliminated is the one where
Depending upon the compiler
, and that compiler's settings, the resulting program
may display any of the following outputs:
usually carries little to no overhead, since the object typically fits in a CPU register
. Returning a larger object of class type may require more expensive copying from one memory location to another. To achieve this, an implementation may create a hidden object in the caller's stack frame, and pass the address of this object to the function. The function's return value is then copied into the hidden object. Thus, code such as this:
May generate code equivalent to this:
which causes the
In the early stages of the evolution of C++
, the language's inability to efficiently return an object of class type from a function was considered a weakness. Around 1991, Walter Bright
invented a technique to minimize copying, effectively replacing the hidden object and the named object inside the function with the object used to hold the result:
Bright implemented this optimization in his Zortech C++
compiler. This particular technique was later coined "Named return value optimization", referring to the fact that the copying of a named object is elided.
Compiler optimization
Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied...
technique that involves eliminating the temporary object
Temporary variable
In computer programming, a temporary variable is a variable whose purpose is short-lived, usually to hold temporary data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared with local scope...
created to hold a function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
's return value. In C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, it is particularly notable for being allowed to change the observable behaviour of the resulting program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
.
Summary
In general, the C++ standard allows a compilerCompiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
to perform any optimization, as long as the resulting executable
Executable
In computing, an executable file causes a computer "to perform indicated tasks according to encoded instructions," as opposed to a data file that must be parsed by a program to be meaningful. These instructions are traditionally machine code instructions for a physical CPU...
exhibits the same observable behaviour as if all the requirements of the standard have been fulfilled. This is commonly referred to as the as-if rule. The term return value optimization refers to a special clause in the C++ standard that allows an implementation to omit a copy operation resulting from a return statement
Return statement
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation...
, even if the copy constructor
Copy constructor
A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed , which might be followed by parameters of any type...
has side effects
Side effect (computer science)
In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world...
, something that is not permitted by the as-if rule alone.
The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor
Copy constructor
A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed , which might be followed by parameters of any type...
has a visible side effect (printing text). The first copy that may be eliminated is the one where
C
is copied into the function f
's return value. The second copy that may be eliminated is the copy of the temporary object returned by f
to obj
.Depending upon the compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...
, and that compiler's settings, the resulting program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
may display any of the following outputs:
Hello World!
A copy was made.
A copy was made.
Hello World!
A copy was made.
Hello World!
Background
Returning an object of builtin type from a functionSubroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
usually carries little to no overhead, since the object typically fits in a CPU register
Processor register
In computer architecture, a processor register is a small amount of storage available as part of a CPU or other digital processor. Such registers are addressed by mechanisms other than main memory and can be accessed more quickly...
. Returning a larger object of class type may require more expensive copying from one memory location to another. To achieve this, an implementation may create a hidden object in the caller's stack frame, and pass the address of this object to the function. The function's return value is then copied into the hidden object. Thus, code such as this:
May generate code equivalent to this:
which causes the
Data
object to be copied twice.In the early stages of the evolution of C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, the language's inability to efficiently return an object of class type from a function was considered a weakness. Around 1991, Walter Bright
Walter Bright
Walter Bright is a computer programmer known for being the designer of the D programming language. He was also the main developer of the first C++ compiler that translated directly to object without going via C, Zortech C++ . Before the C++ compiler, he developed the Datalight C compiler, also...
invented a technique to minimize copying, effectively replacing the hidden object and the named object inside the function with the object used to hold the result:
Bright implemented this optimization in his Zortech C++
Digital Mars
Digital Mars is a small American software company owned by Walter Bright that makes C and C++ compilers for Windows and DOS. They also distribute the compilers for free on their web site....
compiler. This particular technique was later coined "Named return value optimization", referring to the fact that the copying of a named object is elided.