Repository navigation
[3.15] gh-129724: Intern string constants in the compiler, not per code object (GH-159193) - #159291
Draft
miss-islington wants to merge 1 commit into
Draft
miss-islington wants to merge 1 commit into
miss-islington wants to merge 1 commit into
Conversation
…e object (pythonGH-159193) The compiler merges equal constants of a module through its constant cache. intern_constants() then ran on each code object and rebuilt a frozenset (or, in the free-threaded build, a slice) when one of its strings was replaced by an already interned one, so a constant shared by several functions was copied for some of them depending on which strings happened to be interned in the process. The class body's __qualname__ constant and co_qualname had the same problem. marshal then wrote different bytes for the same source. Intern the eligible strings of the final constants of each code unit after optimization and before the code object is created. Tuples are updated in place; a frozenset or slice is rebuilt once and stored in the constant cache so that later units get the same object. The same code is used in both builds. Which strings are interned does not change, except that identifier-like strings in slice constants are now interned in the default build too. TestConstantsIdentity compares the identity graph and the marshal output of code compiled in differently prepared processes, including names tuples shared with constants. (cherry picked from commit 627eabf) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
miss-islington
requested review from
iritkatriel and
markshannon
as code owners
October 13, 2026 05:09
gpshead
marked this pull request as draft
October 13, 2026 05:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The compiler merges equal constants of a module through its constant
cache. intern_constants() then ran on each code object and rebuilt a
frozenset (or, in the free-threaded build, a slice) when one of its
strings was replaced by an already interned one, so a constant shared by
several functions was copied for some of them depending on which strings
happened to be interned in the process. The class body's qualname
constant and co_qualname had the same problem. marshal then wrote
different bytes for the same source.
Intern the eligible strings of the final constants of each code unit
after optimization and before the code object is created. Tuples are
updated in place; a frozenset or slice is rebuilt once and stored in the
constant cache so that later units get the same object. The same code
is used in both builds.
Which strings are interned does not change, except that identifier-like
strings in slice constants are now interned in the default build too.
TestConstantsIdentity compares the identity graph and the marshal
output of code compiled in differently prepared processes, including
names tuples shared with constants.
(cherry picked from commit 627eabf)
Co-authored-by: Gregory P. Smith 68491+gpshead@users.noreply.github.com