You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently to check whether a value is True or False we need to compare it either True or False.
In the both the JIT and the interpreter (although this is worse in the JIT) we need to load a full 8 byte value from the instruction stream in order to perform the comparison.
Here's the x86 stencil for _GUARD_IS_TRUE_POP (with TOS caching):
struct_booleans {
PyLongObjectFalse;
PyLongObjectTrue;
};
alignas(sizeof(struct_booleans)) struct_booleans_PyBooleans= {
/* Data for False *//* Data for True */
};
we can use the alignment to check for True or False by testing a single bit: ref.bits & sizeof(PyLongObject), resulting in this very efficient stencil for _GUARD_IS_TRUE_POP:
It doesn't matter which bit differs, as long as some bit differs (and they must).
We just need to compute the bit at runtime.
This means we can't do this optimization in the interpreter efficiently (yet), but we can still do it in the JIT
Activity
CarlosEduR commentedon Aug 29, 2025
markshannon commentedon Dec 12, 2025
It looks like
_Py_FalseStructand_Py_TrueStructare part of the stable ABI until 3.13.So it will be tricky do this without an ABI break.
markshannon commentedon Jan 8, 2026
It doesn't matter which bit differs, as long as some bit differs (and they must).
We just need to compute the bit at runtime.
This means we can't do this optimization in the interpreter efficiently (yet), but we can still do it in the JIT
GH-138245: Perform boolean guards by testing a single bit, rather tha…
pythonGH-138245: Perform boolean guards by testing a single bit, rath…