@lru_cache on functions with no arguments

codewizard at gmail.com codewizard at gmail.com
Mon Jul 31 20:00:22 EDT 2017


On Monday, July 31, 2017 at 7:31:52 PM UTC-4, t... at tomforb.es wrote:
> As part of the Python 3 cleanup in Django there are a fair few uses of @functools.lru_cache on functions that take no arguments. A lru_cache isn't strictly needed here, but it's convenient to just cache the result. Some examples are here: https://github.com/django/django/pull/8825/files
> 
> I did some profiling and I found that using `@lru_cache(maxsize=None)` on such functions is twice as fast as a standard `@lru_cache()`, apparently because with a `maxsize` the lru_cache code requires a lock acquisition and a fair bit more state to track.
> 
> Am I right in thinking that using `maxsize=None` is best for functions that accept no arguments? Should we even be using a `lru_cache` in such situations, or write our own simple cache decorator instead?

If the performance savings are real, another choice would be to improve the implementation of lru_cache to special-case no-argument functions to avoid locks, etc.



More information about the Python-list mailing list