Title: `tokenize.generate_tokens()` performance regression in 3.12 · Issue #119118 · python/cpython · GitHub
Open Graph Title: `tokenize.generate_tokens()` performance regression in 3.12 · Issue #119118 · python/cpython
X Title: `tokenize.generate_tokens()` performance regression in 3.12 · Issue #119118 · python/cpython
Description: Bug report Bug description: There seems to be a significant performance regression in tokenize.generate_tokens() between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I searched the existing issues but couldn't find...
Open Graph Description: Bug report Bug description: There seems to be a significant performance regression in tokenize.generate_tokens() between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I search...
X Description: Bug report Bug description: There seems to be a significant performance regression in tokenize.generate_tokens() between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I search...
Opengraph URL: https://github.com/python/cpython/issues/119118
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`tokenize.generate_tokens()` performance regression in 3.12","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nThere seems to be a significant performance regression in `tokenize.generate_tokens()` between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I searched the existing issues but couldn't find anything about this.\r\n\r\nTo reproduce, rename the file [largedict.py.txt](https://github.com/python/cpython/files/15348502/largedict.py.txt) to `largedict.py` in the same directory as the script below, then run the script. That file comes from https://github.com/nedbat/coveragepy/issues/1785.\r\n\r\n```python\r\nimport io, time, sys, tokenize\r\nimport largedict\r\n\r\ntext = largedict.d\r\nreadline = io.StringIO(text).readline\r\nglob_start = start = time.time()\r\nprint(f\"{sys.implementation.name} {sys.platform} {sys.version}\")\r\n\r\nfor i, (ttype, ttext, (sline, scol), (_, ecol), _) in enumerate(tokenize.generate_tokens(readline)):\r\n if i % 500 == 0:\r\n print(i, ttype, ttext, sline, scol, time.time() - start)\r\n start = time.time()\r\n if i % 5000 == 0:\r\n print(time.time() - glob_start)\r\n\r\nprint(f\"Time taken: {time.time() - glob_start}\")\r\n```\r\n\r\nFor Python 3.12, this results in:\r\n```\r\ncpython linux 3.12.3 (main, May 17 2024, 07:19:22) [GCC 11.4.0]\r\n0 1 a_large_dict_literal 1 0 0.04641866683959961\r\n0.046633005142211914\r\n500 3 ':tombol_a_(golongan_darah):' 1 2675 9.689745903015137\r\n1000 3 ':flagge_anguilla:' 1 5261 9.767053604125977\r\n1500 3 ':флаг_Армения:' 1 7879 9.258271932601929\r\n[...]\r\n```\r\n\r\nFor Python 3.11, this results in:\r\n```\r\ncpython linux 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0]\r\n0 1 a_large_dict_literal 1 0 0.013637304306030273\r\n0.013663768768310547\r\n500 3 ':tombol_a_(golongan_darah):' 1 2675 0.002939462661743164\r\n1000 3 ':flagge_anguilla:' 1 5261 0.0028715133666992188\r\n1500 3 ':флаг_Армения:' 1 7879 0.002806425094604492\r\n[...]\r\n352500 3 'pt' 1 2589077 0.003370046615600586\r\nTime taken: 2.1244866847991943\r\n```\r\n\r\nThat is, each 500 tokens in Python 3.12 is taking over 9 seconds to process, while the 352500 tokens in Python 3.11 is taking a bit over 2 seconds to process. \r\n\r\nI can reproduce this on Linux (WSL) and Windows. Also seems to affect 3.13.\r\n\r\n### CPython versions tested on:\r\n\r\n3.9, 3.10, 3.11, 3.12\r\n\r\n### Operating systems tested on:\r\n\r\nLinux, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-119615\n* gh-119682\n* gh-119683\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2024-05-17T11:49:37Z","url":"https://github.com/python/cpython/issues/119118"}
| 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:13162630-a942-f6df-dcc7-8e2455dcdd12 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA58:1F2B:1489FD0:1B589E0:6B020244 |
| html-safe-nonce | 75305edb7d7d0ccb07c3a09064145ad94eea9a3b627e33c7de165a46f540cd13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTU4OjFGMkI6MTQ4OUZEMDoxQjU4OUUwOjZCMDIwMjQ0IiwidmlzaXRvcl9pZCI6IjI0MDAxMzU1MzQ3NzQxOTA2NjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 80f6c7fbc1e5d3a6982184f5cdca828274d28bd17b999401db21ac21f14c643b |
| hovercard-subject-tag | issue:2302529303 |
| 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/119118/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1839b84d3db155238b0a872a5f5ab9bbcf26565a7540bd66b44601383ff2d5d0/python/cpython/issues/119118 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1839b84d3db155238b0a872a5f5ab9bbcf26565a7540bd66b44601383ff2d5d0/python/cpython/issues/119118 |
| og:image:alt | Bug report Bug description: There seems to be a significant performance regression in tokenize.generate_tokens() between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I search... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width