Skip to content

Use alignment "tricks" to speed up core operations on booleans #142584

@markshannon

Description

@markshannon

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions