You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
singledispatchmethod significantly slower than singledispatch #85160
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?
Trying this simple patch
diff --git a/Lib/functools.py b/Lib/functools.py
index 5cab497..e42f485 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -900,6 +900,7 @@ class singledispatchmethod:
#107148 significantly optimised @singledispatchmethod for all non-slotted classes. If anybody can think of further optimisations that can be safely applied, I suggest a new issue should be opened for them.
Activity
CaselIT commentedon Jun 15, 2020
The implementation of singledispatchmethod is significantly slower (~4x) than the normal singledispatch version
Using timeit to test this example case:
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?
Trying this simple patch
improves the performance noticeably
iritkatriel commentedon Sep 9, 2022
I see a similar time difference on main.
gh-85160: improve performance of `functools.singledispatchmethod` (#1…
AlexWaygood commentedon Aug 6, 2023
#107148 significantly optimised
@singledispatchmethodfor all non-slotted classes. If anybody can think of further optimisations that can be safely applied, I suggest a new issue should be opened for them.Thanks @casselt for the report, and thanks @mental32 and @eendebakpt for working on this!
pythongh-85160: Reduce memory usage of `singledispatchmethod` when in…
gh-85160: Reduce memory usage of `singledispatchmethod` (#107706)
pythongh-127750: Fix and optimize functools.singledispatchmethod()
gh-127750: Fix and optimize functools.singledispatchmethod() (GH-130008)