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

264

u/lightmatter501 Mar 07 '24

Not actually turning on the optimizer.

125

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

MFW when people on embedded forums mention that their project is compiled without optimizations "so the compiler doesn't break things" (iow their code is full of bugs).

29

u/[deleted] Mar 07 '24

[removed] — view removed comment

45

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

I was thinking more of people not using atomics correctly and such so that their code breaks as soon as the compiler reorders variable accesses.

29

u/[deleted] Mar 07 '24

[removed] — view removed comment

11

u/valdocs_user Mar 07 '24

"#define int volatile"

/s

21

u/RoyBellingan Mar 07 '24

replace

not using atomic
with
not having any clue what is cache coherency, probably not even knowing cache exists at all and CPU directly read and write from RAM

26

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

TBF, most bare metal embedded MCUs don't even have data cache. People manage to fuck up with just threads and interrupts alone.

6

u/lightmatter501 Mar 07 '24

There’s also a lot of people used to doing that who use an ARM processor, which promptly screws them over.

3

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

I've never run into a Cortex-M4 or earlier that would have data cache. M7 on the other hand...

→ More replies (2)
→ More replies (9)
→ More replies (1)
→ More replies (1)
→ More replies (1)

7

u/lightmatter501 Mar 07 '24

These same people never turn on compile warnings high enough to see that gcc can tell you how to fix it…

0

u/[deleted] Mar 07 '24

[deleted] — view deleted comment

6

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

I consider -Werror to be fundamentally a bad idea. Not that it would even be possible on many systems since it assumes no manufacturer header (often autogenerated for a different compiler, possibly many years ago) throws no warnings on compiler upgrade.

0

u/[deleted] Mar 07 '24

[deleted] — view deleted comment

5

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

Until the compiler gives a warning about using non-standard pragma to disable specific warnings. Yes, I've that happen in a real project. Not that pragmas are a solution for third party code anyway.

3

u/JesusWantsYouToKnow Mar 07 '24

Absolutely, those of us that work day in and day out with all the esoteric barely supported HALs and BSPs that manufacturers crank out for their platforms know exactly why embedded devs don't embrace -Werror as a blanket policy. You can spend an inordinate amount of time improving the vendor's toolchain for some super niche product that nobody will care about, or you can get on with implementing your firmware and making sure your own code is well structured and error free.

→ More replies (1)
→ More replies (1)

5

u/nryhajlo Mar 07 '24

There are a lot of superstitious grey beards in the embedded world that hold a lot of prejudice (why C is still very common). I guess being that close to the hardware for that long can make anyone superstitious.

5

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

There are a lot of superstitious grey beards in the embedded world that hold a lot of prejudice (why C is still very common).

And not so grey beards. I will never understand why so many people under the age of 40 keep avoiding C++ there. Even if you only use it as a stronger typed C, you already get significant benefits.

I guess being that close to the hardware for that long can make anyone superstitious.

Can't be that, given that I was writing low level hw interfacing code (for DOS as a teenager) before many of those C lovers were even born.

4

u/GYN-k4H-Q3z-75B Mar 07 '24

Works on my hunk of metal.

13

u/Polarstrike Mar 07 '24

Well.. most underrated comment

4

u/HydrogenxPi Mar 07 '24

Is this referring to compiling in debug rather than release mode?

13

u/AssemblerGuy Mar 07 '24

It is referring to compiling with -O0.

6

u/Nicksaurus Mar 07 '24 edited Mar 07 '24

Debug/Release are build system presets (in visual studio and cmake at least) that change various things including optimisation levels, so basically yes - Debug builds are unoptimised, release builds are optimised

Depending on the libraries you're building with, debug builds may also come with other performance penalties from enabling runtime safety checks to make debugging easier

3

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

Debug builds are unoptimised

Not necessarily. On many systems debug builds are built with gcc -Og or equivalent, particularly if there are any performance requirements (eg. embedded systems with interrupt handlers that have some maximum latency to avoid losing data).

→ More replies (3)

4

u/Ameisen vemips, avr, rendering, systems Mar 07 '24

Or using -O3 with MSVC which does nothing.

→ More replies (2)
→ More replies (3)