It would be great to be able to tag an IF as a likely branch -- perhaps LikelyBranchingIf -- causing the compiler to emit the static-branch-likely-hint prefix (0x3E) to the conditional emitted in the assembly code. Since there is a work-around (albetit rather ugly), this is a low-priority wish.
The topic of branch misprediction was first discussed at https://software.intel.com/en-us/articl ... ispredicts
PureBasic does a great job of generating code with branches ordered as written, so the code following an IF is the "likely" target and the code following the corresponding ELSE is the "unlikely" target. This is an ideal match for the branch prediction algorithm used by Intel processors. However, there currently doesn't seem to be a graceful way to insert a branch-likely hint for a (fully-embedded) IF block as a "likely" branch.
If (MyVar > 0) ;BRANCH UNLIKELY; Will be correctly predicted.
mRefCtr = mRefCtr + 1
If (mRefCtr = 1) ;BRANCH LIKELY; Will be mispredicted.
{do some initialization stuff}
EndIf
{do more stuff}
Else ;Target of Unlikely branch above
{do stuff for condition MyVar <= 0}
EndIf
Obviously, one way to rewrite the code above so that the branch is correctly predicted is to use the very ugly form:
If (MyVar > 0)
mRefCtr = mRefCtr + 1
If (mRefCtr <> 1)
ComeBackHere:
{do more stuff}
else
{do some initialization stuff}
goto ComeBackHere
endif
else
{do stuff for condition MyVar <= 0}
endif
Continuing the example, the wish is for something simple like:
If (MyVar > 0)
mRefCtr = mRefCtr + 1
LikelyBranchingIf (mRefCtr = 1)
{do some initialization stuff}
endif
{do more stuff}
else
{do stuff for condition MyVar <= 0}
endif