reimplements some methods to traverse operations in reverse order, much efficient w/ transactions where there is a lot of pending operation stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 15 Oct 2009 20:30:40 +0200
branchstable
changeset 3691 ccd72f500daa
parent 3690 a5ef45850d23
child 3692 54aa8d13aab4
reimplements some methods to traverse operations in reverse order, much efficient w/ transactions where there is a lot of pending operation
server/pool.py
--- a/server/pool.py	Thu Oct 15 20:29:51 2009 +0200
+++ b/server/pool.py	Thu Oct 15 20:30:40 2009 +0200
@@ -180,10 +180,15 @@
         """return the index of  the lastest instance which is not a
         LateOperation instance
         """
-        for i, op in enumerate(self.session.pending_operations):
+        # faster by inspecting operation in reverse order for heavy transactions
+        i = None
+        for i, op in enumerate(reversed(self.session.pending_operations)):
             if isinstance(op, (LateOperation, SingleLastOperation)):
-                return i
-        return None
+                continue
+            return -i or None
+        if i is None:
+            return None
+        return -(i + 1)
 
     def handle_event(self, event):
         """delegate event handling to the opertaion"""
@@ -238,10 +243,15 @@
         """return the index of  the lastest instance which is not a
         SingleLastOperation instance
         """
-        for i, op in enumerate(self.session.pending_operations):
+        # faster by inspecting operation in reverse order for heavy transactions
+        i = None
+        for i, op in enumerate(reversed(self.session.pending_operations)):
             if isinstance(op, SingleLastOperation):
-                return i
-        return None
+                continue
+            return -i or None
+        if i is None:
+            return None
+        return -(i + 1)
 
 
 class SingleOperation(Operation):
@@ -261,10 +271,9 @@
 
     def equivalent_index(self, operations):
         """return the index of the equivalent operation if any"""
-        equivalents = [i for i, op in enumerate(operations)
-                       if op.__class__ is self.__class__]
-        if equivalents:
-            return equivalents[0]
+        for i, op in enumerate(reversed(operations)):
+            if op.__class__ is self.__class__:
+                return -(i+1)
         return None