Skip to content

[mypyc] feat: __mypyc_empty_tuple__ constant #19654

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Aug 14, 2025

I realized that any time a user has a kwarg-only call expression like fn(abc=123, ...) in their compiled code, and func is not a native function, a new empty tuple is created every time

This is not really necessary, we can just hold the same empty tuple in memory as a constant and pass it around. It's immutable, and that's already what we're already doing, since tuple() is tuple() but our current method involves more steps.

This should slightly improve the speed of kwarg-only python func calling.

@BobTheBuidler BobTheBuidler marked this pull request as draft August 14, 2025 06:24
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 14, 2025

In 3.13 and later, we could use Py_GetConstant(Py_CONSTANT_EMPTY_TUPLE);. However, having a static variable with the value would be faster, as we'd save at least a function call, and it will work on older Python versions as well, so it's reasonable to cache it in mypyc.

@BobTheBuidler BobTheBuidler force-pushed the empty-tuple-constant branch 2 times, most recently from 02cd112 to fecc89c Compare August 25, 2025 13:55

static inline PyObject *_CPyTuple_LoadEmptyTupleConstant() {
// do tests still pass if I comment this out? empty tuple singleton is not tracked by gc
// Py_INCREF(__mypyc_empty_tuple__);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I can take out this Py_INCREF since the empty tuple singleton is not tracked by the GC but I want to wait for confirmation before I get rid of the comment

@BobTheBuidler BobTheBuidler marked this pull request as ready for review August 25, 2025 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants