Skip to content

gh-85160: Reduce memory usage of singledispatchmethod when owner instances cannot be weakref'd #107706

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

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,14 +928,14 @@ class singledispatchmethod:
"""

def __init__(self, func):
import weakref # see comment in singledispatch function
if not callable(func) and not hasattr(func, "__get__"):
raise TypeError(f"{func!r} is not callable or a descriptor")

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

import weakref # see comment in singledispatch function
self._method_cache = weakref.WeakKeyDictionary()
self._all_weakrefable_instances = True

def register(self, cls, method=None):
"""generic_method.register(cls, func) -> func
Expand All @@ -945,11 +945,11 @@ def register(self, cls, method=None):
return self.dispatcher.register(cls, func=method)

def __get__(self, obj, cls=None):
if self._all_weakrefable_instances:
if self._method_cache is not None:
try:
_method = self._method_cache[obj]
except TypeError:
self._all_weakrefable_instances = False
self._method_cache = None
except KeyError:
pass
else:
Expand All @@ -963,7 +963,7 @@ def _method(*args, **kwargs):
_method.register = self.register
update_wrapper(_method, self.func)

if self._all_weakrefable_instances:
if self._method_cache is not None:
self._method_cache[obj] = _method

return _method
Expand Down