Title: Performance of attribute lookup for type objects · Issue #92216 · python/cpython · GitHub
Open Graph Title: Performance of attribute lookup for type objects · Issue #92216 · python/cpython
X Title: Performance of attribute lookup for type objects · Issue #92216 · python/cpython
Description: Bug report The performance of attribute lookup for type objects is worse than for other objects. A benchmark import pyperf runner=pyperf.Runner() setup=""" class Class: def all(self): pass x=Class() """ runner.timeit('hasattr x.all', "ha...
Open Graph Description: Bug report The performance of attribute lookup for type objects is worse than for other objects. A benchmark import pyperf runner=pyperf.Runner() setup=""" class Class: def all(self): pass x=Class(...
X Description: Bug report The performance of attribute lookup for type objects is worse than for other objects. A benchmark import pyperf runner=pyperf.Runner() setup=""" class Class: def all(self)...
Opengraph URL: https://github.com/python/cpython/issues/92216
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Performance of attribute lookup for type objects","articleBody":"\u003c!--\r\n If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not\r\n the right place to seek help. Consider the following options instead:\r\n\r\n - reading the Python tutorial: https://docs.python.org/3/tutorial/\r\n - posting in the \"Users\" category on discuss.python.org: https://discuss.python.org/c/users/7\r\n - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list\r\n - searching our issue tracker (https://github.com/python/cpython/issues) to see if\r\n your problem has already been reported\r\n--\u003e\r\n\r\n**Bug report**\r\n\r\nThe performance of attribute lookup for type objects is worse than for other objects. A benchmark\r\n```\r\nimport pyperf\r\nrunner=pyperf.Runner()\r\n\r\nsetup=\"\"\"\r\nclass Class:\r\n def all(self):\r\n pass\r\n\r\nx=Class()\r\n\"\"\"\r\n\r\nrunner.timeit('hasattr x.all', \"hasattr(x, 'all')\", setup=setup)\r\nrunner.timeit('hasattr x.__array_ufunc__', \"hasattr(x, '__array_ufunc__')\", setup=setup)\r\nrunner.timeit('hasattr Class.all', \"hasattr(Class, 'all')\", setup=setup)\r\nrunner.timeit('hasattr Class.__array_ufunc__', \"hasattr(Class, '__array_ufunc__')\", setup=setup) # worse performance\r\n```\r\nResults:\r\n```\r\nhasattr x.all: Mean +- std dev: 68.1 ns +- 1.1 ns\r\nhasattr x.__array_ufunc__: Mean +- std dev: 40.4 ns +- 0.3 ns\r\nhasattr Class.all: Mean +- std dev: 38.1 ns +- 0.6 ns\r\nhasattr Class.__array_ufunc__: Mean +- std dev: 255 ns +- 2 ns\r\n```\r\n\r\nThe reason seems to be that the [`type_getattro`](https://github.com/python/cpython/blob/364ed9409269fb321dc4eafdea677c09a4bc0d8d/Objects/typeobject.c#L3902) always executes `PyErr_Format`, wheras for the \"normal\" attribute lookup this is avoided (see [here](https://github.com/python/cpython/blob/364ed9409269fb321dc4eafdea677c09a4bc0d8d/Objects/object.c#L952) and [here](https://github.com/python/cpython/blob/364ed9409269fb321dc4eafdea677c09a4bc0d8d/Objects/object.c#L1348))\r\n\r\nNotes:\r\n* The benchmark is from the python side, but we are working with the C-API\r\n* The performance is important for numpy, see https://github.com/numpy/numpy/pull/21423\r\n* Another location where this is a bottleneck: https://github.com/python/cpython/blob/v3.12.0a2/Lib/dataclasses.py#L1301\r\n\r\n**Your environment**\r\n\r\n\u003c!-- Include as many relevant details as possible about the environment you experienced the bug in --\u003e\r\n\r\n- CPython versions tested on: Python 3.11.0a7+\r\n- Operating system and architecture: Linux Ubuntu\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-99977\r\n* gh-99979\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2022-05-03T06:44:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/92216/cpython/issues/92216"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:67428e50-5ef1-8f9b-3afa-6d9a7d9cb269 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C706:A8104:14E9311:1DBCAF7:69691B4B |
| html-safe-nonce | f066b944ed3d340579daf916c87335765fa6e8c598f78dca59305be738c0d70b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzA2OkE4MTA0OjE0RTkzMTE6MURCQ0FGNzo2OTY5MUI0QiIsInZpc2l0b3JfaWQiOiI2NjU3MDAxNjIxODEzNzk1NjU5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | caa0ace29f86ea5d06cd5c97eb660d637c41981549ff56045b5d3b66ceecc005 |
| hovercard-subject-tag | issue:1223744607 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/python/cpython/92216/issue_layout |
| twitter:image | https://opengraph.githubassets.com/47fd5baeeb4c185ce891d4ef9cf67130ea875f30b41cbd1a29fae97bc001743b/python/cpython/issues/92216 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/47fd5baeeb4c185ce891d4ef9cf67130ea875f30b41cbd1a29fae97bc001743b/python/cpython/issues/92216 |
| og:image:alt | Bug report The performance of attribute lookup for type objects is worse than for other objects. A benchmark import pyperf runner=pyperf.Runner() setup=""" class Class: def all(self): pass x=Class(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eendebakpt |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0e60568924309a021b51adabdce15c2a2f285b556f3130d1a2fa2a5bce11c55f |
| turbo-cache-control | no-preview |
| 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 | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | dd206f7ed6207863172be4a783826e86bd2375c3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width