Title: gh-102213: Optimizing the Performance of __getattr__ #102213 by wangxiang-hz · Pull Request #102248 · python/cpython · GitHub
Open Graph Title: gh-102213: Optimizing the Performance of __getattr__ #102213 by wangxiang-hz · Pull Request #102248 · python/cpython
X Title: gh-102213: Optimizing the Performance of __getattr__ #102213 by wangxiang-hz · Pull Request #102248 · python/cpython
Description: when __getattr__ is defined, python with try to find an attribute using _PyObject_GenericGetAttrWithDict. Find nothing is reasonable so we don't need an exception, it will hurt performance. Using this test code: import time import sys NUM = 1000000 class AttrObj(object): def __init__(self) -> None: super(AttrObj, self).__init__() self.pps = 2 def __getattr__(self, name): return 4 def main(): start = time.time() for i in range(1, NUM): pass end = time.time() peer = end - start o = AttrObj() print(f"Python version of {sys.version}") start = time.time() for i in range(1, NUM): s = o.ppp end = time.time() print(f"Call __getattr__ spend time: {end - start - peer}") if __name__ == "__main__": main() Now the result is: Call __getattr__ spend time: 0.4704132080078125 After this modification, the result is: Call __getattr__ spend time: 0.07422256469726562 Issue: gh-102213
Open Graph Description: when __getattr__ is defined, python with try to find an attribute using _PyObject_GenericGetAttrWithDict. Find nothing is reasonable so we don't need an exception, it will hurt performance. Usi...
X Description: when __getattr__ is defined, python with try to find an attribute using _PyObject_GenericGetAttrWithDict. Find nothing is reasonable so we don't need an exception, it will hurt performance....
Opengraph URL: https://github.com/python/cpython/pull/102248
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:13f1ddc8-c631-1b9b-b09e-b477d99111f6 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 900E:D22DC:7508D:A3282:6969F741 |
| html-safe-nonce | 592da28c104a682fbc773896d64c82d6281063d89d7c60fb78edc26de6026c84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDBFOkQyMkRDOjc1MDhEOkEzMjgyOjY5NjlGNzQxIiwidmlzaXRvcl9pZCI6IjcwMDM1MDU1MTI5NDIyNzAyNzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4f1c93386affe2b2fb94998d424ba461e97484d6b3fd441d8a86c9a7aaa7535e |
| hovercard-subject-tag | pull_request:1254247608 |
| 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/102248/files |
| twitter:image | https://avatars.githubusercontent.com/u/34048878?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/34048878?s=400&v=4 |
| og:image:alt | when __getattr__ is defined, python with try to find an attribute using _PyObject_GenericGetAttrWithDict. Find nothing is reasonable so we don't need an exception, it will hurt performance. Usi... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width