python / python/cpython

Optimize reference tracking and eliminate branching during returns and yields

Aperta
#144,540 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.15 interpreter-core topic-JIT
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

There are a few optimizations we can make to returns and yields, but they are somewhat related so I'm grouping them into a single issue.

During a return, the VM needs to make the returned reference "heap safe" (convert any borrowed references to strong references) and then pop and clear the frame, which can involve quite a lot of decrefs.
During a yield, the VM needs to make the returned reference heap safe and then pop the frame, but not clear it.

We want to minimize the amount of refcounting operations that we do.

Before we can do much else, we should split RETURN_VALUE and YIELD_VALUE into micro-ops:

macro(RETURN_VALUE) = _MAKE_HEAP_SAFE + _RETURN_VALUE

macro(YIELD_VALUE) = _MAKE_HEAP_SAFE + _YIELD_VALUE

so that we can optimize the uops independently

Eliminate making heap safe if we know that reference already is

If TOS is a strong reference _MAKE_HEAP_SAFE -> _NOP

Avoid the branch in _RETURN_VALUE by splitting into _RETURN_VALUE_GEN and _RETURN_VALUE_FUNC

To avoid using up opcodes, we'll need to do this in the JIT, not the interpreter.

Track borrows/immortals to optimize clearing frames

E.g. if a frame has four local variables, a, b, c, d but we can tell that a and d are immortal, or borrowed, then instead of looping over all the variables, we could emit code just to decref b and c.
This might end up bloating the code, as we would need to inline the decrefs, but it could be quite a lot faster.

Linked PRs
  • gh-144414
  • gh-146320

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 leggendo le macro RETURN_VALUE e YIELD_VALUE, quindi segui _MAKE_HEAP_SAFE, _RETURN_VALUE e _YIELD_VALUE attraverso l’interprete e il JIT. Confronta il lavoro collegato gh-144414 e gh-146320 prima di decidere l’ambito; il completamento richiederebbe percorsi di return e yield ottimizzati indipendentemente e una riduzione del conteggio dei riferimenti non necessario, senza regressioni nel comportamento.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, performance
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.