r/cpp • u/pavel_v • Oct 21 '25
When Compiler Optimizations Hurt Performance
https://nemanjatrifunovic.substack.com/p/when-compiler-optimizations-hurt
68
Upvotes
r/cpp • u/pavel_v • Oct 21 '25
View all comments
50
u/ReDucTor Game Developer | quiz.cpp-perf.com Oct 21 '25
I feel a more intuitive way to understand low level performance like this is to think of the backend part of the CPU as a data dependency graph, where parts of keep getting added on many of them are just predictions of what will be added and when it gets it wrong then it needs to throw them away and start again.
So let's assume that your using the jump table version in a loop like
Assuming it's mostly inlined and the jump table was even more simplified down to
If you look at each
pit's dependent on the data being loadedseq_size[*p]which is dependent on the data loaded from*pso it becomesBut this is just a single iteration, the next iteration depends on the previous so it's more like
If you keep going, it's just one really long line if individual loads to determine where to do the next load, there leaved limited room for out of order execution, even though they are all things that should be in the L1 cache due to previous usage and hardware prefetching.
However if you compare it to something like
Where the majority of cases will hit the
*p < 127then the branch predictor will end up just queueingp += 1and I'm sure you can work out what the data dependency graph for it will look like (the immediate1)