r/cpp • u/GiannoVersaco • Jan 26 '21
Moving Faster: Everyday Efficiency in Modern C++
https://www.youtube.com/watch?v=LFv7XwgsdLY20
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).
10
u/adnukator Jan 27 '21 edited Jan 27 '21
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.
If you remove
for (int n = 0; n < 9; ++n), the difference is just 10% https://quick-bench.com/q/3ILLTJUWjencJTNWrDnfZ88sTCwIf you call v.reserve(1000); in the
load_numbersfunctions, ByRef becomes 25% faster, WithMove becomes 20% slower than the original. https://quick-bench.com/q/ThuPYlhZQupgwzybEt-9fxr3MQIIf you call
reserve(1000)before the loops, ByRef remains the same as in the above bullet, WithMove becomes 25% faster than in the original https://quick-bench.com/q/NBDdPUrz3bBLk48642bq1gP5QicThe 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 ByRefEven 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
Microbenchmarks are an art form...
3
u/jk-jeon Jan 27 '21
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
ByRefcan 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_backand replaces it withresizeandoperator[]. The result was thatByRefis marginally faster thanReturningWithMove. So probably Clang is somewhat confused optimizingpush_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.1
u/Full-Spectral Jan 27 '21
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.
11
u/PRSprogrammer Jan 26 '21
Good talk, but confused (30:25) why this is this more costly
vector<string> vec;
vec.reserve(...);
vec.push_back("Hello");
vec.push_back("World");than this
vector<string> vec;
vec.reserve(...);
vec.emplace_back("Hello");
vec.emplace_back("World");11
u/Habitate Jan 26 '21
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.
8
u/adnukator Jan 26 '21
To be exact, it saves a move constructor and a destructor call (the moved-from string still exists in some form after the move)
2
u/xurxoham Jan 26 '21
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.7
u/dodheim Jan 27 '21 edited Jan 27 '21
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 tostd::strings.ED: The upside is that it effectively does the
.reserve()call for you and avoids the capacity-check branches inside ofpush_/emplace_back; but having to copy-construct each element will be a net loss the majority of the time.1
u/SunnybunsBuns Jan 27 '21
Aren't those elements
char [N]s, notstd::strings?2
u/dodheim Jan 27 '21
The signature of
vector<T>::insertisconstexpr iterator insert(const_iterator, initializer_list<T>);so if
vecis avector<string>then that is implicitly aninitializer_list<string>.0
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.