Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 4, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Dec 4, 2023

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
@AlexWaygood
Copy link
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 AlexWaygood merged commit c718ab9 into python:main Dec 4, 2023
32 checks passed
@AlexWaygood 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)
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.

None yet

3 participants