Skip to content

Faster True/False checks using alignment #138245

Closed

Description

@markshannon

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):

    // 0: 48 b8 00 00 00 00 00 00 00 00 movabsq $0x0, %rax
    // 0000000000000002:  R_X86_64_64  _Py_TrueStruct+0x1
    // a: 4c 39 f8                      cmpq    %r15, %rax
    // d: 0f 85 00 00 00 00             jne     exit

By putting True and False in an aligned array:

struct _booleans {
     PyLongObject False;
     PyLongObject True;
};

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:

    // 0: 41 f6 c7 20                   testb   $0x20, %r15b
    // 4: 0f 84 00 00 00 00             je      exit

Linked PRs

Activity

  1. added
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.15bugs and security fixes
    on Aug 29, 2025
  2. added
    type-featureA feature request or enhancement
    and removed
    3.15bugs and security fixes
    on Aug 29, 2025
  3. CarlosEduR commented on Aug 29, 2025

    @CarlosEduR
  4. markshannon commented on Dec 12, 2025

    @markshannon
    MemberAuthor

    It looks like _Py_FalseStruct and _Py_TrueStruct are part of the stable ABI until 3.13.
    So it will be tricky do this without an ABI break.

  5. markshannon commented on Jan 8, 2026

    @markshannon
    MemberAuthor

    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

  6. added a commit that references this issue on Jan 21, 2026
  7. added a commit that references this issue on Feb 15, 2026
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions