Skip to content

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
@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)
@namka279

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