python / python/cpython

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

Aperta
#141,858 34 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core performance type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando la PR collegata gh-142027 e i benchmark di autoconfronto dell'issue per liste e stringhe. Determina l'ambito concordato per i tipi integrati e valida qualsiasi proposta con i test referenziati e i risultati di pyperformance; il lavoro sarà completo quando sarà disponibile un approccio accettato con evidenze di benchmark a supporto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
performance
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.