I completely understand and respect your argument against branches in loops. I mean I've designed and built computer systems on the circuit level and understand machine cycles, energy consumption, registers and such, but don't you think that "branches in for loops are bad juju" is a bit excessive for the general case? If you're iterating over 300,000 objects, then I can see where you can shave off a substantial amount of time eliminating branches. However, with today's PC hardware standard, would looping through 10 or 15 items with branches make a difference you should care about?Speedwise, get rid of the conditionals in any way possible (even - or especially - if this means restructuring your data).
Branches in for loops are bad juju. They typically play very very poorly with branch predictors. Branch prediction is a _massively_ important part of how CPUs work; they're up there with cache access pattern in terms of "hardware crap you really need to understand and think about" when writing games (or any application, imo - even if raw speed isn't important to you, saving on energy costs is hugely important in mobile and server workloads)....
It isn't just the performance considerations. Pre-sorting objects to iterate through them without conditions simplifies the code, as well. You can look at a piece of code and know exactly where it's going to go just by looking at it. That (usually) makes it easier to see what a given piece of code is supposed to do in addition to what it does.
// this is annoying to read and debug - I have to step through the loop to see what kind of entity I'm on at any given type
for (auto& e : entities)
{
if (e.type == boop) { // ...
}
if (e.type == snoot){ // ...
}
}
// this is better
for (auto& e : boops)
{
// ...
}
for (auto& e : snoots)
{
// ...
}