Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: python/cpython
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: eendebakpt/cpython
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: match-class-perf
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 3 files changed
  • 2 contributors

Commits on Jun 29, 2026

  1. Speed up zero-argument class patterns in match statements

    A class pattern with no sub-patterns (`case C():`) is just an isinstance
    check, but it compiled to LOAD_COMMON_CONSTANT (empty names tuple) +
    MATCH_CLASS + COPY + POP_JUMP_IF_NONE + UNPACK_SEQUENCE 0, building and
    unpacking an empty attributes tuple every time.
    
    Compile it instead to a CALL_INTRINSIC_2 invoking a new
    INTRINSIC_MATCH_CLASS_ISINSTANCE, which performs the same PyType_Check
    and PyObject_IsInstance as _PyEval_MatchClass (including the identical
    TypeError when the pattern does not name a class) and pushes a bool.
    No new opcode is added.
    
    This is ~25% faster on a matching subject and ~14% faster on a
    non-matching subject for the zero-argument case, and is even faster than
    an equivalent isinstance() call since the intrinsic avoids a Python-level
    call frame.
    
    Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
    eendebakpt and claude committed Jun 29, 2026
    Configuration menu
    Copy the full SHA
    c6cd07f View commit details
    Browse the repository at this point in the history
Loading