Repository navigation
gh-140328: Use interned versions of string constants if they're already present - #140688
Draft
albertedwardson wants to merge 27 commits into
Draft
albertedwardson wants to merge 27 commits into
albertedwardson wants to merge 27 commits into
Conversation
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.
closes #140328
intern_constants()previously only interned string constants whenshould_intern_string()returned true, leaving equal strings duplicated even if an interned instance already existed. This change first checks the global and per-interpreter interned string tables and reuses an existing interned string when available. This applies only to string constants that come from the codeobjects; dynamically created strings are unaffected and are still created as new objects.This makes string interning behavior more consistent: once a string is interned, subsequent equal constants can reuse the canonical object. It may slightly reduce memory usage and improves interaction with
sys.intern().The change adds a small lookup cost when loading code objects, which may affect
eval(),exec(), modules loading and similar operations.I haven't run
pyperformanceyet, but I plan to provide a benchmark run once I have access to an appropriate runner. Given the limited scope of the change, I expect both the performance impact and memory savings to be relatively small but measurable.