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
Domain: github.com
IPv4 address: 140.82.113.4
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f56dfe79-3bc2-a533-abac-d6c34170e94b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC84:1A09C8:FBA6DC:14E33A2:6B064DFE |
| html-safe-nonce | 7c548ac45e7b260fed403da2cd633f2984dd34fdac12ecbe3e1883e2570189f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzg0OjFBMDlDODpGQkE2REM6MTRFMzNBMjo2QjA2NERGRSIsInZpc2l0b3JfaWQiOiI5MTg1MDY3NjA0NzQ5MjA5MDg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2b04e07bf0ad9e1d97e02cac2206825e1631d8d30e91e2942fc45cc666493ceb |
| hovercard-subject-tag | issue:1219700779 |
| 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/92041/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d057fe831d776fcb42dddf5e9bae9aec933f7c27f3da63e3463609cf33a1cda3/python/cpython/issues/92041 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d057fe831d776fcb42dddf5e9bae9aec933f7c27f3da63e3463609cf33a1cda3/python/cpython/issues/92041 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mdeck |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| 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 | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width