Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do RVO and copy elision only work within one compilation unit or not?

Do they work across different object files? Do they work across different DLLs?

I know this depends on the compiler. I'm curious if there are any compilers and optimization settings that will make this work.

like image 920
Sarien Avatar asked May 21 '14 08:05

Sarien


People also ask

Is copy elision guaranteed?

Copy elision (NRVO) is allowed there and is routinely performed by most compilers, but is still non-guaranteed, and the widget class cannot be non-copyable non-movable.

What is copy move elision?

Copy elision is a compiler optimization technique that eliminates unnecessary copying/moving of objects.

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 C have RVO?

> 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). What ? RVO is precisely needed because a copy in C++ can run arbitrary code and so is not as easy to ellide as a memcpy.


1 Answers

Normally, yes, but in principle, using Link-Time-Optimization (-flto for GCC/Clang compilers and linkers) or Link-Time-Code-Generation (/LTCG and /GL for MSVC's compiler and linker), the compiler and linker can leverage their shared knowledge and perhaps inline code and elide copies. GCC's manual states:

[...] this causes all the interprocedural analyses and optimizations in GCC to work across the two files as if they were a single one. This means, for example, that the inliner is able to inline functions in bar.o into functions in foo.o and vice-versa.

Note this will not work with DLLs, because a shared library's code is fixed and already fully compiled.

RVO only needs information about the function itself (as it constructs the function's return value in-place instead of copying/moving on return. This will likely work without the aboce options.

like image 115
rubenvb Avatar answered Oct 02 '22 06:10

rubenvb