python / python/cpython

`list.sort` enhancement proposal: Adaptivity for `binarysort`

Aperta
#138,946 53 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@tim-one ci sta già lavorando.

Dal 16/9/2025.

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:

Adding adaptivity to binarysort routine of list.sort.

See PR for specifics.

Initial Post (Outdated)

Note:

  1. This is POC that this has a observable impact.
  2. This is subject to further calibration and optimizations, but high level concept is dicusable.
  3. Will issue PR shortly.
Rationale

Galloping provides adaptivity when merging runs.
However, underlying binarysort always does O(nlogn).

Concept

The concept is simple:

  1. binarysort optionally does adaptive routine.
    1. It has a mechanism to switch it off during the run and go to simple binarysort
    2. It returns 1 if it has completed full data with binary routine and 0 otherwise
  2. timsort calls binarysort
    1. If it returns 1, then use it again next time
    2. If it returns 0, then use binarysort without adaptivity next time
    3. increase number of simple binarysort runs before next attempt of adaptive run with every 0 returned
High level results
  • A0 is current
  • A1 is with adaptivity
  • P means performance / runtime
  • C means comparison count
  • Aggregate numbers for all datasets combined are in the title
  1. Plain integers and floats
Image
  1. Integers and floats wrapped in list, so comparisons are __lt__ calls, thus more expensive
Image

So the benefit is higher when comparisons cost more.
But there is some benefit for optimized comparisons as well.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/sorting-adaptivity-improvement/103700

Linked PRs
  • gh-138947
  • gh-139342
  • gh-139969

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.