python / python/cpython

Avoid refcounting on code objects during frame creation/destruction

Aperta
#152,666 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

The __code__ attribute of function objects is assignable.
As a consequence, frames must hold a strong reference to the code object in case the reference held by the function is lost.

Ideally, the __code__ attribute would be read-only, but to fix that would be a multi-year deprecation effort.

Until that happens, if ever, we can replace the strong reference with a borrowed reference to the code object as long as code object is guaranteed to outlive the frame.
Since the function will outlive the frame, if the function object keeps references to its prior __code__ attributes
when __code__ is assigned, then the code object will always outlive the frame.

This will need an extra field in each function, but the cost of one assignment to NULL during function creation, is much less than the cost of an incref and a decref on every call to that function.

The only downside is that in some obscure cases the lifetime of a few code objects will be extended.
Since the free-threading build makes all code objects immortal, this could actually be a big saving for the free-threading build.

Linked PRs
  • gh-152955

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-152955, quindi segui gli oggetti funzione, i frame e gli oggetti codice in relazione all’attributo code e alla creazione e distruzione dei frame. Il lavoro è completato quando viene dimostrato che gli oggetti codice sopravvivono ai propri frame evitando operazioni di conteggio dei riferimenti a ogni chiamata e i test CPython pertinenti continuano a essere superati.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Refactoring
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.