From 715e6d04ab6a08cc883f6b9cefd38db89f7e4b81 Mon Sep 17 00:00:00 2001
From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl>
Date: Sun, 5 Feb 2017 06:10:31 +0100
Subject: [PATCH] optimization

short messages are a common case
---
 coolamqp/attaches/consumer.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/coolamqp/attaches/consumer.py b/coolamqp/attaches/consumer.py
index b17f5a4..d8e9df3 100644
--- a/coolamqp/attaches/consumer.py
+++ b/coolamqp/attaches/consumer.py
@@ -529,13 +529,17 @@ class MessageReceiver(object):
             # Does body need preprocessing?
             body = self.body
             if self.recv_mode == BodyReceiveMode.BYTES:
-                # since b''.join() with list comprehension and .tobytes() would create
-                # an extra copy of string
-                bio = io.BytesIO()
-                for mv in body:
-                    bio.write(mv)
+                if len(self.body) == 1:
+                    # common case :)
+                    body = self.body[0].tobytes()
+                else:
+                    # since b''.join() with list comprehension and .tobytes() would create
+                    # an extra copy of string
+                    bio = io.BytesIO()
+                    for mv in body:
+                        bio.write(mv)
 
-                body = bio.getvalue()
+                    body = bio.getvalue()
             # if MEMORYVIEW, then it's already ok
 
             rm = ReceivedMessage(
-- 
GitLab