Title: gh-74690: Micro-optimise `typing._get_protocol_attrs` by AlexWaygood · Pull Request #103152 · python/cpython · GitHub
Open Graph Title: gh-74690: Micro-optimise `typing._get_protocol_attrs` by AlexWaygood · Pull Request #103152 · python/cpython
X Title: gh-74690: Micro-optimise `typing._get_protocol_attrs` by AlexWaygood · Pull Request #103152 · python/cpython
Description: This PR is a bunch of micro-optimisations for typing._get_protocol_attrs that together have a significant impact on the speed of isinstance checks against runtime-checkable protocols. Benchmark: import time from typing import Protocol, runtime_checkable @runtime_checkable class HasX(Protocol): x: int class Foo: @property def x(self) -> int: return 42 class Bar: x = 42 class Baz: def __init__(self): self.x = 42 class Egg: ... class Nominal(HasX): def __init__(self): self.x = 42 class Registered: ... HasX.register(Registered) num_instances = 500_000 foos = [Foo() for _ in range(num_instances)] bars = [Bar() for _ in range(num_instances)] bazzes = [Baz() for _ in range(num_instances)] basket = [Egg() for _ in range(num_instances)] nominals = [Nominal() for _ in range(num_instances)] registereds = [Registered() for _ in range(num_instances)] def bench(objs, title): start_time = time.perf_counter() for obj in objs: isinstance(obj, HasX) elapsed = time.perf_counter() - start_time print(f"{title}: {elapsed:.2f}") bench(foos, "Time taken for objects with a property") bench(bars, "Time taken for objects with a classvar") bench(bazzes, "Time taken for objects with an instance var") bench(basket, "Time taken for objects with no var") bench(nominals, "Time taken for nominal subclass instances") bench(registereds, "Time taken for registered subclass instances") Results on dfc4c95: Time taken for objects with a property: 2.17 Time taken for objects with a classvar: 2.10 Time taken for objects with an instance var: 2.13 Time taken for objects with no var: 2.47 Time taken for nominal subclass instances: 2.20 Time taken for registered subclass instances: 4.72 Results with this PR: Time taken for objects with a property: 1.65 Time taken for objects with a classvar: 1.60 Time taken for objects with an instance var: 1.61 Time taken for objects with no var: 1.89 Time taken for nominal subclass instances: 1.61 Time taken for registered subclass instances: 3.79 I'm once again skipping NEWS, since #103034 will counteract the boost to performance here, so the overall impact on performance for runtime-checkable protocols in 3.12 is still TBD. Issue: gh-74690
Open Graph Description: This PR is a bunch of micro-optimisations for typing._get_protocol_attrs that together have a significant impact on the speed of isinstance checks against runtime-checkable protocols. Benchmark: i...
X Description: This PR is a bunch of micro-optimisations for typing._get_protocol_attrs that together have a significant impact on the speed of isinstance checks against runtime-checkable protocols. Benchmark: i...
Opengraph URL: https://github.com/python/cpython/pull/103152
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:9f4f4418-fb1c-faee-83bb-db79e0f5056a |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B1AA:13E08:9D4D8:DEBB7:696A46C7 |
| html-safe-nonce | 6556be5bcabcbae454ca8df58f0704082c3a35e28fcf3d028564dda262cb5732 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUFBOjEzRTA4OjlENEQ4OkRFQkI3OjY5NkE0NkM3IiwidmlzaXRvcl9pZCI6Ijc3NjgxNDYwMDUzMDIzMzEwNzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9cf1a13cb935571dfd1bc10abbdcddd5d733a7e8485da75764528972ca7f3723 |
| hovercard-subject-tag | pull_request:1298176202 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/103152/files |
| twitter:image | https://avatars.githubusercontent.com/u/66076021?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/66076021?s=400&v=4 |
| og:image:alt | This PR is a bunch of micro-optimisations for typing._get_protocol_attrs that together have a significant impact on the speed of isinstance checks against runtime-checkable protocols. Benchmark: i... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3f871c8e07f0ae1886fa8dac284166d28b09ad5bada6476fc10b674e489788ef |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 63c426b30d262aba269ef14c40e3c817b384cd61 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width