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

1

7

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.

Thill answered 21/5, 2014 at 8:28 Comment(1)
you can check link. eatplayhate.me/2013/10/01/c-cargo-cults-rvo-and-copy-elisionUnderthrust
P
3

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.

Panhellenism answered 21/5, 2014 at 8:34 Comment(6)
but not between modulesGonick
@tenfour: define "modules"; for static libraries and object files, you're wrong, for shared libraries (so/DLL), you're right. Added that to my answer.Panhellenism
in a windows context, "module" means a PE file... DLLs, OCX, EXE, etc. So we agree :DGonick
@Gonick Oddly, the link that Jerry posted above claims that it even works for DLLs (RVO at least)Thill
true, and maybe some compilers would make it happen. But true RVO requires cooperation between caller & callee, so I don't see how it's possible in any general sense. When it comes to exporting C++ features across DLL bounds, things get very precarious and implementation-specificGonick
Yes, passing values across boundaries can cause trouble anyway because the allocators can differ.Thill

© 2022 - 2024 — McMap. All rights reserved.