python / python/cpython

Add a self-comparison fast path for some built-ins types

オープン
#141,858 コメント 34 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core performance type-feature
主要言語
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.

result.txt

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンクされている PR gh-142027 と、リストおよび文字列に関する issue の自己比較ベンチマークを確認します。組み込み型について合意された対象範囲を特定し、参照されているテストと pyperformance の結果で提案を検証します。完了条件は、裏付けとなるベンチマークのエビデンスを伴う、受け入れられたアプローチがあることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
performance
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。