@lru_cache on functions with no arguments

tom at tomforb.es tom at tomforb.es
Tue Aug 1 08:50:41 EDT 2017


Hello all,
Thank you for the replies! So, here is the context:

1. The functions Django wants to cache require Django to be initialized and the settings loaded. This means the return values are not available at definition time. (Matt Wheeler hits it on the head).

2. Django has a long-standing no-dependencies rule, which may change in the near future but for now it is stdlib only. We can't add a dependency on `lazy-property`.

This is all a bit off-topic anyway. I was merely asking about the tradeoffs for using maxsize=None vs maxsize=default in `lru_cache`.

On my Ubuntu machine I am getting some interesting benchmark results. If you disable the LRU cache C extension, so it uses the following implementations:

1. maxsize is not none: https://github.com/python/cpython/blob/master/Lib/functools.py#L526-L581

2. maxsize is none: https://github.com/python/cpython/blob/master/Lib/functools.py#L509-L522

And you have a simple function:

def test():
   return object()

I get the following numbers without much variance:

1. lru_cache(maxsize=None) - 870ns 

2. lru_cache() - 1300ns

3. no cache - 100ns

So, in the best case, without the C extension lru_cache is 8x as slow as the function itself. I get that it's not a great comparison as few functions are as simple as that, but a 700ns overhead is quite a bit, and if you're putting it around simple functions and expecting them to be faster, that may not be true.

With the C extension I get 50ns for both. So obviously a lot faster. Maybe it doesn't matter...



More information about the Python-list mailing list