Skip to content

gh-123660: Internal macros for accessing empty tuple singleton #123661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Sep 4, 2024

While writing this PR, I thought about the following:

  • For callables that need an empty tuple, using the macro should be recommended. This will 1) reduce the number of lines (we don't need to check if the value is NULL for instance) 2) make it clear that we are using an empty tuple.

  • We should replace calls of the form return PyTuple_New(0) to calls of the form Py_GetConstant(...) and Py_GetConstantBorrowed depending on the semantics. This is something that is important, both for contributors that read the code and for detecting the places where this should be done.

There are places where the compiler would not accept a non-static value (when you initialize structures for instance). In those places, AFAICT, we can only use the macro form and not a call.

While there are many occurrences of constructing empty tuples, they don't have the same semantic meaning. I decided to only replace the following:

  • explicit usage of _Py_SINGLETON.
  • usage of an empty tuple when calling PyArg_ParseTupleAndKeywords.
  • usage of an empty tuple when constructing a structure.

There are other usages where you return an empty tuple, you call PyObject_Call or other functions, but since the diff would be much larger, I decided not to do it for those files. I also removed an empty tuple reference from the lzma's state since it's not needed at all and could make the module slightly faster to instantiate.

While using _Py_EMPTY_TUPLE over PyTuple_New(0) could be slightly faster (or maybe not, I don't know), it removes the needs of redundant checks if (arg == NULL) { ... } since empty tuples are immortal. Currently, those kind of checks still exist, probably because they were not removed when we introduced immortal objects. There is a trade-off between changing everything and removing the checks or not changing all places and only keep the relevant ones changed.

I'm willing to revert the commit for the changes with PyArg_ParseTupleAndKeywords and PyCode_New but I'd like to keep the one concerning the LZMA module.

@picnixz picnixz force-pushed the chore/empty-tuple-macros-123660 branch from c303836 to c345b2a Compare September 4, 2024 12:56
@picnixz picnixz closed this Nov 12, 2024
@picnixz picnixz deleted the chore/empty-tuple-macros-123660 branch November 12, 2024 14:41
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.

1 participant