This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author thesheep
Recipients thesheep
Date 2013-12-02.10:27:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1385980067.33.0.547233710394.issue19859@psf.upfronthosting.co.za>
In-reply-to
Content
As most naïve "memoized" decorator implementations, lru_cache keeps references to all the values of arguments of the decorated function in the cache. That means, that if we call such a decorated function with an object as a parameter, that object will be kept alive in memory forever -- that is, until the program ends. This is an obvious waste, since when we no longer have any other reference to that object, we are unable to call that function with the same parameter ever again, so it just wastes the cache space.

This is a very common case when we decorate a method -- the first parameter is "self". One solution for this particular case is to use a dedicated "memoized_method" decorator, that stores the cache on the "self" object itself, so that it can be released together with the object.

A more general solution uses weakrefs where possible in the cache key, maybe even with an additional callback that removes the cache entry when any of its parameters is dead. Obviously it adds some overhead and makes the caching decorator even slower, but it can let us save a lot of memory, especially in long-running applications.

To better illustrate what I mean, here is an example of such an improved @memoized decorator that I wrote: https://review.openstack.org/#/c/54117/5/horizon/utils/memoized.py

It would be great to have an option to do something similar with lru_cache, and if there is an interest, I would like to work on that.
History
Date User Action Args
2013-12-02 10:27:47thesheepsetrecipients: + thesheep
2013-12-02 10:27:47thesheepsetmessageid: <1385980067.33.0.547233710394.issue19859@psf.upfronthosting.co.za>
2013-12-02 10:27:47thesheeplinkissue19859 messages
2013-12-02 10:27:46thesheepcreate