]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Speed-up cache updates
authorRaymond Hettinger <python@rcn.com>
Thu, 2 Sep 2010 09:44:28 +0000 (09:44 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 2 Sep 2010 09:44:28 +0000 (09:44 +0000)
Lib/collections.py
Lib/functools.py

index 1a43afbe894fbf850f6b9c25c1e42ad09835101c..6daa5967c1205f789210d7e956058f5825bb0f37 100644 (file)
@@ -161,6 +161,19 @@ class OrderedDict(dict, MutableMapping):
     def __del__(self):
         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
index 1bbc520e7c3b8b18dc80acd9ca226de33f5a82dd..f9e35d857ab44088a4f3317046487620736295a9 100644 (file)
@@ -139,8 +139,7 @@ def lru_cache(maxsize=100):
             try:
                 with lock:
                     result = cache[key]
-                    del cache[key]
-                    cache[key] = result         # record recent use of this key
+                    cache._move_to_end(key)     # record recent use of this key
                     wrapper.hits += 1
             except KeyError:
                 result = user_function(*args, **kwds)