Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
04fe64e
bpo-40988: Optimized singledispatchmethod access.
mental32 Nov 10, 2020
fc72bea
Merge branch 'main' into bpo-40988
AlexWaygood Jul 5, 2023
31d292b
Merge branch 'main' into singledispatchmethod3
eendebakpt Jul 23, 2023
5ae8348
add test for slotted classes
eendebakpt Jul 5, 2023
1edc0e3
add test for slotted classes
eendebakpt Jul 23, 2023
9a1ffae
update new entry with attribution to original author
eendebakpt Jul 23, 2023
3588943
add test for assignment to dispatched methods
eendebakpt Jul 23, 2023
af233d4
typo
eendebakpt Jul 23, 2023
d5aebdf
Update Lib/test/test_functools.py
eendebakpt Jul 23, 2023
b773f82
apply review suggestion
eendebakpt Jul 23, 2023
25e1915
Merge branch 'main' into singledispatchmethod3
eendebakpt Jul 23, 2023
593c923
Update Lib/functools.py
eendebakpt Jul 23, 2023
39bec6d
Update Lib/functools.py
eendebakpt Jul 23, 2023
d954244
use hasattr to check for slotted types
eendebakpt Jul 24, 2023
df2eeb5
make attrname private
eendebakpt Jul 24, 2023
774ec3c
review comments
eendebakpt Jul 24, 2023
f03ebba
use weakref for caching
eendebakpt Jul 30, 2023
b459718
Merge branch 'singledispatchmethod3' of githubeendebakpt:eendebakpt/c…
eendebakpt Jul 30, 2023
a422989
Merge branch 'main' into singledispatchmethod3
eendebakpt Jul 30, 2023
450ea12
make patchcheck
eendebakpt Jul 31, 2023
032ab24
review comments
eendebakpt Jul 31, 2023
18d7e43
Apply suggestions from code review
eendebakpt Jul 31, 2023
755efdd
Merge branch 'main' into singledispatchmethod3
eendebakpt Jul 31, 2023
56d342c
Ensure we only weakref() `obj` once
AlexWaygood Jul 31, 2023
a119e21
Get rid of the cache local variable
AlexWaygood Jul 31, 2023
47c18a5
Don't assign `caching` in the fast path
AlexWaygood Jul 31, 2023
2031680
Merge pull request #5 from AlexWaygood/even-more-singledispatchmethod…
eendebakpt Jul 31, 2023
ca691e8
remove check on obj being None
eendebakpt Jul 31, 2023
e8eeedd
eliminate caching variable
eendebakpt Jul 31, 2023
307f0c1
Merge pull request #6 from eendebakpt/singledispatchmethod3b
eendebakpt Aug 1, 2023
afea7b9
Test slotted staticmethods on instances as well as the class
AlexWaygood Aug 1, 2023
b43dcee
eliminate method variable and reduce attribute lookups
eendebakpt Aug 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-40988: Optimized singledispatchmethod access.
  • Loading branch information
mental32 committed Nov 10, 2020
commit 04fe64e5c261688149c28cb56db71f13d51dc8b5
13 changes: 13 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ def __init__(self, func):

self.dispatcher = singledispatch(func)
self.func = func
self.attrname = None

def register(self, cls, method=None):
"""generic_method.register(cls, func) -> func
Expand All @@ -909,6 +910,9 @@ def register(self, cls, method=None):
"""
return self.dispatcher.register(cls, func=method)

def __set_name__(self, _, name):
self.attrname = name

def __get__(self, obj, cls=None):
def _method(*args, **kwargs):
method = self.dispatcher.dispatch(args[0].__class__)
Expand All @@ -917,6 +921,15 @@ def _method(*args, **kwargs):
_method.__isabstractmethod__ = self.__isabstractmethod__
_method.register = self.register
update_wrapper(_method, self.func)

if self.attrname is not None:
attrname = self.attrname

try:
obj.__dict__[attrname] = _method
except AttributeError:
pass # not all objects have __dict__ (e.g. class defines slots)

return _method

@property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Applied a cached_propery type optimization suggested by federico to
singledispatchmethod yielding approximately a 3.5 speedup in access times.