From 246db5b13c5b3c21263b9db42ebefaeb963d5bb9 Mon Sep 17 00:00:00 2001 From: Andy McCurdy Date: Tue, 17 Jun 2014 09:08:35 -0700 Subject: [PATCH] better pack_commands algorithm with less string joining --- redis/connection.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/redis/connection.py b/redis/connection.py index f9efa7b4e8..141731d6ef 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -600,19 +600,23 @@ def pack_command(self, *args): def pack_commands(self, commands): "Pack multiple commands into the Redis protocol" + output = [] pieces = [] - buff = SYM_EMPTY + buffer_length = 0 for cmd in commands: packed = self.pack_command(*cmd)[0] - buff = SYM_EMPTY.join((buff, packed)) - if len(buff) > 6000: - pieces.append(buff) - buff = SYM_EMPTY - - if buff: - pieces.append(buff) - return pieces + pieces.append(packed) + buffer_length += len(packed) + + if buffer_length > 6000: + output.append(SYM_EMPTY.join(pieces)) + buffer_length = 0 + pieces = [] + + if pieces: + output.append(SYM_EMPTY.join(pieces)) + return output class SSLConnection(Connection):