Title: sets with >30 elements not constant-folded in `in`/`not in` and `for` loops · Issue #148817 · python/cpython · GitHub
Open Graph Title: sets with >30 elements not constant-folded in `in`/`not in` and `for` loops · Issue #148817 · python/cpython
X Title: sets with >30 elements not constant-folded in `in`/`not in` and `for` loops · Issue #148817 · python/cpython
Description: Bug report Bug description: Since gh-126835 (moving constant folding from AST to CFG), set and list literals with more than 30 constant elements are no longer constant-folded in in/not in membership tests and for loop iteration. This cau...
Open Graph Description: Bug report Bug description: Since gh-126835 (moving constant folding from AST to CFG), set and list literals with more than 30 constant elements are no longer constant-folded in in/not in membershi...
X Description: Bug report Bug description: Since gh-126835 (moving constant folding from AST to CFG), set and list literals with more than 30 constant elements are no longer constant-folded in in/not in membershi...
Opengraph URL: https://github.com/python/cpython/issues/148817
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"sets with >30 elements not constant-folded in `in`/`not in` and `for` loops","articleBody":"# Bug report\n\n### Bug description:\n\nSince gh-126835 (moving constant folding from AST to CFG), set and list literals with more than 30 constant elements are no longer constant-folded in `in`/`not in` membership tests and `for` loop iteration. This causes the collection to be rebuilt from scratch on every evaluation instead of being stored as a compile-time constant.\n\nFor example:\n```python\nimport dis\n\n# 30 elements: folded to frozenset constant\ndis.dis(\"x in {%s}\" % \", \".join(str(i) for i in range(1, 31)))\n# LOAD_NAME x\n# LOAD_CONST frozenset({1, 2, ..., 30})\n# CONTAINS_OP\n\n# 31 elements: NOT folded, set rebuilt every time (BAD)\ndis.dis(\"x in {%s}\" % \", \".join(str(i) for i in range(1, 32)))\n# LOAD_NAME x\n# BUILD_SET 0\n# LOAD_SMALL_INT 1\n# SET_ADD 1\n# ... (31 x LOAD + SET_ADD)\n# CONTAINS_OP\n```\n\nUsing a set-membership check with a set literal with 50 elements as a microbenchmark, this manifests as a ~20x slowdown from 3.13 to 3.14:\n\n```python\nimport timeit, sys\nelts = \", \".join(str(i) for i in range(50))\nt = timeit.timeit(f\"25 in {{{elts}}}\", number=1_000_000)\nprint(f\"25 in {{0..49}}: {t:.3f}s\")\n```\n\nPython 3.13.13: 25 in {0..49}: 0.015s\nPython 3.14.4: 25 in {0..49}: 0.291s\n\nIt also affects code object size:\n\n```python\nfor n in [30, 31, 50, 100]:\n elts = \", \".join(str(i) for i in range(1, n + 1))\n code = compile(f\"x in {{{elts}}}\", \"\u003ctest>\", \"eval\")\n print(f\"x in {{1..{n}}}: {len(code.co_code):>5} bytes, {len(code.co_consts)} const(s)\")\n```\n\nPython 3.13.13:\n\n```\nx in {1..30}: 12 bytes, 1 const(s)\nx in {1..31}: 12 bytes, 1 const(s)\nx in {1..50}: 12 bytes, 1 const(s)\nx in {1..100}: 12 bytes, 1 const(s)\n```\n\nPython 3.14.4:\n\n```\nx in {1..30}: 12 bytes, 2 const(s)\nx in {1..31}: 136 bytes, 1 const(s)\nx in {1..50}: 212 bytes, 1 const(s)\nx in {1..100}: 412 bytes, 1 const(s)\n```\n\n\n### CPython versions tested on:\n\n3.15, CPython main branch, 3.14\n\n### Operating systems tested on:\n\nmacOS, Linux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149016\n* gh-155897\n* gh-155898\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/itamaro","@type":"Person","name":"itamaro"},"datePublished":"2026-04-21T00:40:41Z","url":"https://github.com/python/cpython/issues/148817"}
| 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:90841d0f-662b-5263-50bf-0a98cf1a40e3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B718:23E48:ABEDC5:D757AA:6B0231C5 |
| html-safe-nonce | 133cfa97e6060cf46958a5ef7b944b07a6ef29e7374269d622e8368648bbd7d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzE4OjIzRTQ4OkFCRURDNTpENzU3QUE6NkIwMjMxQzUiLCJ2aXNpdG9yX2lkIjoiMzA0NzcwMDI2NTIzMTg1NjA2OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 230b07c1fe537d35d2e54ce4a1f26ef9f9f620396b133902049cc84d2b49e44b |
| hovercard-subject-tag | issue:4299535918 |
| 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/148817/issue_layout |
| twitter:image | https://opengraph.githubassets.com/87cb3e56e3dff6466886c501752cf4e4fcf1741e81c802771b87fbba13c4a68c/python/cpython/issues/148817 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/87cb3e56e3dff6466886c501752cf4e4fcf1741e81c802771b87fbba13c4a68c/python/cpython/issues/148817 |
| og:image:alt | Bug report Bug description: Since gh-126835 (moving constant folding from AST to CFG), set and list literals with more than 30 constant elements are no longer constant-folded in in/not in membershi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | itamaro |
| 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