python / python/cpython

Remove Incref/Decref of Specific Immortal Objects

Ouverte
#117,425 3 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

3.13 type-feature
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Feature or enhancement

Proposal:

In various places we incref or decref an object that we already know is a specific immortal object, like None. For example, see gh-117393. These operations are unnecessary and the (small) cost can be avoided.

We could simply drop the increfs and decrefs, but there is value in communicating that we would have done the op if the object weren't known to be immortal. (Furthermore, there's the very, very remote chance that some known-immortal object might be made mortal again some day, for some currently unknown reason. It would be quite hard at the point to know that we should add back in the incref or decref we had removed.)

Here are the options I see:

  1. leave the existing code alone
  2. replace the existing code with comments
  3. replace the existing code with a noop macro (in the internal API)

We probably wouldn't make all the changes we could find all at once, but could at the least point to this issue for the decision on what should be done. If we do add a macro then we'd do it relative to this issue.

Personally, I'd go with the macro. It would communicate the intent to readers just as well as the existing code does or as a comment would. I imagine it would look something like the following:

#ifdef Py_DEBUG
# define Py_INCREF_IMMORTAL(OBJ) \
    assert(_Py_IsImmortal(OBJ)
# define Py_DECREF_IMMORTAL(OBJ) \
    assert(_Py_IsImmortal(OBJ)
#else
# define Py_INCREF_IMMORTAL(OBJ)
# define Py_DECREF_IMMORTAL(OBJ)
#endif
For None we could be even more explicit:
#define Py_INCREF_NONE() \
    Py_INCREF_IMMORTAL(Py_None)
#define Py_DECREF_NONE() \
    Py_DECREF_IMMORTAL(Py_None)

// or

#define Py_INCREF_NONE(OBJ) \
    do { \
        assert(OBJ == Py_None) \
        Py_INCREF_IMMORTAL(OBJ) \
    } while (0)
#define Py_DECREF_NONE(OBJ) \
    do { \
        assert(OBJ == Py_None) \
        Py_DECREF_IMMORTAL(OBJ) \
    } while (0)

CC @eduardo-elizondo

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la proposition et la discussion gh-117393 référencée, puis localisez les opérations incref/decref existantes sur les immortal objects connus. Comparez les trois options énoncées, y compris le comportement Py_DEBUG proposé, et déterminez quel choix d’API et quelle portée seraient considérés comme terminés ; aucun fichier ni test n’est nommé dans l’issue.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
c
Domaine
backend
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.