[Python-checkins] r84432 - in python/branches/py3k/Lib: collections.py functools.py

raymond.hettinger python-checkins at python.org
Thu Sep 2 11:44:29 CEST 2010


Author: raymond.hettinger
Date: Thu Sep  2 11:44:28 2010
New Revision: 84432

Log:
Speed-up cache updates

Modified:
   python/branches/py3k/Lib/collections.py
   python/branches/py3k/Lib/functools.py

Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py	(original)
+++ python/branches/py3k/Lib/collections.py	Thu Sep  2 11:44:28 2010
@@ -161,6 +161,19 @@
     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

Modified: python/branches/py3k/Lib/functools.py
==============================================================================
--- python/branches/py3k/Lib/functools.py	(original)
+++ python/branches/py3k/Lib/functools.py	Thu Sep  2 11:44:28 2010
@@ -139,8 +139,7 @@
             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)


More information about the Python-checkins mailing list