Skip to content

[mypyc] Use native calls to singledispatch functions #10981

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

Conversation

pranavrajpal
Copy link
Contributor

Fixes mypyc/mypyc#881

This makes generated code use native calls to singledispatch functions whenever possible, which makes singledispatch functions faster.

Benchmark results

This PR:

running sum_tree_singledispatch
..........
interpreted: 0.593835s (avg of 5 iterations; stdev 2.4%)
compiled:    0.044264s (avg of 5 iterations; stdev 2.1%)

compiled is 13.416x faster

Current master (4c90aa4):

running sum_tree_singledispatch
..........
interpreted: 0.588636s (avg of 5 iterations; stdev 1.4%)
compiled:    0.059705s (avg of 5 iterations; stdev 1.5%)

compiled is 9.859x faster

Test Plan

I added an irbuild test to make sure that we were using native calls to singledispatch functions.

mypyc doesn't have support for calling callable classes without going
through the python API, so instead of having to special case the
singledispatch callable class when we want to generate a native call to
it, we create a glue function that loads the callable class from the
globals dict and then makes a native call to that callable class's
__call__ method.
Use a native call to the glue function that we generate for
singledispatch functions instead of calling those singledispatch
functions through the python API.
Add an irbuild test to make sure we use native calls to the glue
functions for singledispatch functions, and that the glue functions make
native calls to the __call__ methods on the callable classes.
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

Woo, really exciting speedup

@msullivan msullivan merged commit cd013d1 into python:master Aug 16, 2021
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 16, 2021

I love the speedup!

sthagen added a commit to sthagen/python-mypy that referenced this pull request Aug 16, 2021
[mypyc] Use native calls to singledispatch functions (python#10981)
@pranavrajpal pranavrajpal deleted the native-calls-to-singledispatch-functions branch August 16, 2021 15:14
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.

Call singledispatch functions directly as C functions?
3 participants