Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix LruCache callback deduplication #6213

Merged
merged 8 commits into from
Nov 7, 2019
Merged

Conversation

V02460
Copy link
Contributor

@V02460 V02460 commented Oct 17, 2019

The PR fixes the deduplication issues for memoizing caches. It makes all identical _CacheContext objects share the same invalidate function. This is required for proper deduplication of invalidate callbacks in LruCache. LruCache is used for the @cached function decorator. Fixes #6200.

LruCache relies on the equality of callback functions for deduplication. This is in contrast to what was annotated in _CacheContext which claims that LruCache relies on the equality of _CacheContext objects itself.

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file
  • Pull request includes a sign off

Signed-off-by: Kai A. Hiller <KaiAlexHiller@web.de>
Kai A. Hiller added 2 commits October 17, 2019 21:14
Signed-off-by: Kai A. Hiller <KaiAlexHiller@web.de>
Signed-off-by: Kai A. Hiller <KaiAlexHiller@web.de>
@V02460 V02460 mentioned this pull request Oct 24, 2019
@hawkowl hawkowl requested a review from a team October 24, 2019 17:41
@richvdh
Copy link
Member

richvdh commented Oct 29, 2019

yup, so this has been changed by python/cpython@ac20e0f#diff-8bd563e1946aef62abb51ecc6d162eaf.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

thank you very much for the contribution!

This is in contrast to what was annotated in _CacheContext which claims that LruCache relies on the equality of _CacheContext objects itself.

well, it was kind of true, in that the equality of the invalidate methods depended on the equality of the _CacheContext objects, though I agree it wasn't very clear.

Anyway: I think this might be more intuitively solved by ensuring that we don't create duplicate _CacheContext objects, so:

  • leave _CacheContext as it was
  • create in CacheDescriptor.__get__, do cache_contexts = {}
  • at line 433, do kwargs["cache_context"] = cache_contexts.setdefault(cache_key, _CacheContext(cache, cache_key))

wdyt?

@richvdh richvdh self-assigned this Oct 29, 2019
@V02460
Copy link
Contributor Author

V02460 commented Oct 31, 2019

Ensuring _CacheContext objects are not duplicated seems saner to me as well, right.

I'd argue that it's not CacheDescriptor's responsibility to manage caching of _CacheContext instances. Because of this and to not clutter CacheDescriptor even more, it might be better to put that code into a get_instance factory method of _CacheContext instead.

I think we still need to use a WeakValueDictionary instead of a normal dict as you suggested. For every new argument of a method call a _CacheContext object is created, so these become many and really should be released when no longer used.

@richvdh
Copy link
Member

richvdh commented Oct 31, 2019

I'd argue that it's not CacheDescriptor's responsibility to manage caching of _CacheContext instances. Because of this and to not clutter CacheDescriptor even more, it might be better to put that code into a get_instance factory method of _CacheContext instead.

ok, that sounds reasonable.

I think we still need to use a WeakValueDictionary instead of a normal dict as you suggested. For every new argument of a method call a _CacheContext object is created, so these become many and really should be released when no longer used.

I think you're right. I hadn't quite thought that part through!

Signed-off-by: Kai A. Hiller <KaiAlexHiller@web.de>
@richvdh richvdh self-requested a review November 4, 2019 18:15
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

lgtm. I've merged latest develop into the branch to try to get the CI to sort itself out.

@richvdh richvdh self-requested a review November 6, 2019 16:26
@richvdh
Copy link
Member

richvdh commented Nov 7, 2019

🎉

@richvdh richvdh merged commit affcc2c into matrix-org:develop Nov 7, 2019
@richvdh
Copy link
Member

richvdh commented Nov 7, 2019

thank you very much for contributing this and for working through the pain from the CI!

@auscompgeek
Copy link
Contributor

FYI you didn't have to turn the function annotations into comments - that's valid Python 3 syntax.

@V02460
Copy link
Contributor Author

V02460 commented Nov 8, 2019

Oh thank you, I didn't know function annotation syntax was available earlier (PEP 3107, Python 3.0) than variable annotation syntax (PEP 526, Python 3.6).

babolivier pushed a commit that referenced this pull request Sep 1, 2021
* commit 'affcc2cc3':
  Fix LruCache callback deduplication (#6213)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants