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

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)

2

u/garfgon Mar 07 '24

Can also be messed up by out-of-order execution...which most bare metal embedded MCUs also don't have. Yet. Although putting in memory barriers can be good practice anyway as it will also (conveniently) ensure the compiler doesn't lift your variable accesses outside the barrier either.

I'm expecting "AI-ready" to be used to sell bigger embedded MCUs any year now...

4

u/tonyarkles Mar 07 '24

I don’t remember which MCU it was… probably an STM32F042 or F103 that required an extra instruction cycle in between writing two peripheral registers. Worked fine in debug mode… that was a special day.

4

u/garfgon Mar 07 '24

Which I'm sure was explained clear as day in the 700 page user guide (/s).

3

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

That's quite literally why the __DSB() intrinsic exists on ARM platforms.

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

2

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

Atmel AVR (8-bit, I'm unfamiliar with 32) doesn't have a cache. Or branch prediction.

2

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

There's a reason I have my own AVR library in C++ to standardize things like this so I don't screw up.

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

2

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

-Werror is like using a cannon to shoot a fly. Just declare that any releases / milestones must not have any unaccounted for warnings. No need to cause potentially massive headache for any future developers many years ahead.

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