Skip to content

Conversation

@AlexWaygood
Copy link

The performance characteristics of WeakKeyDictionary() seem pretty different to dict, so going back to a try/except KeyError structure actually seems much more performant now we're using a WeakKeyDictionary for the non-slotted case, and doesn't seem to cost us anything for the slotted case

Lib/functools.py Outdated
def __get__(self, obj, cls=None):
if self._all_weakrefable_instances and cls is not None and obj in self._method_cache:
return self._method_cache[obj]
caching = self._all_weakrefable_instances and obj is not None
Copy link
Owner

Choose a reason for hiding this comment

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

This does no longer check for cls is not None. Is that ok? I could not construct any test cases where cls = None, so in my PR i kept it in.

Copy link
Author

Choose a reason for hiding this comment

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

I think it's okay. The tests I added in this PR fail if we don't do the if obj is not None check. But, like you, I couldn't come up with a test case where cls=None. I'm also not actually sure why it would be a problem if cls=None for our caching here.

Copy link
Author

Choose a reason for hiding this comment

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

https://docs.python.org/3/howto/descriptor.html#overview-of-descriptor-invocation appears to confirm that desc.__get__(obj, None) is semantically equivalent to desc.__get__(obj, type(obj))

@AlexWaygood AlexWaygood requested a review from eendebakpt July 31, 2023 14:22
@eendebakpt eendebakpt merged commit 2031680 into eendebakpt:singledispatchmethod3 Jul 31, 2023
@AlexWaygood AlexWaygood deleted the even-more-singledispatchmethod-perf branch July 31, 2023 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants