From ce5e64d8937eb491708d7f4e36cf90cb3b5ad00e Mon Sep 17 00:00:00 2001 From: David Blewett Date: Tue, 12 Aug 2014 18:34:36 -0400 Subject: 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." --- statsd/client.py | 9 +++++---- 1 file 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 -- cgit v1.2.1