mirror of
https://github.com/python/cpython.git
synced 2025-10-08 16:11:51 +00:00
Use a flag to indicate when the circular queue is fully populated and stable.
This commit is contained in:
parent
417c3848d5
commit
018b4fbb9b
1 changed files with 12 additions and 9 deletions
|
@ -176,6 +176,7 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
|
|
||||||
cache = {}
|
cache = {}
|
||||||
hits = misses = currsize = 0
|
hits = misses = currsize = 0
|
||||||
|
full = False
|
||||||
cache_get = cache.get # bound method to lookup a key or return None
|
cache_get = cache.get # bound method to lookup a key or return None
|
||||||
lock = Lock() # because linkedlist updates aren't threadsafe
|
lock = Lock() # because linkedlist updates aren't threadsafe
|
||||||
root = [] # root of the circular doubly linked list
|
root = [] # root of the circular doubly linked list
|
||||||
|
@ -224,7 +225,7 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
|
|
||||||
def wrapper(*args, **kwds):
|
def wrapper(*args, **kwds):
|
||||||
# size limited caching that tracks accesses by recency
|
# size limited caching that tracks accesses by recency
|
||||||
nonlocal root, hits, misses, currsize
|
nonlocal root, hits, misses, currsize, full
|
||||||
key = make_key(args, kwds, typed) if kwds or typed else args
|
key = make_key(args, kwds, typed) if kwds or typed else args
|
||||||
with lock:
|
with lock:
|
||||||
link = cache_get(key)
|
link = cache_get(key)
|
||||||
|
@ -247,13 +248,7 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
# update is already done, we need only return the
|
# update is already done, we need only return the
|
||||||
# computed result and update the count of misses.
|
# computed result and update the count of misses.
|
||||||
pass
|
pass
|
||||||
if currsize < maxsize:
|
elif full:
|
||||||
# put result in a new link at the front of the queue
|
|
||||||
last = root[PREV]
|
|
||||||
link = [last, root, key, result]
|
|
||||||
cache[key] = last[NEXT] = root[PREV] = link
|
|
||||||
currsize += 1
|
|
||||||
else:
|
|
||||||
# use root to store the new key and result
|
# use root to store the new key and result
|
||||||
root[KEY] = key
|
root[KEY] = key
|
||||||
root[RESULT] = result
|
root[RESULT] = result
|
||||||
|
@ -262,6 +257,13 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
root = root[NEXT]
|
root = root[NEXT]
|
||||||
del cache[root[KEY]]
|
del cache[root[KEY]]
|
||||||
root[KEY] = root[RESULT] = None
|
root[KEY] = root[RESULT] = None
|
||||||
|
else:
|
||||||
|
# put result in a new link at the front of the queue
|
||||||
|
last = root[PREV]
|
||||||
|
link = [last, root, key, result]
|
||||||
|
cache[key] = last[NEXT] = root[PREV] = link
|
||||||
|
currsize += 1
|
||||||
|
full = (currsize == maxsize)
|
||||||
misses += 1
|
misses += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -272,11 +274,12 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
|
|
||||||
def cache_clear():
|
def cache_clear():
|
||||||
"""Clear the cache and cache statistics"""
|
"""Clear the cache and cache statistics"""
|
||||||
nonlocal hits, misses, currsize
|
nonlocal hits, misses, currsize, full
|
||||||
with lock:
|
with lock:
|
||||||
cache.clear()
|
cache.clear()
|
||||||
root[:] = [root, root, None, None]
|
root[:] = [root, root, None, None]
|
||||||
hits = misses = currsize = 0
|
hits = misses = currsize = 0
|
||||||
|
full = False
|
||||||
|
|
||||||
wrapper.cache_info = cache_info
|
wrapper.cache_info = cache_info
|
||||||
wrapper.cache_clear = cache_clear
|
wrapper.cache_clear = cache_clear
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue