Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self._operations should use a deque() which is O(1) #9

Open
lukemarsden opened this issue Mar 10, 2013 · 2 comments
Open

self._operations should use a deque() which is O(1) #9

lukemarsden opened this issue Mar 10, 2013 · 2 comments

Comments

@lukemarsden
Copy link

I've just fixed this in a sibling of the txMySQL codebase, due to O(N) performance append()ing and pop(0)ing from lists, this should be a decent performance win in any case where there are around > 1000 queued queries.

@lukemarsden
Copy link
Author

NB In txMySQL this is spelled self._pending_operations.

@lukemarsden
Copy link
Author

The following patch gives the gist:

+from collections import deque

 CancellableDeferredSemaphore = CancellableDeferredSemaphore
 SPREAD_PORT = 16171
@@ -499,7 +500,7 @@ class QueueEnabledMixin(object):
             from twisted.internet import reactor
             clock = reactor
         self.clock = clock
-        self._operations = []
+        self._operations = deque()
         self._current_operation = None
         self._logTag = logTag
         # For debugging
@@ -516,8 +517,8 @@ class QueueEnabledMixin(object):
         """
         HybridUtils.channelLog(self._logTag,
                 "in _do_operation, self._operations "
-                "is %r self._current_operation is %r" % (
-                    self._operations, self._current_operation))
+                "is %d long self._current_operation is %r" % (
+                    len(self._operations), self._current_operation))
         d = defer.Deferred()
         self._operations.append((d, func, a, kw))
         if self._current_operation is None:
@@ -575,7 +576,7 @@ class QueueEnabledMixin(object):
                 "is %r self._current_operation is %r" % (
                     self._operations, self._current_operation))
         if self._operations:
-            d, f, a, kw = self._operations.pop(0)
+            d, f, a, kw = self._operations.popleft()
             self._current_fn = f
             self._current_args = (a, kw)
             self._current_operation = defer.maybeDeferred(f, *a, **kw)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant