Branching on a boolean value is a very common operation in the Python VM, as all values are converted to booleans before any branch.
Checking for the truth of a boolean variable in the VM requires either dereferencing the pointer to get at the value or comparing with a full 64 bit pointer.
If we allocate the booleans in an array and align that array by 2*sizeof(PyLongObject) then we can determine the truth of the pointer by simply inspecting a bit in the pointer.
Instead of
ptr == Py_True or
ptr->ob_digits[0]
we can use a simple bitwise test:
((uintptr_t)ptr) & sizeof(PyLongObject)
Branching on a boolean value is a very common operation in the Python VM, as all values are converted to booleans before any branch.
Checking for the truth of a boolean variable in the VM requires either dereferencing the pointer to get at the value or comparing with a full 64 bit pointer.
If we allocate the booleans in an array and align that array by
2*sizeof(PyLongObject)then we can determine the truth of the pointer by simply inspecting a bit in the pointer.Instead of
ptr == Py_Trueorptr->ob_digits[0]we can use a simple bitwise test:
((uintptr_t)ptr) & sizeof(PyLongObject)