Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Branchy code where the branches are statically known to be very unlikely ought to be practically as fast as the branch-free equivalent. You're probably forgoing a few optimization opportunities since you do have to build a proper exception record (and perhaps a stack trace) if the code errors out but other than that, it just doesn't matter that much.


Branchy checks for error codes -- the alternative to using exceptions -- are not statically predictable. Each such branch is dynamically predictable, but burns one of a strictly limited number of available branch prediction slots.

So, while the successfully predicted branches are pretty cheap, many fewer can be predicted. Synthetic benchmarks generally have many fewer branches to predict than live code has.


Static branch prediction isn't found on any major architectures, right?

You can optimize the code generation around that knowledge but not passing it directly to the CPU


It is absolutely done, it's just implicit rather than explicit. The simplest heuristic is to predict backwards branches as taken (loops) and forward branches as not taken, and it works quite well (though I think it might be more sophisticated than that).


Of course the error-code checking branches will most usually be forward branches, thus not predicted correctly the first time through, or after spilling.


> Static branch prediction isn't found on any major architectures, right?

All major AMD and Intel processors do static branch prediction.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: