Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 12 commits intopython:masterfromBobTheBuidler:empty-tuple-constant
Aug 27, 2025

Conversation

@BobTheBuidler
Copy link
Contributor

@BobTheBuidlerBobTheBuidler commentedAug 14, 2025
edited
Loading

I realized that any time a user has a kwarg-only call expression likefn(abc=123, ...) in their compiled code, andfunc 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, sincetuple() is tuple() but our current method involves more steps.

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

@BobTheBuidlerBobTheBuidler marked this pull request as draftAugust 14, 2025 06:24
@JukkaL
Copy link
Collaborator

In 3.13 and later, we could usePy_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.

@BobTheBuidlerBobTheBuidlerforce-pushed theempty-tuple-constant branch 2 times, most recently from02cd112 tofecc89cCompareAugust 25, 2025 13:55
@BobTheBuidlerBobTheBuidler marked this pull request as ready for reviewAugust 25, 2025 14:09
PyErr_SetString(PyExc_RuntimeError,"Failed to initialize __mypyc_empty_tuple__");
return;
}
Py_INCREF(__mypyc_empty_tuple__);
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to do an incref, sincePyTuple_New returns a new reference that gets implicitly transferred to__mypyc_empty_tuple__.

@BobTheBuidler
Copy link
ContributorAuthor

Thanks for your feedbacks, I've updated the PR accordingly.

Copy link
Collaborator

@JukkaLJukkaL left a comment

Choose a reason for hiding this comment

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

Found one more thing.

size:Value=Integer(len(items),c_pyssize_t_rprimitive)
returnself.call_c(new_tuple_op, [size]+items,line)
else:
returnself.call_c(load_empty_tuple_constant_op, [],line)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also use the new primitive innew_tuple_with_length below if length == 0.

Copy link
ContributorAuthor

@BobTheBuidlerBobTheBuidlerAug 27, 2025
edited
Loading

Choose a reason for hiding this comment

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

I'm not sure how to implement this logic at compile time because in this location,length is a Value not a known integer

we could do a boolean op to check, but then we're just recreating what already exists in PyTuple_New and making our IR longer to do it

Copy link
Collaborator

@JukkaLJukkaLAug 27, 2025
edited
Loading

Choose a reason for hiding this comment

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

You are correct, please disregard my above comment.

The new function could be used inmypyc/codegen/emit.py on this line when generating C for a box operation:self.emit_line(f"{declaration}{dest} = PyTuple_New({len(typ.types)});"). If length is zero, we could use the new primitive?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

looks correct. We can also skip the subsequent NULL check. I've implemented this and the tests are running, I may need to update the IR test definitons.

Copy link
Collaborator

@JukkaLJukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, looks good now!

@JukkaLJukkaL merged commite0ce3e3 intopython:masterAug 27, 2025
13 checks passed
@BobTheBuidlerBobTheBuidler deleted the empty-tuple-constant branchAugust 27, 2025 17:48
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JukkaLJukkaLJukkaL approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@BobTheBuidler@JukkaL

[8]ページ先頭

©2009-2025 Movatter.jp