← 返回首页
gh-74690: Optimise `isinstance()` and `issubclass()` calls against runtime-checkable protocols by avoiding costly `super()` calls by AlexWaygood · Pull Request #112708 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-74690: Optimise isinstance() and issubclass() calls against runtime-checkable protocols by avoiding costly super() calls#112708

Merged
AlexWaygood merged 3 commits into
python:mainfrom
AlexWaygood:protocol-costly-super-calls
Dec 4, 2023
Merged

gh-74690: Optimise isinstance() and issubclass() calls against runtime-checkable protocols by avoiding costly super() calls#112708
AlexWaygood merged 3 commits into
python:mainfrom
AlexWaygood:protocol-costly-super-calls

Conversation

AlexWaygood commented Dec 4, 2023
edited
Loading

Copy link
Copy Markdown
Member

Before:

>python -m timeit -s "from typing import SupportsIndex" "isinstance(0, SupportsIndex)" Running PGUpdate|x64 interpreter... 1000000 loops, best of 5: 379 nsec per loop >python -m timeit -s "from typing import SupportsIndex" "issubclass(int, SupportsIndex)" Running PGUpdate|x64 interpreter... 500000 loops, best of 5: 592 nsec per loop

After:

>python -m timeit -s "from typing import SupportsIndex" "isinstance(0, SupportsIndex)" Running PGUpdate|x64 interpreter... 1000000 loops, best of 5: 298 nsec per loop >python -m timeit -s "from typing import SupportsIndex" "issubclass(int, SupportsIndex)" Running PGUpdate|x64 interpreter... 500000 loops, best of 5: 523 nsec per loop

Timings done using a fresh PGO-optimised build on Windows with the main branch.

…nst runtime-checkable protocols by avoiding costly `super()` calls

Copy link
Copy Markdown
Member Author

For comparison, here's the timings against "normal" classes... we're still some way off 🙃 A lot closer than we were in 3.11, though.

>python -m timeit "isinstance(0, int)" Running PGUpdate|x64 interpreter... 10000000 loops, best of 5: 27.4 nsec per loop >python -m timeit "issubclass(str, int)" Running PGUpdate|x64 interpreter... 10000000 loops, best of 5: 34.6 nsec per loop

AlexWaygood merged commit c718ab9 into python:main Dec 4, 2023
AlexWaygood deleted the protocol-costly-super-calls branch December 4, 2023 15:41
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…nst runtime-checkable protocols by avoiding costly `super()` calls (python#112708)

This comment was marked as spam.

Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…nst runtime-checkable protocols by avoiding costly `super()` calls (python#112708)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage topic-typing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Footer

© 2026 GitHub, Inc.