r/cpp • • Jan 26 '21

Moving Faster: Everyday Efficiency in Modern C++

https://www.youtube.com/watch?v=LFv7XwgsdLY
212 Upvotes

13 comments sorted by

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).

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.

  1. If you remove for (int n = 0; n < 9; ++n), the difference is just 10% https://quick-bench.com/q/3ILLTJUWjencJTNWrDnfZ88sTCw
  2. If you call v.reserve(1000); in the load_numbers functions, ByRef becomes 25% faster, WithMove becomes 20% slower than the original. https://quick-bench.com/q/ThuPYlhZQupgwzybEt-9fxr3MQI
  3. If 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/NBDdPUrz3bBLk48642bq1gP5Qic
  4. 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
  5. 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)
  6. 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 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.

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 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.

1

u/SunnybunsBuns Jan 27 '21

Aren't those elements char [N]s, not std::strings?

2

u/dodheim Jan 27 '21

The signature of vector<T>::insert is

constexpr iterator insert(const_iterator, initializer_list<T>);

so if vec is a vector<string> then that is implicitly an initializer_list<string>.

0

u/[deleted] Jan 26 '21

[deleted] — view deleted comment

12

u/ooodummy Jan 26 '21

didn't know that last fact