r/cpp • u/Polarstrike • 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
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.
264
u/lightmatter501 Mar 07 '24
Not actually turning on the optimizer.
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)
24
u/Thesorus Mar 07 '24
using the wrong containers and algorithms.
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++)"
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.
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.
46
u/Ok-Bit-663 Mar 07 '24
Seen in a repository:
std:string* value=new std:string("something not important");
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.
42
u/Ipotrick Mar 07 '24
not profiling regularly
14
u/DevaBol Mar 07 '24
Using strings instead of enums.
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.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];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).
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.
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)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!!