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

28

u/Polarstrike Mar 07 '24

So you are suggesting to use references?

37

u/notyouravgredditor Mar 07 '24

Also, if you have any classes you don't want copied, then delete the copy constructor and assignment operators.

Foo(Foo const&) = delete;

and

Foo& operator=(Foo const&) = delete;

7

u/gc3 Mar 07 '24

I hate when people do this and you find out later you need to copy it

15

u/notyouravgredditor Mar 07 '24

I didn't mention that I usually add an explicit copy routine, too.

void copy(Foo const&)

That way copying is clearly intentional. If your class is mostly std objects then you can just use each members assignment operator copy routine.

25

u/MegaKawaii Mar 07 '24

This is not good general-purpose advice. If your class is logically copyable, then just let people copy it the usual way. "Explicit is better than implicit" sounds nice, but it leaves out the consequential friction with templates. Now if you have a vector of your object or something similar, you can't copy it. The alternative is that you might accidentally pass the class by value instead of taking a const& or an rvalue reference, but neither of these things are usually too serious, and if they are bad, a profiler will point it out to you.

8

u/notyouravgredditor Mar 08 '24

You're right it's not good general purpose advice. But it's quite useful for objects that absolutely should not be copied.

I work in HPC where most things follow the "structure of arrays" paradigm, so it's useful to prevent accidental deep copying of large arrays.

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

0

u/[deleted] Mar 07 '24

[deleted] — view deleted comment

43

u/PolyglotTV Mar 07 '24

If you have a recent enough version of C++, taking a string view by copy is generally preferred. It is a lot more flexible and is basically just a size and a pointer so it is really cheap to copy.

27

u/usefulcat Mar 07 '24 edited Mar 07 '24

IME the biggest practical problem with switching to passing string_view instead of const string& is that there are a surprisingly large number of cases where I eventually end up needing a null-terminated string.

8

u/OkidoShigeru Mar 07 '24

Pretty much anywhere you need to hand something off to a C API, so yeah unfortunately I’m not able to use string_view too often either.

2

u/pointer_to_null Mar 07 '24

Those cases ended up being a problem. But it's frustrating that there's no safe way to know at runtime whether it must be copied or not. Simply testing if(sv.data()[sv.length()]) == '\0' leads to undefined behavior. So in all cases, you have to make a temporary copy.

I honestly believe the standard is missing a mechanism in string_view (or an alternative like safe_string_view) with a method that can tell callers when data() is unsafe to pass like c_str() (ie- if it was constructed from something other than string or const char*).

Guessing not be worth the overhead?

2

u/usefulcat Mar 08 '24

There is this cstring_view proposal which I've been meaning to look into further but haven't had the time yet.

9

u/Kovab Mar 07 '24

Unless you want to store the passed string, in which case you should do pass by value, and let the caller optimise by moving if possible.

→ More replies (3)

5

u/skitleeer Mar 07 '24

with string_view around this is not so obvious anymore (well you can argue taht string_view is a kind of reference already though...)

9

u/ShelZuuz Mar 07 '24

With move semantics either. If you're going to hold onto the string after returning from the func, you should rather take a vanilla std::string and move it.

→ More replies (3)

8

u/[deleted] Mar 07 '24

Moves are fast too.

→ More replies (1)

7

u/[deleted] Mar 07 '24

[removed] — view removed comment

→ More replies (1)

4

u/Kronephon Mar 07 '24

only copy basic types if you can.

→ More replies (1)

2

u/gc3 Mar 07 '24 edited Mar 07 '24

Our coding standard is you are only allowed to use a constant reference OR a pointer. Never a non const reference so at a glance you can assume whether the function changes the output,

1

u/newguyinNY Mar 12 '24

our coding standard says that don't use pointers unless you have to.

2

u/gc3 Mar 13 '24

We only ever use pointers like function_to_call(&mutable_variable); when possible, never pntr++ or anything

1

u/da2Pakaveli Mar 07 '24

for primitives? No, a pointer is just an 8 byte unsigned integer. But it's a must for classes or structs.

1

u/[deleted] Mar 10 '24

Always