Skip to content

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

Closed as duplicate

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)

Activity

  1. added
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.15bugs and security fixes
    on Dec 11, 2025
  2. sergey-miryanov commented on Dec 11, 2025

    @sergey-miryanov
    Contributor

    Is this a duplicate of the #138245?

  3. markshannon commented on Dec 12, 2025

    @markshannon
    MemberAuthor

    Yes, it is. Your ability to search GitHUb is clearly superior to mine 🙂

  4. sergey-miryanov commented on Dec 12, 2025

    @sergey-miryanov
    Contributor

    It's on my favorites list :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs 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