Skip to content

gh-91927: Add helpers for obtaining new references to various singletons#91925

Closed
hvenev wants to merge 9 commits into
python:mainfrom
hvenev:ref
Closed

gh-91927: Add helpers for obtaining new references to various singletons#91925
hvenev wants to merge 9 commits into
python:mainfrom
hvenev:ref

Conversation

@hvenev
Copy link
Copy Markdown
Contributor

@hvenev hvenev commented Apr 25, 2022

There are lots of places where a new reference to a singleton is needed. This is done in a variety of ways:

obj = Py_NewRef(Py_None);
obj = Py_None;
Py_INCREF(obj);
Py_INCREF(Py_None);
obj = Py_None;
obj = Py_None;
Py_INCREF(Py_None);
obj = (Py_INCREF(Py_None), Py_None);

The helpers implemented here add another way that can hopefully become more idiomatic:

obj = Py_RefNone();

One advantage of using the new helpers is that it abstracts away the necessity to increment the reference count if immortal instances are implemented.

Alternatively, if immortal instances aren't implemented and all singletons are made per-interpreter, it will slightly reduce the performance overhead by making sure that the singletons are only obtained once (compared to patterns 3, 4, and 5, where the singetons are obtained twice).

@hvenev hvenev changed the title Add helpers for obtaining new references to various singletons gh-91927: Add helpers for obtaining new references to various singletons Apr 25, 2022
hvenev added 9 commits April 25, 2022 23:32
Helpers are defined for the following singletons:
- None
- NotImplemented
- Ellipsis
- False
- True
They are now simply calls to Py_Ref{True,False}.
The function body is quite small and already open-coded in lots of
places.

The original `PyBool_FromLong` is retained for ABI compatibility. A new
inline `_PyBool_FromLong` is added and a macro is used so that calls to
`PyBool_FromLong` use it instead.
Now that it is inline, there shouldn't be a performance penalty.
@brettcannon brettcannon removed the request for review from a team April 25, 2022 23:28
@brettcannon brettcannon removed their request for review April 25, 2022 23:28
@rhettinger
Copy link
Copy Markdown
Contributor

-1

From my point of view, this doesn't make anything better. The code would be slightly more consistent (but in an unimportant way) and the new functions are less readable — in particular, my mind rebels at the sight of Py_RefFalse.

If immortals do come to fruition, this can be revisited, but I can see any real value coming from it unless all third-party code could be converted as well (otherwise, you have a mix of old and new calls).

@rhettinger
Copy link
Copy Markdown
Contributor

If another coredev wants to champion this one, feel free to reopen.

@rhettinger rhettinger closed this May 5, 2022
@ericsnowcurrently
Copy link
Copy Markdown
Member

I agree with @rhettinger. It is too soon to consider any API like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants