Title: gh-92216: improve performance of hasattr for type objects by eendebakpt · Pull Request #99979 · python/cpython · GitHub
Open Graph Title: gh-92216: improve performance of hasattr for type objects by eendebakpt · Pull Request #99979 · python/cpython
X Title: gh-92216: improve performance of hasattr for type objects by eendebakpt · Pull Request #99979 · python/cpython
Description: This PR improves performance of hasattr for type objects in the case the attribute is missing. The optimization is similar to the one used for objects which have PyObject_GenericGetAttr as the attribute lookup method (see specialization for PyObject_GenericGetAttr in _PyObject_LookupAttr and suppress in _PyObject_GenericGetAttrWithDict ) Benchmark: import pyperf runner = pyperf.Runner() setup=""" from dataclasses import dataclass, asdict @dataclass class D: a : dict b: list d=D({'1': 1}, [1,2,3]) asdict(d) """ runner.timeit(name=f"micro benchmark", stmt=f"hasattr(o, '__str__')", setup='import os; o=type(os)') runner.timeit(name=f"micro benchmark: misssing attr", stmt=f"hasattr(o, '_no_such_attr')", setup='import os; o=type(os)') runner.timeit(name=f"asdict on dataclass", stmt=f"asdict(d)", setup=setup) Results: micro benchmark: Mean +- std dev: [main] 42.1 ns +- 1.1 ns -> [pr] 44.1 ns +- 1.8 ns: 1.05x slower micro benchmark: misssing attr: Mean +- std dev: [main] 256 ns +- 6 ns -> [pr] 37.6 ns +- 0.7 ns: 6.80x faster asdict on dataclass: Mean +- std dev: [main] 8.87 us +- 0.17 us -> [pr] 6.61 us +- 0.05 us: 1.34x faster Geometric mean: 2.06x faster Issue: gh-92216
Open Graph Description: This PR improves performance of hasattr for type objects in the case the attribute is missing. The optimization is similar to the one used for objects which have PyObject_GenericGetAttr as the attr...
X Description: This PR improves performance of hasattr for type objects in the case the attribute is missing. The optimization is similar to the one used for objects which have PyObject_GenericGetAttr as the attr...
Opengraph URL: https://github.com/python/cpython/pull/99979
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:531e0871-4491-0a19-f830-767cfb983baa |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A57C:3FD47:725A3:9A6FE:696951A5 |
| html-safe-nonce | 7b03a97a14461d48351a1a53fa8958f887e95226ce2e6431dc35e9c449b1d6d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTdDOjNGRDQ3OjcyNUEzOjlBNkZFOjY5Njk1MUE1IiwidmlzaXRvcl9pZCI6IjM4NzM0MDE1NjQ1ODI5OTAyNDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 02554d37b94f13c6da14c60d38079c27f984906278c9c89ff07c68b20577660b |
| hovercard-subject-tag | pull_request:1144513999 |
| 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/99979/files |
| twitter:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| og:image:alt | This PR improves performance of hasattr for type objects in the case the attribute is missing. The optimization is similar to the one used for objects which have PyObject_GenericGetAttr as the attr... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 54182691a21263b584d2e600b758e081b0ff1d10ffc0d2eefa51cf754b43b51d |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d69ac0477df0f87da03b8b06cebd187012d7a930 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width