[issue35780] Recheck logic in the C version of the lru_cache()

Raymond Hettinger report at bugs.python.org
Sat Jan 19 04:31:07 EST 2019


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

---------  Demonstration of one of the bugs ---------

# The currsize is initially equal to maxsize of 10
# Then we cause an orphan link in a full cache 
# The currsize drops to 9 and never recovers the full size of 10

from functools import lru_cache

once = True

@lru_cache(maxsize=10)
def f(x):
    global once
    rv = f'.{x}.'
    if x == 20 and once:
        once = False
        print('Calling again', f(x))
    return rv

for x in range(15):
    f(x)

print(f.cache_info())
print(f(20))
print(f.cache_info())
print(f(21))
print(f.cache_info())


------ Output --------
CacheInfo(hits=0, misses=15, maxsize=10, currsize=10)
Calling again .20.
.20.
CacheInfo(hits=0, misses=17, maxsize=10, currsize=9)
.21.
CacheInfo(hits=0, misses=18, maxsize=10, currsize=9)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35780>
_______________________________________


More information about the Python-bugs-list mailing list