Intern string representation of operators and some other symbolic literals
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
Python currently interns certain strings, such as keywords and some ASCII/Unicode characters, as well as module-specific strings. I propose extending this interning mechanism to the string representations of operators (e.g., "+=", "==", "|=").
Rationale:
Interning these strings could improve performance, particularly in code parsing workflows, by:
- Reducing memory overhead for repeated operator strings.
- Accelerating string comparisons (e.g., during AST construction or bytecode generation).
Target Symbols:
The following multi-character syntactic literals (with len() > 1) are candidates for interning:
# Syntax literals
'...', '->'
# Operators
'**', '//', '==', '!=', '>=', '<=', ':=',
'+=', '-=', '*=', '/=', '//=', '%=', '**=',
'<<', '>>', '<<=', '>>=', '&=', '|=', '^='
# And maybe character sequence that used in REPL?
'>>>'
Considerations:
- The change would be low-risk, as it targets immutable, statically known strings.
- The impact on startup time and memory usage should be negligible, given the small set of operators.
Would this be a worthwhile optimization for CPython? I’d appreciate feedback on the idea and the PoC.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-140235
- gh-151838
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposal and the linked PRs gh-140235 and gh-151838, focusing on the parser, AST construction, bytecode generation, and REPL cases mentioned. The issue does not define a settled implementation or acceptance criteria; confirm the intended scope and outcome from those discussions before considering the work complete.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100