René's URL Explorer Experiment


Title: Improve the Efficiency of Python3.11.1 __getattr__ · Issue #102213 · python/cpython · GitHub

Open Graph Title: Improve the Efficiency of Python3.11.1 __getattr__ · Issue #102213 · python/cpython

X Title: Improve the Efficiency of Python3.11.1 __getattr__ · Issue #102213 · python/cpython

Description: Feature or enhancement __getattr__ in Python3.11.1 is much slower than @Property and visiting an object's attribute. It's even slower than Python3.10.4. _PyObject_GenericGetAttrWithDict is the key reason. If Python3 fails finding an attr...

Open Graph Description: Feature or enhancement __getattr__ in Python3.11.1 is much slower than @Property and visiting an object's attribute. It's even slower than Python3.10.4. _PyObject_GenericGetAttrWithDict is the key ...

X Description: Feature or enhancement __getattr__ in Python3.11.1 is much slower than @Property and visiting an object's attribute. It's even slower than Python3.10.4. _PyObject_GenericGetAttrWithDict is ...

Opengraph URL: https://github.com/python/cpython/issues/102213

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve the Efficiency of Python3.11.1 __getattr__","articleBody":"# Feature or enhancement\r\n\r\n`__getattr__` in Python3.11.1 is much slower than `@Property` and visiting an object's attribute. It's even slower than Python3.10.4.\r\n`_PyObject_GenericGetAttrWithDict` is the key reason. If Python3 fails finding an attribute in normal ways, it will return NULL and raise an exception. But raising an exception has performance cost. Python3.11.1 add set_attribute_error_context to support `Fine Grained Error Locations in Tracebacks`. It makes things worser.\r\n\r\n# Pitch\r\n\r\nWe can use this test code:\r\n```py\r\nimport time\r\nimport sys\r\n\r\nclass A:\r\n    def foo(self):\r\n        print(\"Call A.foo!\")\r\n\r\n    def __getattr__(self, name):\r\n        return 2\r\n\r\n    @property\r\n    def ppp(self):\r\n        return 3\r\n\r\nclass B(A):\r\n    def foo(self):\r\n        print(\"Call B.foo!\")\r\n\r\n\r\nclass C(B):\r\n\r\n    def __init__(self) -\u003e None:\r\n        self.pps = 1\r\n\r\n    def foo(self):\r\n        print(\"Call C.foo!\")\r\n\r\n\r\ndef main():\r\n    start = time.time()\r\n    for i in range(1, 1000000):\r\n        pass\r\n    end = time.time()\r\n    peer = end - start\r\n    c = C()\r\n    print(f\"Python version of {sys.version}\")\r\n    start = time.time()\r\n    for i in range(1, 1000000):\r\n        s = c.pps\r\n    end = time.time()\r\n    print(f\"Normal getattr spend time: {end - start - peer}\")\r\n    start = time.time()\r\n    for i in range(1, 1000000):\r\n        s = c.ppa\r\n    end = time.time()\r\n    print(f\"Call __getattr__ spend time: {end - start - peer}\")\r\n    start = time.time()\r\n    for i in range(1, 1000000):\r\n        s = c.ppp\r\n    end = time.time()\r\n    print(f\"Call property spend time: {end - start - peer}\")\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n```\r\n\r\nThe result shows how slow `__getattr__` is:\r\n```\r\nPython version of 3.11.1 (main, Dec 26 2022, 16:32:50) [GCC 8.3.0]\r\nNormal getattr spend time: 0.03204226493835449\r\nCall __getattr__ spend time: 0.4767305850982666\r\nCall property spend time: 0.06345891952514648\r\n```\r\n\r\nWhen we define `__getattr__`, failed to find an attribute is what we expected. If we can get this result and then call `__getattr__` without exception handling, it will be faster.\r\nI tried to modify Python3.11.1 like this:\r\n1. add a new function in `object.c`:\r\n```c\r\nPyObject *\r\nPyObject_GenericTryGetAttr(PyObject *obj, PyObject *name)\r\n{\r\n    return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 1);\r\n}\r\n```\r\n\r\n2. change `typeobject.c` :\r\n```c\r\nif (getattribute == NULL ||\r\n        (Py_IS_TYPE(getattribute, \u0026PyWrapperDescr_Type) \u0026\u0026\r\n         ((PyWrapperDescrObject *)getattribute)-\u003ed_wrapped ==\r\n         (void *)PyObject_GenericGetAttr))\r\n        // res = PyObject_GenericGetAttr(self, name);\r\n        res = PyObject_GenericTryGetAttr(self, name);\r\n    else {\r\n        Py_INCREF(getattribute);\r\n        res = call_attribute(self, getattribute, name);\r\n        Py_DECREF(getattribute);\r\n    }\r\n    if (res == NULL) {\r\n        if (PyErr_ExceptionMatches(PyExc_AttributeError))\r\n            PyErr_Clear();\r\n        res = call_attribute(self, getattr, name);\r\n    }\r\n    Py_DECREF(getattr);\r\n    return res;\r\n```\r\n\r\nRebuild python, it really become faster: `spend time: 0.13772845268249512.`\r\n\r\n# Previous discussion\r\n\r\n\u003c!--\r\n  New features to Python should first be discussed elsewhere before creating issues on GitHub,\r\n  for example in the \"ideas\" category (https://discuss.python.org/c/ideas/6) of discuss.python.org,\r\n  or the python-ideas mailing list (https://mail.python.org/mailman3/lists/python-ideas.python.org/).\r\n  Use this space to post links to the places where you have already discussed this feature proposal:\r\n--\u003e\r\n[\r\n__getattr__ is much slower in Python3.11](https://discuss.python.org/t/getattr-is-much-slower-in-python3-11/24028)\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-102248\n* gh-103332\n* gh-103761\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/wangxiang-hz","@type":"Person","name":"wangxiang-hz"},"datePublished":"2023-02-24T12:23:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/102213/cpython/issues/102213"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b1f04b4a-2cf0-7b2b-9343-853c73b1faee
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCEE4:23CDC6:CB764:117B9A:696962E6
html-safe-nonce3d3118b0a8be82afa37125d0ea6bf0afa527cfbb140caa78a0a374fd3df27eef
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUU0OjIzQ0RDNjpDQjc2NDoxMTdCOUE6Njk2OTYyRTYiLCJ2aXNpdG9yX2lkIjoiMjc0Nzg2ODk0MDYwNTQ0NzQyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmaca1d45dc20ec5ad5fb3320eae118436d4e83aa77380fa2d34cbed35ce8eb74ca0
hovercard-subject-tagissue:1598539750
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/102213/issue_layout
twitter:imagehttps://opengraph.githubassets.com/e0a969fe368316cbe1a09cf94f03b48385c04d33589eb5bc10ff686e0e40e4b4/python/cpython/issues/102213
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/e0a969fe368316cbe1a09cf94f03b48385c04d33589eb5bc10ff686e0e40e4b4/python/cpython/issues/102213
og:image:altFeature or enhancement __getattr__ in Python3.11.1 is much slower than @Property and visiting an object's attribute. It's even slower than Python3.10.4. _PyObject_GenericGetAttrWithDict is the key ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamewangxiang-hz
hostnamegithub.com
expected-hostnamegithub.com
None48487c1ad776a7975b7132d95f4240ff3ae37cd5b8e3cb597102a4edb76738f1
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release669463fcc54773a88c1f5a44eef6b99a5504b9c7
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/102213#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F102213
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F102213
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/102213
Reloadhttps://github.com/python/cpython/issues/102213
Reloadhttps://github.com/python/cpython/issues/102213
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/102213
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/102213
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/102213
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/102213
Improve the Efficiency of Python3.11.1 __getattr__https://github.com/python/cpython/issues/102213#top
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/wangxiang-hz
https://github.com/wangxiang-hz
wangxiang-hzhttps://github.com/wangxiang-hz
on Feb 24, 2023https://github.com/python/cpython/issues/102213#issue-1598539750
getattr is much slower in Python3.11https://discuss.python.org/t/getattr-is-much-slower-in-python3-11/24028
gh-102213: Optimizing the Performance of __getattr__ #102213 #102248https://github.com/python/cpython/pull/102248
Revert "gh-102213: Optimize the performance of __getattr__ (GH-102248)" #103332https://github.com/python/cpython/pull/103332
gh-102213: Optimize the performance of __getattr__ #103761https://github.com/python/cpython/pull/103761
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.