]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixes issue #19506: Use a memoryview to avoid a data copy when piping data
authorGregory P. Smith <greg@krypto.org>
Sun, 8 Dec 2013 03:12:46 +0000 (19:12 -0800)
committerGregory P. Smith <greg@krypto.org>
Sun, 8 Dec 2013 03:12:46 +0000 (19:12 -0800)
to stdin within subprocess.Popen.communicate.  5-10% less cpu usage.

Lib/subprocess.py
Misc/NEWS

index c3a278836c19121319363f1c896e528b48968202..546a7a0fcda89d6963999bc25bda43c856a4e145 100644 (file)
@@ -1621,6 +1621,9 @@ class Popen(object):
 
             self._save_input(input)
 
+            if self._input:
+                input_view = memoryview(self._input)
+
             while self._fd2file:
                 timeout = self._remaining_time(endtime)
                 if timeout is not None and timeout < 0:
@@ -1638,8 +1641,8 @@ class Popen(object):
 
                 for fd, mode in ready:
                     if mode & select.POLLOUT:
-                        chunk = self._input[self._input_offset :
-                                            self._input_offset + _PIPE_BUF]
+                        chunk = input_view[self._input_offset :
+                                           self._input_offset + _PIPE_BUF]
                         try:
                             self._input_offset += os.write(fd, chunk)
                         except OSError as e:
index 623e1fe6b97a2b4162ea99821dde301f0acd385b..706446072032d213aa8bd00eb74f6e24b50135a4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #19506: Use a memoryview to avoid a data copy when piping data
+  to stdin within subprocess.Popen.communicate.  5-10% less cpu usage.
+
 - Issue #19839: Fix regression in bz2 module's handling of non-bzip2 data at
   EOF, and analogous bug in lzma module.