Add a self-comparison fast path for some built-ins types
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:
a = [0] * 1000000
b = [0] * 1000000
timeit.timeit("a == b", globals=globals(), number=1000)
Out: 0.5669927500130143
timeit.timeit("a == a", globals=globals(), number=1000) # should be faster?
Out: 0.5312646660022438
for strings:
s1 = "a" * 1000000 + "b"
s2 = "a" * 1000000 + "c"
timeit.timeit("s1 == s2", globals=globals(), number=1000)
Out: 0.017710374988382682
timeit.timeit("s1 == s1", globals=globals(), number=1000)
Out: 1.5166995581239462e-05
Why doesn't Cpython, in the example above, return True from the == simply based on the object being compared to itself, whereas this optimisation is there for strings?
I made my own branch with such optimization: https://github.com/dima179-fuse/cpython/pull/1
It appears to make some benchmarks faster, and tests pass.
Also attached are pyperformance results for both my branches and main:
2025-11-21_21-36-main-92972aea0f0e.json.gz
self-list-comparison-optimisation-e4ebdcbb8b51.json.gz
standard-containers-comparison-optimisation-54518a64f037.json.gz
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
- gh-142027
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 linked PR gh-142027 and the issue's self-comparison benchmarks for lists and strings. Determine the agreed scope for built-in types and validate any proposal with the referenced tests and pyperformance results; done requires an accepted approach with supporting benchmark evidence.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100