From ced17c6881883ed5f07c3dc43e5d79d702bd73c1 Mon Sep 17 00:00:00 2001 From: Ulrik Mikaelsson Date: Tue, 26 Nov 2019 23:55:59 +0100 Subject: [PATCH] stream/ws_client: Use StringIO for WSClient._all bytes() += bytes() copies both buffers into a new one, causing exponential cost and gradual slow-down. Replacing with StringIO improves that --- stream/ws_client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stream/ws_client.py b/stream/ws_client.py index 7f0412060..775849d00 100644 --- a/stream/ws_client.py +++ b/stream/ws_client.py @@ -24,6 +24,7 @@ import six import yaml from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse +from six import StringIO from websocket import WebSocket, ABNF, enableTrace @@ -47,7 +48,7 @@ class WSClient: header = [] self._connected = False self._channels = {} - self._all = "" + self._all = StringIO() # We just need to pass the Authorization, ignore all the other # http headers we get from the generated code @@ -157,8 +158,8 @@ class WSClient: TODO: Maybe we can process this and return a more meaningful map with channels mapped for each input. """ - out = self._all - self._all = "" + out = self._all.getvalue() + self._all = self._all.__class__() self._channels = {} return out @@ -195,7 +196,7 @@ class WSClient: if channel in [STDOUT_CHANNEL, STDERR_CHANNEL]: # keeping all messages in the order they received # for non-blocking call. - self._all += data + self._all.write(data) if channel not in self._channels: self._channels[channel] = data else: