René's URL Explorer Experiment


Title: gh-129724: Intern string constants in the compiler, not per code object by gpshead · Pull Request #159193 · python/cpython · GitHub

Open Graph Title: gh-129724: Intern string constants in the compiler, not per code object by gpshead · Pull Request #159193 · python/cpython

X Title: gh-129724: Intern string constants in the compiler, not per code object by gpshead · Pull Request #159193 · python/cpython

Description: Compiling the same source could give code objects whose constants are shared differently, depending on which strings happened to be interned in the process. marshal then writes different bytes, so .pyc files were not reproducible (gh-129724). Example Two functions use an equal set constant. The compiler makes it one frozenset object, unless a string in it is already interned when the code objects are created: 1. fresh interpreter len(marshal.dumps(code))=398 f and g share one frozenset: True 2. same again len(marshal.dumps(code))=398 f and g share one frozenset: True 3. while another code object using the same strings is alive len(marshal.dumps(code))=408 f and g share one frozenset: False 4. after that code object is freed len(marshal.dumps(code))=398 f and g share one frozenset: True With this PR all four are 398 bytes with one shared frozenset. The same happened to the __qualname__ constant of a class body: compiling class A: class B: pass twice while the first result is alive gives different bytes on main. Cause and fix The compiler merges equal constants of a module through its constant cache. Interning then ran later, once per code object, in intern_constants(). A frozenset cannot be modified, so when one of its strings had to be replaced by an already interned string, that code object got a private copy. flowchart TB subgraph before["main"] direction TB m1["compiler constant cache
one frozenset F for f and g"] --> m2["code object f
intern_constants()"] m1 --> m3["code object g
intern_constants()"] m2 -- "a string of F is
already interned" --> m4["F' = copy of F
with the interned string"] m3 -- "same" --> m5["F'' = another copy"] m4 --> m6["f and g no longer share:
marshal writes the set twice"] m5 --> m6 end subgraph after["this PR"] direction TB n1["compiler constant cache
one frozenset F for f and g"] --> n2["intern_final_constants()
when f is assembled"] n2 -- "a string of F is
already interned" --> n3["F' built once and
stored in the cache entry of F"] n3 --> n4["g is assembled:
the cache gives F'"] n4 --> n5["f and g share F'
whatever was interned before"] n5 --> n6["intern_constants() in
the code object: nothing
left to replace"] end Loading intern_final_constants() runs in optimize_and_assemble_code_unit(), after the optimizer and before the code object is created, on the constants the unit really ends up with. Strings that never reach a code object are not interned. Tuples are updated in place. A frozenset or slice is rebuilt at most once, and the result is stored in its constant cache entry so later code units get the same object. The qualified name of a class is interned by the compiler only where it would not be interned as a constant anyway. intern_constants() in Objects/codeobject.c is not changed; its should_intern_string() is renamed _PyCode_ShouldInternString() and exported so the compiler uses the same rule. Behaviour changes Which strings get interned does not change, with one exception: identifier-like strings inside slice constants are now interned on the default build too, as they already were for every other kind of constant. Compared with main in a fresh process, 13 of 2078 stdlib files compile to different (smaller) bytes: constants that main always copied are now shared. compile() is about 0.5% slower on the default build (release, not PGO), which is inside the run-to-run noise. Tests TestConstantsIdentity in test_compile compiles a table of cases in child processes prepared in different ways (strings pre-interned, used as names, used as constants, the same source already compiled) and compares both the identity graph of the constants and the marshal output with a fresh process. It also covers tuples of names that are the same object as an equal constant (co_names of methods using a mangled name, keyword names, from lists, __static_attributes__), so that a change which re-creates those tuples per code object fails the test (the current version of draft #122180 does this). With the marshal PR gh-159192 fixing the other half of gh-129724 Compiling each stdlib file under 14 different process conditions (file order, threads, pre-interned strings, earlier imports, hash seeds, keeping code objects alive) and comparing with one file per process: default (GIL) free-threaded main, files differing in the worst condition 433 63 both PRs, plus the fixes for gh-159184, gh-159185, and gh-159188 0 0 The matrix was only run with all of these applied together. The three other fixes do not affect code compiled from source strings, which is all this matrix compiles. drafted using Claude, reviewed & iterated upon by me. (combined with the marshal fix, this will close the title issue)

Open Graph Description: Compiling the same source could give code objects whose constants are shared differently, depending on which strings happened to be interned in the process. marshal then writes different bytes, so...

X Description: Compiling the same source could give code objects whose constants are shared differently, depending on which strings happened to be interned in the process. marshal then writes different bytes, so...

Opengraph URL: https://github.com/python/cpython/pull/159193

X: @github

direct link

Domain: github.com

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/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:17092440-a15e-34c4-6b39-738952262f49
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id8956:2D40DE:3316822:4107084:6B02156D
html-safe-nonceb4d64638eaef53f6ebe239bb7151cbc43780d6333a3238875086149f994829e1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTU2OjJENDBERTozMzE2ODIyOjQxMDcwODQ6NkIwMjE1NkQiLCJ2aXNpdG9yX2lkIjoiNjA1NDgyMDI1MTcyNjY0ODY4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac364e42af26a36ca9aad7be9810f7164235e740135baf112655c1eb8d922c74cb
hovercard-subject-tagpull_request:4821617854
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/159193/files
twitter:imagehttps://avatars.githubusercontent.com/u/68491?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/68491?s=400&v=4
og:image:altCompiling the same source could give code objects whose constants are shared differently, depending on which strings happened to be interned in the process. marshal then writes different bytes, so...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/159193/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F159193%2Ffiles
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%2Fpull%2F159193%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/159193/files
Reloadhttps://github.com/python/cpython/pull/159193/files
Reloadhttps://github.com/python/cpython/pull/159193/files
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
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
gpsheadhttps://github.com/gpshead
python:mainhttps://github.com/python/cpython/tree/main
gpshead:compile-intern-constshttps://github.com/gpshead/cpython/tree/compile-intern-consts
Conversation 3 https://github.com/python/cpython/pull/159193
Commits 1 https://github.com/python/cpython/pull/159193/commits
Checks 77 https://github.com/python/cpython/pull/159193/checks
Files changed 5 https://github.com/python/cpython/pull/159193/files
gh-129724: Intern string constants in the compiler, not per code object https://github.com/python/cpython/pull/159193/files#top
Show all changes 1 commit https://github.com/python/cpython/pull/159193/files
badcae7 gh-129724: Intern string constants in the compiler, not per code object gpshead Oct 11, 2026 https://github.com/python/cpython/pull/159193/commits/badcae76551eee7fcc49972d53f2d9d722aa08cd
Clear filters https://github.com/python/cpython/pull/159193/files
Please reload this pagehttps://github.com/python/cpython/pull/159193/files
Please reload this pagehttps://github.com/python/cpython/pull/159193/files
pycore_code.h https://github.com/python/cpython/pull/159193/files#diff-97ea1759f82531a45fa685065ff86bc1077c3ab3d5f65220eea48b7730032bc8
test_compile.py https://github.com/python/cpython/pull/159193/files#diff-c69ceae0611c40012b4691e3536bf17efacdb5b51b8e155eec9a939108e42e43
2026-10-11-01-41-31.gh-issue-129724.Kq3vTz.rst https://github.com/python/cpython/pull/159193/files#diff-c0aed578d6e6c53d481ec00dfe979d1281f5c7370208e258ef9940515c67cfd1
codeobject.c https://github.com/python/cpython/pull/159193/files#diff-6f869eb8beb7cbe4bc6817584b99ad567f88962fa67f7beca25d009dc401234d
compile.c https://github.com/python/cpython/pull/159193/files#diff-ebc983d9f91e5bcf73500e377ac65e85863c4f77fd5b6b6caf4fcdf7c0f0b057
Include/internal/pycore_code.hhttps://github.com/python/cpython/pull/159193/files#diff-97ea1759f82531a45fa685065ff86bc1077c3ab3d5f65220eea48b7730032bc8
View file https://github.com/gpshead/cpython/blob/badcae76551eee7fcc49972d53f2d9d722aa08cd/Include/internal/pycore_code.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/159193/{{ revealButtonHref }}
https://github.com/python/cpython/pull/159193/files#diff-97ea1759f82531a45fa685065ff86bc1077c3ab3d5f65220eea48b7730032bc8
https://github.com/python/cpython/pull/159193/files#diff-97ea1759f82531a45fa685065ff86bc1077c3ab3d5f65220eea48b7730032bc8
Please reload this pagehttps://github.com/python/cpython/pull/159193/files
Please reload this pagehttps://github.com/python/cpython/pull/159193/files
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.