@@ -168,15 +168,14 @@ def lru_cache(maxsize=100, typed=False):
168168 # to allow the implementation to change (including a possible C version).
169169
170170 # Constants shared by all lru cache instances:
171- kwd_mark = (object (),) # separate positional and keyword args
172- sentinel = object () # unique object used to signal cache misses
173- _len = len # localize the global len() function
171+ kwd_mark = (object (),) # separate positional and keyword args
172+ sentinel = object () # unique object used to signal cache misses
174173 PREV , NEXT , KEY , RESULT = 0 , 1 , 2 , 3 # names for the link fields
175174
176175 def decorating_function (user_function ):
177176
178177 cache = {}
179- hits = misses = 0
178+ hits = misses = currsize = 0
180179 cache_get = cache .get # bound method to lookup a key or return None
181180 lock = Lock () # because linkedlist updates aren't threadsafe
182181 root = [] # root of the circular doubly linked list
@@ -209,7 +208,7 @@ def wrapper(*args, **kwds):
209208
210209 def wrapper (* args , ** kwds ):
211210 # simple caching without ordering or size limit
212- nonlocal hits , misses
211+ nonlocal hits , misses , currsize
213212 key = make_key (args , kwds , typed ) if kwds or typed else args
214213 result = cache_get (key , sentinel )
215214 if result is not sentinel :
@@ -218,13 +217,14 @@ def wrapper(*args, **kwds):
218217 result = user_function (* args , ** kwds )
219218 cache [key ] = result
220219 misses += 1
220+ currsize += 1
221221 return result
222222
223223 else :
224224
225225 def wrapper (* args , ** kwds ):
226226 # size limited caching that tracks accesses by recency
227- nonlocal root , hits , misses
227+ nonlocal root , hits , misses , currsize
228228 key = make_key (args , kwds , typed ) if kwds or typed else args
229229 with lock :
230230 link = cache_get (key )
@@ -241,11 +241,12 @@ def wrapper(*args, **kwds):
241241 return result
242242 result = user_function (* args , ** kwds )
243243 with lock :
244- if _len ( cache ) < maxsize :
244+ if currsize < maxsize :
245245 # put result in a new link at the front of the queue
246246 last = root [PREV ]
247247 link = [last , root , key , result ]
248248 cache [key ] = last [NEXT ] = root [PREV ] = link
249+ currsize += 1
249250 else :
250251 # use root to store the new key and result
251252 root [KEY ] = key
@@ -261,15 +262,15 @@ def wrapper(*args, **kwds):
261262 def cache_info ():
262263 """Report cache statistics"""
263264 with lock :
264- return _CacheInfo (hits , misses , maxsize , len ( cache ) )
265+ return _CacheInfo (hits , misses , maxsize , currsize )
265266
266267 def cache_clear ():
267268 """Clear the cache and cache statistics"""
268- nonlocal hits , misses
269+ nonlocal hits , misses , currsize
269270 with lock :
270271 cache .clear ()
271272 root [:] = [root , root , None , None ]
272- hits = misses = 0
273+ hits = misses = currsize = 0
273274
274275 wrapper .cache_info = cache_info
275276 wrapper .cache_clear = cache_clear
0 commit comments