FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-85160: Optimized singledispatchmethod access (noticeable improvement). by mental32 · Pull Request #23213 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions Lib/functools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,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 @@ -941,6 +942,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 @@ -949,6 +953,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)
Comment on lines +957 to +963

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
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)
if self.attrname is not None:
try:
obj.__dict__[self.attrname] = _method
except AttributeError:
pass # not all objects have __dict__ (e.g. class defines slots)


return _method

@property
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Back | FazBrowse Home | New Git URL