Add a self-comparison fast path for some built-ins types
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先查看已链接的 PR gh-142027,以及 issue 中针对列表和字符串的自比较基准测试。确定针对内置类型已达成一致的范围,并使用所引用的测试和 pyperformance 结果验证任何提案;完成的要求是采用一种已被接受且有基准测试证据支持的方法。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- performance
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100