Have I understood this right, if statements are more dependent on branch prediction and v-table look-up is more dependent on branch target prediction? Regarding v-tables, there is no "branch prediction", just the target prediction?
Trying to understand how a v-table is processed by the CPU.
Branch prediction is predicting whether or not the branch will be taken. Branch target prediction is prediction where the branch is going to. These two things are independent and can occur in all combinations.
Examples of these might be:
Unconditional branch, fixed target
- Infinite loop
gotostatementbreakorcontinuestatement- End of the 'then' clause of an
if/elsestatement (to jump past theelseclause) - Non-virtual function call
Unconditional branch, variable target
- Returning from a function
- Virtual function call
- Function pointer call
switchstatement (if compiled into a jump table)
Conditional branch, fixed target
ifstatementswitchstatement (if compiled into a series ofif/elsestatements)- Loop condition tests
- The
&&and||operators - The ternary
?:operator
Conditional branch, variable target
- Less likely to show up under normal conditions, but the compiler may synthesize one as an optimization, combining two of the above cases. For example, on x86, the compiler may optimize code like
if (condition) { obj->VirtualFunctionCall(); }into a conditional indirect jump likejne *%eaxif it appears at the end of a function due to tail call optimization.
来源:https://stackoverflow.com/questions/21608874/branch-prediction-vs-branch-target-prediction