What is branchless programming? A branch occurs as soon we are using statements like if, switches, conditional operator, etc. where the CPU has more than one path it can take. For example, if there is an if statement, the CPU prepares (or better guesses) for all possible branches the code might take. By doing that, it fills up unnecessarily memory space for ones, and after the execution of the given statement, the CPU needs to flush the not used path instructions. And that process is slow.

Take the following example:

def Smaller(a, b):
    if a < b:
        return a
        return b

Not that there is anything wrong with that, and it is a fairly normal function to write. Here is the same function just as branchless:

def Smaller_Branchless(a, b):
    return a * (a < b) + b * (b <= a)

In branchless programming, we resort to using conditional operators, i.e., <. In the very simple example above, the conditional parts return either 0 or 1, and therefore, the value of either a or b is multiplied by that value.

For example, a = 7 and b = 5, and when we plug in the values, we get:

return 7 * (7 < 5) + 5 * (5 <= 7)

which gives us

return 7 * 0 + 5 * 1

returning the value 5.

Why is that important?

In “normal” day to day scripting, it is not at all critical. But in heavy computational programming, like for gaming or ML, it becomes an important aspect very quickly. Especially when your CPU and memory resources are limited.

That is to say that if we compile our code, the compiler tries its best to do the right thing. But that is a whole different topic.