r/cpp • • Mar 07 '24

What are common mistakes in C++ code that results in huge performance penalties?

As title, list some common mistakes that you have done/seen which lead to performance penalties.

231 Upvotes

325 comments sorted by

View all comments

Show parent comment

1

u/CentralSword Mar 07 '24 edited Mar 07 '24

In my opinion using atomics doesn't require thinking about the most suitable memory order in every operation. It's enough to know what these two words mean and how to use it to optimize your code. I myself have never used half of the memory order types, I know that if I ever want to optimize something with it, I will just open cppreference. Also I was taught that relaxed memory order is an expert friendly thing that isn't that necessary to use, unless you're on a very specific architecture.

3

u/angelicosphosphoros Mar 07 '24

Relaxed is quite useful. For me, relaxed is the easiest order to reason about, followed by acquire and release, then acqure_release, and, finally, seq_cst. In fact, I have yet to encounter a case of usage of seq_cst in production that wasn't either careless usage or usage by a person who don't understand what he is doing.

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Mar 08 '24

I myself have never used half of the memory order types

Hell, I only ever use sequentially consistent memory order. In the code I write the variables are accessed relatively rarely and with almost zero contention but locking is not permitted due to unbounded latency issues. With those constraints it simply isn't worth it to try to optimize the accesses.