Ensure we only weakref() obj once#5
Merged
eendebakpt merged 3 commits intoeendebakpt:singledispatchmethod3from Jul 31, 2023
Merged
Conversation
eendebakpt
reviewed
Jul 31, 2023
eendebakpt
reviewed
Jul 31, 2023
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 |
Owner
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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))
eendebakpt
reviewed
Jul 31, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The performance characteristics of
WeakKeyDictionary()seem pretty different todict, so going back to atry/except KeyErrorstructure actually seems much more performant now we're using aWeakKeyDictionaryfor the non-slotted case, and doesn't seem to cost us anything for the slotted case