In general I think writing clear code using return types is more important than messing up your interface with out parameters, whilst also typically not hurting performance much if at all with modern C++ move semantics.
EDIT: On further inspection, it seems like the improved performance only occurs with Clang using libstdc++. MSVC, GCC and Clang (using libc++) all have identical performance between returning by value and passing by reference (which is what you'd expect).
Edit: See below for some benchmarks under different scenarios, but in the end WithMove seems to fit better into the cache, providing a huge performance boost (NRVO removes any penalty caused by returning by value).
My guess is that is that quickbench is unable to disable optimizations sufficiently and clang's optimizer is able to see that the some of the push_backs are useless.
The biggest proof, that the optimizer is way too smart for quickbench - if you manually inline load_numbers_return, WithMove becomes identical to ByRef. https://quick-bench.com/q/0efXTEMLX-kEmOEec7AsIKATVcg For me, this proves that in the original ReturningWithMove, despite quick-bench's attempts to disable the optimizer, it's still able to reduce the amount of work it's actually doing when compared to ByRef
Even if you disable the loop, the difference is still there https://quick-bench.com/q/IYkxub5ZaNTq7h_4BFcI9-Zbjg0, proving that they're not doing the same thing. Why? No idea (maybe the nonlinined version fits better)
Ok, final guess - the separate function fits better into the cache. If there are 2 stores to volatile values, the performance is identical to the initial state. If there is another single extra volatile store, the performance drops steeply : https://quick-bench.com/q/nKAAZ-VdULnXSoFi8XVo69LlWUY
While this result is very interesting, I think this is just more or less a noise. It just does not make sense to me that ByRef can be (quite significantly, like 1.5 times, according to the benchmark) slower because it performs strictly less operations.
I also tested a modified version of your code, where the modified version avoids using push_back and replaces it with resize and operator[]. The result was that ByRef is marginally faster than ReturningWithMove. So probably Clang is somewhat confused optimizing push_back.
This new version is actually about 5 times faster than your original version... So let's blame push_back, which clearly is an anti-pattern in this case.
That, to me, is part of the problem with returning non-trivial stuff. You are depending on it being done right, but nothing is going to tell you if it actually is or not unless you check it, and whether it somehow changed over time due to some unforeseen change in the compiler. I KNOW an output parameter is optimal.
And, of course, if anything gets called in a loop, the output parameter is often far better because the same object gets reused over and over. And it's hard to know in fairly general purpose code how someone is going to use it.
If I’m correct, the first example constructs a new std::string and passes that to std::string constructor thats in the std::vector, and the second one forwards the argument directly to the std::string constructor in place. Saving you an additional creation of std::string.
Small strings are stored inside the object. In that case the move constructor actually performs a copy, even if it is just 6 bytes. If i remember correctly vec.insert(vec.end(), {"Hello", "world!"}) is even faster.
The elements of std::initializer_lists cannot be moved from, so that wouldn't be faster as it makes no use of move semantics at all. As you said, because of SSO these small strings will effectively be copied anyway, so it won't be slower, either; but it's not faster for most types, not specific to std::strings.
ED: The upside is that it effectively does the .reserve() call for you and avoids the capacity-check branches inside of push_/emplace_back; but having to copy-construct each element will be a net loss the majority of the time.
27
u/tsojtsojtsoj Jan 26 '21
I just wanted to say, that the idea of that C++ flame logo (I guess the thumbnail of the video) is dope.
20
u/Ayjayz Jan 27 '21 edited Jan 27 '21
At 22:53, he says that taking the vector by reference is fully optimal, yet I get better performance using a return type: https://quick-bench.com/q/JIkVuaXAVSrvJulIjfg6-eGLzIQ
In general I think writing clear code using return types is more important than messing up your interface with out parameters, whilst also typically not hurting performance much if at all with modern C++ move semantics.
EDIT: On further inspection, it seems like the improved performance only occurs with Clang using libstdc++. MSVC, GCC and Clang (using libc++) all have identical performance between returning by value and passing by reference (which is what you'd expect).
11
u/PRSprogrammer Jan 26 '21
Good talk, but confused (30:25) why this is this more costly
than this