René's URL Explorer Experiment


Title: Slow inspect.getmodule · Issue #92041 · python/cpython · GitHub

Open Graph Title: Slow inspect.getmodule · Issue #92041 · python/cpython

X Title: Slow inspect.getmodule · Issue #92041 · python/cpython

Description: Bug report inspect.getmodule is slow. It becomes slower as len(sys.modules) grows. It is slower with modules that don't resolve to valid file paths. This causes inspect.stack and others to be slow as well. def run_test(module): # cols ha...

Open Graph Description: Bug report inspect.getmodule is slow. It becomes slower as len(sys.modules) grows. It is slower with modules that don't resolve to valid file paths. This causes inspect.stack and others to be slow ...

X Description: Bug report inspect.getmodule is slow. It becomes slower as len(sys.modules) grows. It is slower with modules that don't resolve to valid file paths. This causes inspect.stack and others to be s...

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

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.113.4


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Slow inspect.getmodule","articleBody":"**Bug report**\n\n`inspect.getmodule` is slow.  It becomes slower as `len(sys.modules)` grows.  It is slower with modules that don't resolve to valid file paths.  This causes `inspect.stack` and others to be slow as well.\n\n```python\ndef run_test(module):\n    # cols have stack depths\n    # rows have len(sys.modules)\n\n    def print_line(h, *vals):\n        if not h:\n            print(\"%5s\" % h, *[\"%6i\" % v for v in vals])\n        else:\n            print(\"%5s\" % h, *[\"%6.1f\" % v for v in vals])\n\n    def add_modules(n):\n        import random\n        import sys\n        for _ in range(n):\n            sys.modules[f\"foo_{random.randint(0,2**64)}\"] = module\n\n    def measure(depth):\n        import inspect\n        import timeit\n\n        def nest(level):\n            if level > 0:\n                return nest(level-1)\n            else:\n                dur = timeit.timeit(lambda: inspect.stack(), number=1)\n                return dur * 1000\n\n        return nest(depth)\n\n    import sys\n    print_line(\"\", *[2**e for e in range(7)])\n    for i in range(1, 5):\n        add_modules(n=10**i)\n        times = [measure(depth=2**e) for e in range(7)]\n        print_line(len(sys.modules), *times)\n\npython 3.11.0a7+ optimized build ====\n\n# sys has no __file__ and doesn't enter getmodule cache\n>>> import sys\n>>> run_test(sys)\n           1      2      4      8     16     32     64\n   87    2.1    0.8    1.0    1.3    2.0    3.3    6.1\n  187    2.0    2.5    2.7    3.6    5.5    9.5   16.8\n 1187   13.6   15.7   19.1   26.3   39.6   67.4  123.3\n11187  134.1  150.3  183.4  252.0  383.5  750.3 1189.8\n>>> exit()\n\n>>> import random\n>>> run_test(random)\n           1      2      4      8     16     32     64\n   87    2.1    0.7    0.9    1.2    1.7    2.9    5.6\n  187    2.3    1.1    1.5    1.8    2.5    4.3    7.8\n 1187   17.0    4.3    5.6    7.1   11.1   18.6   33.9\n11187  164.6   37.4   45.7   61.6   94.7  161.0  297.7\n>>> exit()\n```\nTables show time in milliseconds for a single `inspect.stack()` call.\nRows show `len(sys.modules)`\nColumns indicate stack depth\n\n**Your environment**\n\n- CPython versions tested on: 3.9, 3.11.0a7+\n- Operating system and architecture: MacOS, Linux, x64\n\n\n\u003c!-- gh-pr-number: gh-92042 -->\n* PR: gh-92042\n\u003c!-- /gh-pr-number -->\n\n\n\u003c!-- gh-pr-number: gh-154403 -->\n* PR: gh-154403\n\u003c!-- /gh-pr-number -->\n\n\n\u003c!-- gh-pr-number: gh-156685 -->\n* PR: gh-156685\n\u003c!-- /gh-pr-number -->\n\n\n\u003c!-- gh-pr-number: gh-160254 -->\n* PR: gh-160254\n\u003c!-- /gh-pr-number -->\n","author":{"url":"https://github.com/mdeck","@type":"Person","name":"mdeck"},"datePublished":"2022-04-29T03:17:33Z","url":"https://github.com/python/cpython/issues/92041"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f56dfe79-3bc2-a533-abac-d6c34170e94b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCC84:1A09C8:FBA6DC:14E33A2:6B064DFE
html-safe-nonce7c548ac45e7b260fed403da2cd633f2984dd34fdac12ecbe3e1883e2570189f7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzg0OjFBMDlDODpGQkE2REM6MTRFMzNBMjo2QjA2NERGRSIsInZpc2l0b3JfaWQiOiI5MTg1MDY3NjA0NzQ5MjA5MDg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac2b04e07bf0ad9e1d97e02cac2206825e1631d8d30e91e2942fc45cc666493ceb
hovercard-subject-tagissue:1219700779
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/92041/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d057fe831d776fcb42dddf5e9bae9aec933f7c27f3da63e3463609cf33a1cda3/python/cpython/issues/92041
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d057fe831d776fcb42dddf5e9bae9aec933f7c27f3da63e3463609cf33a1cda3/python/cpython/issues/92041
og:image:altBug report inspect.getmodule is slow. It becomes slower as len(sys.modules) grows. It is slower with modules that don't resolve to valid file paths. This causes inspect.stack and others to be slow ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemdeck
hostnamegithub.com
expected-hostnamegithub.com
None772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e
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
releasef1f9a08f7c769a37f1dfc18ad43d463c222a7eea
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/92041#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F92041
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F92041
Sign uphttps://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/92041
Reloadhttps://github.com/python/cpython/issues/92041
Reloadhttps://github.com/python/cpython/issues/92041
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/92041
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/92041
Slow inspect.getmodulehttps://github.com/python/cpython/issues/92041#top
performancehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
stdlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
type-bughttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/mdeck
mdeckhttps://github.com/mdeck
on Apr 29, 2022https://github.com/python/cpython/issues/92041#issue-1219700779
gh-92041: Avoid repeated sys.modules scans in inspect.getmodule #92042https://github.com/python/cpython/pull/92042
gh-92041: Optimize inspect.getmodule lookup path #154403https://github.com/python/cpython/pull/154403
gh-92041: Avoid a second sys.modules scan in inspect.getmodule #156685https://github.com/python/cpython/pull/156685
gh-92041: Resolve registered frames from verified globals ownership #160254https://github.com/python/cpython/pull/160254
mdeckhttps://github.com/mdeck
type-bughttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Apr 29, 2022https://github.com/python/cpython/issues/92041#event-6519662028
hugovkhttps://github.com/hugovk
performancehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
on Apr 29, 2022https://github.com/python/cpython/issues/92041#event-6520830195
https://github.com/mdeck
mdeckhttps://github.com/mdeck
on May 8, 2022https://github.com/python/cpython/issues/92041#issuecomment-1120434482
#92042https://github.com/python/cpython/pull/92042
https://github.com/mdeck
mdeckhttps://github.com/mdeck
on May 8, 2022https://github.com/python/cpython/issues/92041#issuecomment-1120450022
iritkatrielhttps://github.com/iritkatriel
3.12https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
stdlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
on Sep 10, 2022https://github.com/python/cpython/issues/92041#event-7361246116
https://github.com/asottile-sentry
asottile-sentryhttps://github.com/asottile-sentry
on Jan 26, 2024https://github.com/python/cpython/issues/92041#issuecomment-1912505132
bdracohttps://github.com/bdraco
on Mar 3, 2024https://github.com/python/cpython/issues/92041#event-11988747571
Avoid expensive inspect calls in config validatorshttps://github.com/home-assistant/core/commit/e43001069388cf9dfb7506ce63c48d011fde2e64
e430010https://github.com/home-assistant/core/commit/e43001069388cf9dfb7506ce63c48d011fde2e64
balloobhttps://github.com/balloob
on Mar 3, 2024https://github.com/python/cpython/issues/92041#event-11988864230
Avoid expensive inspect calls in config validators (#112085)https://github.com/home-assistant/core/commit/6421a08ba852cc38d77b923bb93bbbb1a26bfa71
#112085https://github.com/home-assistant/core/pull/112085
6421a08https://github.com/home-assistant/core/commit/6421a08ba852cc38d77b923bb93bbbb1a26bfa71
Avoid expensive inspect calls in config validators (#112085)https://github.com/home-assistant/core/commit/780f6e897462aafcb7d7abb181e8fe9ef5b5e0a0
#112085https://github.com/home-assistant/core/pull/112085
780f6e8https://github.com/home-assistant/core/commit/780f6e897462aafcb7d7abb181e8fe9ef5b5e0a0
serhiy-storchakahttps://github.com/serhiy-storchaka
Inspect issues 🔍https://github.com/orgs/python/projects/45
on Mar 5, 2024https://github.com/python/cpython/issues/92041#event-14872282604
bdracohttps://github.com/bdraco
on Mar 7, 2024https://github.com/python/cpython/issues/92041#event-12035466667
Speed up the frame helperhttps://github.com/home-assistant/core/commit/ca80f5d625ef1aaf0c903442baf2614439483f2d
ca80f5dhttps://github.com/home-assistant/core/commit/ca80f5d625ef1aaf0c903442baf2614439483f2d
bdracohttps://github.com/bdraco
on Mar 14, 2024https://github.com/python/cpython/issues/92041#event-12113968656
Fix scene integration doing blocking I/O in the event loop to import …https://github.com/bdraco/home-assistant/commit/d4672349008d96c7ae3a2da692b4ebb29469f863
d467234https://github.com/bdraco/home-assistant/commit/d4672349008d96c7ae3a2da692b4ebb29469f863
mdeckhttps://github.com/mdeck
on Aug 14, 2026https://github.com/python/cpython/issues/92041#event-29470492761
pythongh-92041: Resolve frame modules from globalshttps://github.com/mdeck/cpython/commit/909b8280ee97af712701645ba72d9a02a713fdaa
pythongh-92041https://github.com/python/cpython/issues/92041
909b828https://github.com/mdeck/cpython/commit/909b8280ee97af712701645ba72d9a02a713fdaa
pythongh-92041: Address getmodule review findingshttps://github.com/mdeck/cpython/commit/58703a5c2859db32ba2d6397a6c8c20a5ab08534
pythongh-92041https://github.com/python/cpython/issues/92041
58703a5https://github.com/mdeck/cpython/commit/58703a5c2859db32ba2d6397a6c8c20a5ab08534
pythongh-92041: Preserve frame filename semanticshttps://github.com/mdeck/cpython/commit/3fbfadc521f0d8611c542ee2f20b4b94f4657e87
pythongh-92041https://github.com/python/cpython/issues/92041
3fbfadchttps://github.com/mdeck/cpython/commit/3fbfadc521f0d8611c542ee2f20b4b94f4657e87
mdeckhttps://github.com/mdeck
on Aug 24, 2026https://github.com/python/cpython/issues/92041#event-29943705251
pythongh-92041: Preserve getmodule filename fallbackhttps://github.com/mdeck/cpython/commit/0afefebe997bb9ddfc51ce72d4d28affe3fa68cd
pythongh-92041https://github.com/python/cpython/issues/92041
0afefebhttps://github.com/mdeck/cpython/commit/0afefebe997bb9ddfc51ce72d4d28affe3fa68cd
pythongh-92041: Safely cache inspect.getmodule filename lookupshttps://github.com/mdeck/cpython/commit/33ccc7b47429caaa33e4bf7797b84cc7ceb50903
pythongh-92041https://github.com/python/cpython/issues/92041
33ccc7bhttps://github.com/mdeck/cpython/commit/33ccc7b47429caaa33e4bf7797b84cc7ceb50903
eendebakpthttps://github.com/eendebakpt
on Aug 30, 2026https://github.com/python/cpython/issues/92041#event-30246760316
pythongh-92041: Avoid a second sys.modules scan in inspect.getmodulehttps://github.com/eendebakpt/cpython/commit/012a35e753c8d6165274a0a6cf019a1afe82f5e8
pythongh-92041https://github.com/python/cpython/issues/92041
012a35ehttps://github.com/eendebakpt/cpython/commit/012a35e753c8d6165274a0a6cf019a1afe82f5e8
pythongh-92041: Avoid a second sys.modules scan in inspect.getmodulehttps://github.com/eendebakpt/cpython/commit/c7b4533617932b9f47140992b747c1aaf9c44a6d
pythongh-92041https://github.com/python/cpython/issues/92041
c7b4533https://github.com/eendebakpt/cpython/commit/c7b4533617932b9f47140992b747c1aaf9c44a6d
pythongh-92041: Avoid a second sys.modules scan in inspect.getmodulehttps://github.com/eendebakpt/cpython/commit/ee04f07e46fdb74b952b471effad40a9fc0936b7
pythongh-92041https://github.com/python/cpython/issues/92041
ee04f07https://github.com/eendebakpt/cpython/commit/ee04f07e46fdb74b952b471effad40a9fc0936b7
pythongh-92041: Avoid a second sys.modules scan in inspect.getmodulehttps://github.com/eendebakpt/cpython/commit/ced53761cbb922e33f01c5d688fe56b09e4449ac
pythongh-92041https://github.com/python/cpython/issues/92041
ced5376https://github.com/eendebakpt/cpython/commit/ced53761cbb922e33f01c5d688fe56b09e4449ac
pythongh-92041: Avoid a second sys.modules scan in inspect.getmodulehttps://github.com/eendebakpt/cpython/commit/da559e7d67af9c24dfa1c68926b2a750ed3c553c
pythongh-92041https://github.com/python/cpython/issues/92041
da559e7https://github.com/eendebakpt/cpython/commit/da559e7d67af9c24dfa1c68926b2a750ed3c553c
pythongh-92041: Test getmodule of a frame exec'd in a fresh namespacehttps://github.com/eendebakpt/cpython/commit/03df224b0164352e784baaa962c8e41735b7121f
pythongh-92041https://github.com/python/cpython/issues/92041
03df224https://github.com/eendebakpt/cpython/commit/03df224b0164352e784baaa962c8e41735b7121f
mdeckhttps://github.com/mdeck
on Oct 22, 2026https://github.com/python/cpython/issues/92041#event-33955643522
pythongh-92041: Avoid registry scans for uncached registered frameshttps://github.com/mdeck/cpython/commit/530c2f0ca9918a8f43ec3aeb06a698eb01419600
pythongh-92041https://github.com/python/cpython/issues/92041
530c2f0https://github.com/mdeck/cpython/commit/530c2f0ca9918a8f43ec3aeb06a698eb01419600
picnixzhttps://github.com/picnixz
3.12https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
on Oct 22, 2026https://github.com/python/cpython/issues/92041#event-33980352598
mdeckhttps://github.com/mdeck
on Oct 22, 2026https://github.com/python/cpython/issues/92041#event-33995901782
pythongh-92041: Address frame lookup review and document cache preced…https://github.com/mdeck/cpython/commit/ef27738a6fea5551f4176e370ba5b3c0a63dd5da
pythongh-92041https://github.com/python/cpython/issues/92041
ef27738https://github.com/mdeck/cpython/commit/ef27738a6fea5551f4176e370ba5b3c0a63dd5da
pythongh-92041: Verify frame ownership and retain safe cached module …https://github.com/mdeck/cpython/commit/6a916683df63b1a6b8bfe9d3a105d09eaab1c09c
pythongh-92041https://github.com/python/cpython/issues/92041
6a91668https://github.com/mdeck/cpython/commit/6a916683df63b1a6b8bfe9d3a105d09eaab1c09c
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/92041
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/92041
performancehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
stdlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
type-bughttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
Inspect issues 🔍https://github.com/orgs/python/projects/45
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

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.