Optimizing branch predictions: how to generalize code that could run wth different compiler, interperter, and hardware prediction?
<p>I ran into some slow downs on a tight loop today caused by an If statement, which surprised me some because I expected branch prediction to successfully pipeline the particular statement to minimize the cost of the conditional.</p>

<p>When I sat down to think more about why it wasn't better handled I realized I didn't know much about how branch prediction was being handled at all.  I know the <em>concept</em> of branch prediction quite well and it's benefits, but the problem is that I didn't know who was implementing it and what approach they were utilizing for predicting the outcome of a conditional.</p>

<p>Looking deeper I know branch prediction can be done at a few levels:</p>

<ol>
<li>Hardware itself with instruction pipelining</li>
<li>C++ style compiler</li>
<li>Interpreter of interpreted language.</li>
<li>half-compiled language like java may do two and three above.</li>
</ol>

<p>However, because optimization can be done in many areas I'm left uncertain as to how to anticipate branch prediction.  If I'm writing in Java, for example, is my conditional optimized when compiled, when interpreted, or by the hardware after interpretation!?  More interesting, does this mean if someone uses a different runtime enviroment?  Could a different branch prediction algorithm used in a different interpreter result in a tight loop based around a conditional showing significant different performance depending on which interpreter it's run with?</p>

<p>Thus my question, how does one generalize an optimization around branch prediction if the software could be run on very different computers which may mean different branch prediction?  If the hardware and interpreter could change their approach then profiling and using whichever approach proved fastest isn't a guarantee.  Lets ignore C++ where you have compile level ability to force this, looking at the interpreted languages if someone still needed to optimize a tight loop within them.</p>

<p>Are there certain presumptions that are generally safe to make regardless of interpreter used?  Does one have to dive into the intricate specification of a language to make any meaningful presumption about branch prediction?</p>