Pipelined processors do not like conditional branches because when conditional branch is entering in the pipeline, the processor does not know yet what will be the condition outcome but it needs to continue to fill the pipeline with new instructions. In that situation the processor will try to predict what will be the condition outcome and start filling the pipeline from its prediction. This is very costly performance wise when the processor mispredicts the condition outcome. When that happens, the processor has to flush the pipeline content and start filling it again from the branch point.
The conditional branch prediction module has 2 components. There is the branch outcome cache that will help the processor to remember what was the outcome of branches that it has encountered recently and a static branch prediction algorithm.
The static branch algorithm is very simple:
If you read the Intel processor optimization guide, few simple guidelines are easy to apply:
1. Eliminate branches
Some conditional branches are simply useless as whether they are taken or not, it will not make any differences on the end result. 2 examples of these are:
if( i != 0 ) i = 0;
or
if( ptr != NULL ) { delete ptr; ptr = NULL; }
should be changed to:
i = 0;
and
delete ptr;
ptr = NULL;
It is faster to reassign the same value to a variable than conditionally assign it. The operator delete
and the function free()
already contain safeguards against NULL
pointers and it should be quite exceptional that they are called anyway with NULL
pointers.
2. Refactor if statement conditions to be true most of the time.
Here is an example:
if(error) { // exceptional error handling } else { // normal operation }
would be faster written like:
if(!error) { // normal operation } else { // exceptional error handling }
To conclude, these guidelines are specifically for Intel processors but since the static branch prediction algorithm used by Intel processors is widely used by other pipelined processors (PowerPC is one of them), applying them on other platforms should be beneficial as well. Also, if you are looking for other optimization techniques, you can consult these sources.
Comments are closed for this post.
I want you to find in this blog informations about C++ programming that I had a hard time to find in the first place on the web.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
<< < | > >> | |||||
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |