-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
singledispatchmethod significantly slower than singledispatch #85160
Comments
The implementation of singledispatchmethod is significantly slower (~4x) than the normal singledispatch version Using timeit to test this example case: from functools import singledispatch, singledispatchmethod
import timeit
class Test:
@singledispatchmethod
def go(self, item, arg):
print('general')
@go.register
def _(self, item:int, arg):
return item + arg
@singledispatch
def go(item, arg):
print('general')
@go.register
def _(item:int, arg):
return item + arg
Prints on my system.
Looking at the singledispatchmethod implementation I believe that most of the difference is because a new function is generated every time the method is called. Maybe an implementation similar to cached_property could be used if the class has __dict__ attribute?
self.dispatcher = singledispatch(func)
self.func = func
+ self.attrname = None
def register(self, cls, method=None):
"""generic_method.register(cls, func) -> func
@@ -908,6 +909,10 @@ class singledispatchmethod:
"""
return self.dispatcher.register(cls, func=method)
improves the performance noticeably
|
I see a similar time difference on main. |
…07148) Co-authored-by: mental <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
#107148 significantly optimised Thanks @casselt for the report, and thanks @mental32 and @eendebakpt for working on this! |
…stances cannot be weakref'd
A small followup to #107148 Co-authored-by: Serhiy Storchaka <[email protected]>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
singledispatchmethod
when owner instances cannot be weakref'd #107706The text was updated successfully, but these errors were encountered: