diff options
author | David Blewett <david@dawninglight.net> | 2014-08-12 18:34:36 -0400 |
---|---|---|
committer | David Blewett <david@dawninglight.net> | 2014-08-12 18:34:36 -0400 |
commit | ce5e64d8937eb491708d7f4e36cf90cb3b5ad00e (patch) | |
tree | b9fb8c60d9c988e4bac8b5177b138f8000702ee7 | |
parent | 1fd482a418c556a1e5ee10d375033a80c5430301 (diff) | |
download | pystatsd-ce5e64d8937eb491708d7f4e36cf90cb3b5ad00e.tar.gz |
Optimize Pipeline by using a deque
This section of code is going to be pretty hot; it's best to optimize it as much as possible.
It might be worth it to switch to a list for the data variable as well, and use '\n'.join(data) (vs. string concatenation).
From: https://docs.python.org/2/library/collections.html#collections.deque
"Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation."
-rw-r--r-- | statsd/client.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/statsd/client.py b/statsd/client.py index 4ea0b57..ecad262 100644 --- a/statsd/client.py +++ b/statsd/client.py @@ -1,4 +1,5 @@ from __future__ import with_statement +from collections import deque from functools import wraps import random import socket @@ -140,7 +141,7 @@ class Pipeline(StatsClient): self._client = client self._prefix = client._prefix self._maxudpsize = client._maxudpsize - self._stats = [] + self._stats = deque() def _after(self, data): if data is not None: @@ -153,12 +154,12 @@ class Pipeline(StatsClient): self.send() def send(self): - # Use pop(0) to preserve the order of the stats. + # Use popleft to preserve the order of the stats. if not self._stats: return - data = self._stats.pop(0) + data = self._stats.popleft() while self._stats: - stat = self._stats.pop(0) + stat = self._stats.popleft() if len(stat) + len(data) + 1 >= self._maxudpsize: self._client._after(data) data = stat |