Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the c++ compiler optimize away unused return value by `reference`?

Before someone jumps and says Profile before optimize!, this is simply a curiosity question and stems from this original question.

If I am returning by reference the same object, would that get optimized away if not used? For example, I have a Vector<> that has various mathematical functions (assume I am not using operator overloading). Two ways of writing it:

inline void Vector::Add(const Vector& in) // Adds incoming vector to this vector

OR

inline Vector& Vector::Add(const Vector& in) // Adds incoming vector to this vector and returns a reference to this vector

Now if Add() is used without utilizing the return value, will the compiler simply throw away the return altogether and the function becomes as if it has no return value to begin with? And what if it is NOT inlined?

like image 556
Samaursa Avatar asked Jun 26 '11 17:06

Samaursa


People also ask

Does C have return value optimization?

> Note also that C doesn't have return-value-optimization, hence all your struct-returning functions will cause a call to memcpy (won't happen when compiled in C++ mode of course).

How does return value optimization work?

In the context of the C++ programming language, return value optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function's return value. RVO is allowed to change the observable behaviour of the resulting program by the C++ standard.

Does compiler optimize code?

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.

How does the C++ compiler optimize?

The C/C++ compiler compiles each source file separately and produces the corresponding object file. This means the compiler can only apply optimizations on a single source file rather than on the whole program. However, some important optimizations can be performed only by looking at the whole program.


2 Answers

References as arguments or return statements are usually implemented in a manner similar to pointers and the cost is minimal (negligible in most case). Depending on the calling convention it can be a single store in a register.

As to whether the return can be optimized away, unless the compiler is inlining the code no, it cannot. When the compiler processes the function, it does not know whether calling code will use or not the return statement, and that in turn means that it must always return something.

like image 189
David Rodríguez - dribeas Avatar answered Oct 20 '22 17:10

David Rodríguez - dribeas


If the function isn't inlined, then yes the return value has to be stored somewhere, probably a CPU register. This probably just requires a single register copy. I would be surprised if the overhead was more than a single CPU cycle in most cases.

like image 42
Tim Armstrong Avatar answered Oct 20 '22 17:10

Tim Armstrong