python / python/cpython

Remove Incref/Decref of Specific Immortal Objects

Open
#117,425 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposal and the referenced gh-117393 discussion, then locate the existing incref/decref operations on known immortal objects. Compare the three stated options, including the proposed Py_DEBUG behavior, and establish which API choice and scope would count as done; no files or tests are named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.