mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Speed-up cache updates
This commit is contained in:
parent
ccb90e3ccd
commit
38d17e3df0
2 changed files with 14 additions and 2 deletions
|
@ -161,6 +161,19 @@ class OrderedDict(dict, MutableMapping):
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.clear() # eliminate cyclical references
|
self.clear() # eliminate cyclical references
|
||||||
|
|
||||||
|
def _move_to_end(self, key, PREV=0, NEXT=1):
|
||||||
|
'Fast version of self[key]=self.pop(key). Private method for internal use.'
|
||||||
|
link = self.__map[key]
|
||||||
|
link_prev = link[PREV]
|
||||||
|
link_next = link[NEXT]
|
||||||
|
link_prev[NEXT] = link_next
|
||||||
|
link_next[PREV] = link_prev
|
||||||
|
root = self.__root
|
||||||
|
last = root[PREV]
|
||||||
|
link[PREV] = last
|
||||||
|
link[NEXT] = root
|
||||||
|
last[NEXT] = root[PREV] = link
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
### namedtuple
|
### namedtuple
|
||||||
|
|
|
@ -139,8 +139,7 @@ def lru_cache(maxsize=100):
|
||||||
try:
|
try:
|
||||||
with lock:
|
with lock:
|
||||||
result = cache[key]
|
result = cache[key]
|
||||||
del cache[key]
|
cache._move_to_end(key) # record recent use of this key
|
||||||
cache[key] = result # record recent use of this key
|
|
||||||
wrapper.hits += 1
|
wrapper.hits += 1
|
||||||
except KeyError:
|
except KeyError:
|
||||||
result = user_function(*args, **kwds)
|
result = user_function(*args, **kwds)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue