Has Python got branch prediction?
Asked Answered
G

1

8

I implemented a physics simulation in Python (most of the heavy lifting is done in numerical libraries anyways, thus performance is good enough). Now that the project has grown a bit, I added extra functionality via parameters that do not change during the simulation. With that comes the necessity to have the program do one thing or another based on their values, i.e., quite a few if-else scattered around the code.

My question is simple: does Python implement some form of branch prediction? Am I going to wear the performance significantly or is the interpreter smart enough to see that some parameters never change? Having a constant if-else inside a function that is called a million times, is the conditional evaluated every time or some magic happens? When there is no easy way to remove the conditional altogether, is there a way to give the interpreter some hints and favour/emulate branch prediction?

Girondist answered 26/10, 2018 at 9:14 Comment(4)
"or is the interpreter smart enough to see that some parameters never change?" No, it will be checked every time.Bolt
I doubt a python interpreter would benefit much from branch prediction.Lara
If these parameters never change, maybe you can evaluate it once on a global level and pass the result as bool to the function?Heiress
You may be able to make use of numba for your function? Without more detail it's hard to say anything for sure.Bolt
L
4

You could in theory benefit here from some JIT functionality that may observe the control flow over time and could effectively suppress never-taken branches by rearranging the code. Some of the Python interpreters contain JIT compilers (I think PyPy does in newer versions, maybe Jython as well), and may be able to do this optimization, but that of course depends on the actual code.

However, the main form of branch prediction is done in HW, and is unrelated to the SW or language constructs used (in Python's case - quite a few levels of abstraction above). This mechanism eventually observes these conditional code paths as branches, and may be able to learn them if they are indeed statically determined. However, as any prediction mechanism, it has limited capacity, and since your code is supposed to be big, it may not be able to accommodate predictions for all these branches. It's still considered quite good, so chances are that the critical ones may work.

Finally, if you really want to optimize your code, you can convert some of these conditions to constants (assigning an argument a constant value instead of parsing the command line), or hiding the condition completely with something like __debug__. This way you won't have to worry about predicting them, but can restore the capability with minimal work if you need them in the future.

Lumberjack answered 29/10, 2018 at 7:41 Comment(2)
Don't HW branch predictor key off of PC, and wouldn't Python branches likely all share just a few or PC's in the compiled interpreter binary, meaning the hardware may not be able to predict branches as well as it isn't able to learn the behavior of the individual branches within the program?Garnish
Interesting, there's a paper on this that seems to indicate that it's not an issue in modern hardware hal.inria.fr/hal-01100647/documentGarnish

© 2022 - 2024 — McMap. All rights reserved.