[pypy-commit] pypy default: patch from raymondh to optimize deque.remove in lib_pypy/_collections.py (normally overridden by builtin _collections)

bdkearns noreply at buildbot.pypy.org
Sat Feb 9 07:41:51 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r61000:5e3878959d9b
Date: 2013-02-09 01:21 -0500
http://bitbucket.org/pypy/pypy/changeset/5e3878959d9b/

Log:	patch from raymondh to optimize deque.remove in
	lib_pypy/_collections.py (normally overridden by builtin
	_collections)

diff --git a/lib_pypy/_collections.py b/lib_pypy/_collections.py
--- a/lib_pypy/_collections.py
+++ b/lib_pypy/_collections.py
@@ -142,12 +142,18 @@
         return c
 
     def remove(self, value):
-        # Need to be defensive for mutating comparisons
-        for i in range(len(self)):
-            if self[i] == value:
-                del self[i]
-                return
-        raise ValueError("deque.remove(x): x not in deque")
+        # Need to defend mutating or failing comparisons
+        i = 0
+        try:
+            for i in range(len(self)):
+                if self[0] == value:
+                    self.popleft()
+                    return
+                self.append(self.popleft())
+            i += 1
+            raise ValueError("deque.remove(x): x not in deque")
+        finally:
+            self.rotate(i)
 
     def rotate(self, n=1):
         length = len(self)


More information about the pypy-commit mailing list