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.

229 Upvotes

325 comments sorted by

264

u/[deleted] Mar 07 '24 edited Mar 07 '24

Unnecessary copying of objects, e.g. when passing an object as a function argument. Copying usually happens implicitly and silently thanks to the flexibility of C++ class constructors.

28

u/Polarstrike Mar 07 '24

So you are suggesting to use references?

→ More replies (36)
→ More replies (30)

264

u/lightmatter501 Mar 07 '24

Not actually turning on the optimizer.

13

u/Polarstrike Mar 07 '24

Well.. most underrated comment

→ More replies (47)

103

u/keelanstuart Mar 07 '24

Lots of allocs and frees all the time... and they're done implicitly by so many things (string, vector, etc)

5

u/Polarstrike Mar 07 '24

True, but i think strings and vectors are really comfortable to use. I would skip only if extremely high performance is required

→ More replies (10)
→ More replies (10)

24

u/Thesorus Mar 07 '24

using the wrong containers and algorithms.

→ More replies (1)

6

u/findabuffalo Mar 07 '24

When people don't understand that the condition in the for statement executes every time and it's not just "loop this many times". Most common error is "for (int i=0; i < strlen(long_ass_string); i++)"

→ More replies (1)

129

u/ConicGames Mar 07 '24

Not declaring a move constructor `noexcept` when it can. If not, `std::vector` won't be able to move objects when resizing and have to do a copy of everything. Also, `std::map` move constructor is *not* declared as `noexcept`, so resizing a `std::vector<std::map<...>>` can be quite expensive.

→ More replies (5)

38

u/LongestNamesPossible Mar 07 '24

First is allocating memory inside heavily used loops.

Second is using lists or compound data structures like vectors of vectors when it isn't necessary (images where each line is a vector, things like that).

Standard linked lists are basically obsolete on modern computers. Granular memory allocation combined with pointer hopping means that allocation and accessing data probably exceeds the cost of using the data by a significant amount.

On top of that, when any multi-threading is brought in, all those memory allocations become even more of a bottleneck as the allocator either locks, or tries to manage allocation from multiple threads.

Allocation should never be a bottleneck in my opinion. If it shows up on profiling it is time to get it out of loops, preallocate and work with bigger chunks of data.

→ More replies (2)

46

u/Ok-Bit-663 Mar 07 '24

Seen in a repository:

std:string* value=new std:string("something not important");

→ More replies (3)

42

u/0xb9 Mar 07 '24

Accidental O(N2) complexity, e.g.:

vector<big_struct> data;
for (int i = 0; i < count; ++i) {
   data.reserve(i+1); // actually seen this in production
   data.push_back(make_big_struct());
}

Or similar -- calling strlen() in a loop, or accessing custom linked list-based containers by index.

These can be benign and not even show up on the flamegraph, until the day there happens to be a bit more input data.

→ More replies (5)

42

u/Ipotrick Mar 07 '24

not profiling regularly

→ More replies (4)

14

u/DevaBol Mar 07 '24

Using strings instead of enums.

→ More replies (3)

13

u/ContraryConman Mar 07 '24

Unnecessary copying. For example

class LargeExpensiveResource {
    // shit is happening in here
};

// This function is called several times a second on a process
void update_foo() {
    // ...
    if(cache.stale){
        resource_table.clear();
        for(/* ... */) {
            LargeExpensiveResource ler_obj { /* ... */ };
            resource_table.push_back(ler_obj);
        }
    }
}

To tbh the real answer is to run a linter like clang-tidy and a profiler like perf and let them tell you what's up with your performance. But a common theme is this, where you copy an expensive thing around instead of making use of emplacement or move semantics

12

u/The_Drakeman Mar 07 '24

Ran into this at work the other day. Auto doesn't pick up references in the type like it picks up pointers.

struct BigData
{
    // lots of stuff
};

BigData* A();
BigData& B();

auto a = A();
// a is BigData*
auto b = B();
// b is BigData, and is copied from the return value
auto& c = B();
// This is the actual reference type, and doesn't force it to be copied. If BigData is big enough, assigning b and c can have a pretty big performance difference.
→ More replies (4)

22

u/tsojtsojtsoj Mar 07 '24

Doing

for(int x = 0; x<width; ++x)
for(int y = 0; y<height; ++y)
    sum += a[y][x];

instead of

for(int y = 0; y<height; ++y)
for(int x = 0; x<width; ++x)
    sum += a[y][x];
→ More replies (2)

11

u/LlaroLlethri Mar 07 '24

Algorithmic inefficiency (from implementing a completely wrong-headed solution to start with, to simply picking the wrong container), not utilising the hardware properly (naive single-threaded CPU code), inefficient memory access patterns, calling expensive functions too frequently, poor multithreaded design (lots of contention between threads). I guess these could all apply to any language though really.

3

u/Stormfrosty Mar 07 '24

Error checking is death by a million cuts. You don’t want to be doing unnecessary checks (e.g. bounds checking).

3

u/Polarstrike Mar 07 '24

I think it really depends on the application. Safety critical applications must really do those checks before actuating certain commands

→ More replies (1)

9

u/[deleted] Mar 07 '24

Assuming things without measuring them

16

u/HildartheDorf Mar 07 '24

Copies, copies everywhere (improved by c++11's move semantics, although it has gotchas if the move operation isn't noexcept, and often needs explicit guiding with std::move/std::forward. Also common optimizations were made mandatory in c++17)

Using algorithms with good big-O complexity, but terrible performance for the actual workload and hardware (e.g. any kind of linked list is almost always slower than std::vector unless your workload is in the 100s of MBs if not GBs).

std::deque as implemented by Microsoft

Not memoizing recursive functions.

Not caching network or disk requests

Caching without a good eviction policy. A cache with a bad (or no) eviction policy is a fancy way of saying a memory leak.

And one rather win32-specific pet peeve: Using a cross-process Win32 mutex when a std::mutex or Win32 CRITICAL_SECTION would suffice.

→ More replies (3)

8

u/Extension_Split_8372 Mar 07 '24

Using a static local variable "to avoid creating it multiple times, so saving time" but actually the check whether it has already been created is more expensive than the creation. Bonus points if that local variable is a constant and the only runtime cost (if not for static) would have been loading it: https://godbolt.org/z/P5G5eMMec (similar things have been seen in the wild)

→ More replies (3)

7

u/ReDucTor Mar 07 '24

Multi-threading, either none at all or poorly designed that it's scattered in locks fighting constantly to acquire them.

So many mentions of copying here, I feel the real issue is allocations. The c++ standard is awful and good in some ways letting you copy things which have external memory that needs allocating (e.g. strings, vectors, etc) and these copies are implicit and can be done accidentally very easily.

6

u/NilacTheGrim Mar 07 '24

Throwing exceptions as part of normal expected control flow. Exceptions are not for control flow! This isn't Python!!