python / python/cpython

Move type checks from switch cases to beginning of `_Py_Specialize_BinaryOp`

Open
#128,310 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

_Py_Specialize_BinaryOp: https://github.com/python/cpython/blob/aeb9b65aa26444529e4adc7d6e5b0d3dd9889ec2/Python/specialize.c#L2380-L2443
uses

if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { 
    break; 
} 

check inside every switch case which is redundant. Placing this check before switch results in same performance but less generated code. Compiler with -O3 option seems to not recognize it. Attaching generated machine code (clang, apple silicon) for old and new versions
new.txt
old.txt

Feel free to close this if not worth it.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-128311

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Check gh-128311 first, since the issue links work already in progress. Then read Python/specialize.c at _Py_Specialize_BinaryOp and compare the attached old.txt and new.txt compiler output; done means the redundant checks are handled consistently and the generated code has the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers, performance
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.