[Python-checkins] cpython (merge 3.3 -> default): Issue #20540: Fix a performance regression (vs. Python 3.2) when layering a

antoine.pitrou python-checkins at python.org
Sat Feb 8 23:09:19 CET 2014


http://hg.python.org/cpython/rev/125c24f47f3c
changeset:   89062:125c24f47f3c
parent:      89060:68c40567e8f8
parent:      89061:4816ab0477d2
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Feb 08 23:05:52 2014 +0100
summary:
  Issue #20540: Fix a performance regression (vs. Python 3.2) when layering a multiprocessing Connection over a TCP socket.
For small payloads, Nagle's algorithm would introduce idle delays before the entire transmission of a message.

files:
  Lib/multiprocessing/connection.py |  22 +++++++++++++-----
  Misc/NEWS                         |   5 ++++
  2 files changed, 21 insertions(+), 6 deletions(-)


diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -395,13 +395,23 @@
         return buf
 
     def _send_bytes(self, buf):
+        n = len(buf)
         # For wire compatibility with 3.2 and lower
-        n = len(buf)
-        self._send(struct.pack("!i", n))
-        # The condition is necessary to avoid "broken pipe" errors
-        # when sending a 0-length buffer if the other end closed the pipe.
-        if n > 0:
-            self._send(buf)
+        header = struct.pack("!i", n)
+        if n > 16384:
+            # The payload is large so Nagle's algorithm won't be triggered
+            # and we'd better avoid the cost of concatenation.
+            chunks = [header, buf]
+        elif n > 0:
+            # Issue # 20540: concatenate before sending, to avoid delays due
+            # to Nagle's algorithm on a TCP socket.
+            chunks = [header + buf]
+        else:
+            # This code path is necessary to avoid "broken pipe" errors
+            # when sending a 0-length buffer if the other end closed the pipe.
+            chunks = [header]
+        for chunk in chunks:
+            self._send(chunk)
 
     def _recv_bytes(self, maxsize=None):
         buf = self._recv(4)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,11 @@
 Library
 -------
 
+- Issue #20540: Fix a performance regression (vs. Python 3.2) when layering
+  a multiprocessing Connection over a TCP socket.  For small payloads, Nagle's
+  algorithm would introduce idle delays before the entire transmission of a
+  message.
+
 - Issue #16983: the new email header parsing code will now decode encoded words
   that are (incorrectly) surrounded by quotes, and register a defect.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list