-
Notifications
You must be signed in to change notification settings - Fork 74
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
Cythonize Dispatcher methods #24
base: main
Are you sure you want to change the base?
Conversation
I benchmarked Cythonized versions on the following dispatched functions: from multipledispatch import dispatch
@dispatch(int)
def f(x):
return x + 1
@dispatch(float)
def f(x):
return x - 1 Let's discuss times in terms of the run time of an |
Dispatcher is now derived from a C-extension base class. This improves performance two ways: 1. Being called via `__call__` is much faster 2. Attribute lookup (i.e., `_cache`, `funcs`, etc.) is much faster I also added an optimization in the Cython version of Dispatcher when dispatching for a single positional argument by not constructing a tuple for the key for `_cache`. I do, however, pass a tuple to `resolve`. Cythonized versions should not be used in PyPy. Hopefully the check works. TravisCI script was modified to test with and without using Cython, and it verifies if the C core was used when expected. Hopefully this works too.
I think this PR is almost ready to merge. It achieves the two primary objectives:
@mrocklin, I know you are busy right now, so I'm just letting you know this PR is awaiting review and feedback. New dispatching policies (as mentioned in #13) have not been explored. The |
Yeah, sorry for not responding to this. Don't mistake my delay for a lack
|
No worries! I didn't expect you to be engaged in this PR just yet--I know how busy and social the conference can be--hence my slow pace :) |
I don't know how well C extensions are supported in other implementations of Python. Hence, let's choose to do the conservative thing and only use Cythonized versions of dispatchers for CPython. Also added a Makefile for convenient building and testing.
Two burning questions:
|
Method dispatch hasn't seen nearly as much usage (by me) than function dispatch. It's likely that there are oversights like this. I don't actually exactly know what the value of cythonizing resolve is. My recollection is that using cache we get down to 1-3 microseconds per call. Maybe this is sufficient or maybe we can't improve much beyond this and so using cython is just needless complexity. My guess is that the thing to do here is to benchmark what we think of as being common cases. This is on my TODO list, but it's being pushed down by other things :( |
Alright. I'll change method dispatching to use
I still think it's amazing that cythonized dispatch only adds 60% overhead when calling a unary function, and 100% for other arities! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you mind rebasing this
This is currently a quickly implemented proof-of-concept to cythonize
Dispatcher.__call__/resolve
as discussed in #13.We will make Cython and a C compiler optional. We also still need to use Cython for
MethodDispatcher
.@mrocklin, I'm curious how much faster this is for you. I have not performed any benchmarks. I believe we can make
__call__
50-100% faster than what is currently done in this PR, but I want to make sure I'm on the right track.