Skip to content

bpo-26110: Add CALL_METHOD_KW opcode to speedup method calls with keywords #26014

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

Merged
merged 18 commits into from
May 15, 2021

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented May 10, 2021

This is the keyword counterpart for CALL_METHOD (which is only emitted for positional only method calls). The original CALL_METHOD was inspired by PyPy. Not documented there is that PyPy also has a CALL_METHOD_KW for the same purpose.

On microbenchmarks, there is a 1.2x speedup similar to the original patch by Yury for things like X.b(1, 2, c=3, d=4). On pyperformance, it isn't as noticeable -- mostly because pyperformance doesn't use keyword argument calls.

If accepted, docs and What's New (if necessary) will be a later patch.

A side effect of this is that CALL_METHOD should also be slightly faster due to the elimination of a branch.

https://bugs.python.org/issue26110

@Fidget-Spinner
Copy link
Member Author

Fidget-Spinner commented May 10, 2021

I'll post pyperformance results here shortly (hopefully by tomorrow).

Edit1: non-scientific timeit microbenchmark results (unfortunately not pyperf timeit :( ) show a roughly 25% time reduction:

 python.exe -m timeit -s "
>> class X:
>>  def foo(self, b, *, c=1, d=2): ...
>> x = X()
>> " "x.foo(2,c=3,d=4)"

# Without the patch (from main):
2000000 loops, best of 5: 146 nsec per loop

# With the CALL_METHOD_KW patch applied:
2000000 loops, best of 5: 107 nsec per loop

Edit2: pyperformance shows no significant change (once again likely because keyword args in method calls aren't common in pyperformance).

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good in general.
A couple of very minor issues

@Fidget-Spinner Fidget-Spinner marked this pull request as draft May 14, 2021 17:30
@Fidget-Spinner Fidget-Spinner marked this pull request as ready for review May 14, 2021 17:57
@Fidget-Spinner
Copy link
Member Author

Mark, thanks for taking the time to review this patch. I really appreciate the feedback (it also led to some really interesting discoveries).

I have made the requested changes; please review again

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

One unused variable. Otherwise good.

You can change STACK_SHRINK in this PR, or a new one. Up to you.

@markshannon
Copy link
Member

Great.

@markshannon markshannon merged commit f24afda into python:main May 15, 2021
@Fidget-Spinner
Copy link
Member Author

Thanks for all the reviews Mark! I'll watch the buildbots and hopefully nothing bad happens.

@Fidget-Spinner Fidget-Spinner deleted the call_method_kw_final branch May 15, 2021 15:24
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.

3 participants