Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7d6d2a

Browse files
committed
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.
1 parent b4062e8 commit b7d6d2a

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lib/multiprocessing/connection.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,23 @@ def _recv(self, size, read=_read):
395395
return buf
396396

397397
def _send_bytes(self, buf):
398-
# For wire compatibility with 3.2 and lower
399398
n = len(buf)
400-
self._send(struct.pack("!i", n))
401-
# The condition is necessary to avoid "broken pipe" errors
402-
# when sending a 0-length buffer if the other end closed the pipe.
403-
if n > 0:
404-
self._send(buf)
399+
# For wire compatibility with 3.2 and lower
400+
header = struct.pack("!i", n)
401+
if n > 16384:
402+
# The payload is large so Nagle's algorithm won't be triggered
403+
# and we'd better avoid the cost of concatenation.
404+
chunks = [header, buf]
405+
elif n > 0:
406+
# Issue # 20540: concatenate before sending, to avoid delays due
407+
# to Nagle's algorithm on a TCP socket.
408+
chunks = [header + buf]
409+
else:
410+
# This code path is necessary to avoid "broken pipe" errors
411+
# when sending a 0-length buffer if the other end closed the pipe.
412+
chunks = [header]
413+
for chunk in chunks:
414+
self._send(chunk)
405415

406416
def _recv_bytes(self, maxsize=None):
407417
buf = self._recv(4)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Core and Builtins
4848
Library
4949
-------
5050

51+
- Issue #20540: Fix a performance regression (vs. Python 3.2) when layering
52+
a multiprocessing Connection over a TCP socket. For small payloads, Nagle's
53+
algorithm would introduce idle delays before the entire transmission of a
54+
message.
55+
5156
- Issue #16983: the new email header parsing code will now decode encoded words
5257
that are (incorrectly) surrounded by quotes, and register a defect.
5358

0 commit comments

Comments
 (0)