Skip to content

Dev Notes: 2017_02_20

Dave Hudson edited this page Feb 20, 2017 · 3 revisions

Better Code Layout

We're starting to find it much harder to make things faster, but there's still some scope.

One of the things that we can do is guide the compiler in how best to arrange the code. By default the compiler uses heuristics to work out if particular branches are likely or unlikely. In general the compiler will rearrange the code so that the branch will "fall through" to the most likely instruction.

In older CPU designs such hinting could have huge performance advantages, but most modern CPUs incorporate hardware branch prediction that lets the CPU determine the most likely next instruction. This significantly mitigates the performance advantage of predicting branch outcomes, but there is still a small advantage from falling through. If we fall through then the CPU is likely to use the remaining instructions in the current cache line, as opposed to having to switch cache lines. A smaller cache footprint is definitely an advantage for performance in the long run.

gcc and clang both support a builtin compiler function called __builtin_expect that allows a caller to indicate whether an operation is likely or not. This function causes the compiler to ignore its default heuristics in favour of the annotation provided by the programmer.

See Also

Clone this wiki locally