python / python/cpython

Intern string representation of operators and some other symbolic literals

Open
#136,757 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

easy interpreter-core performance type-feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.