diff --git a/tornado/iostream.py b/tornado/iostream.py index 62f31fdc..9e95a066 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -189,12 +189,16 @@ class IOStream(object): """ assert isinstance(data, bytes_type) self._check_closed() + # We use bool(_write_buffer) as a proxy for write_buffer_size>0, + # so never put empty strings in the buffer. if data: - # We use bool(_write_buffer) as a proxy for write_buffer_size>0, - # so never put empty strings in the buffer. - if len(data) > 128*1024: - for i in range(0, len(data), 128*1024): - self._write_buffer.append(data[i:i+128*1024]) + # Break up large contiguous strings before inserting them in the + # write buffer, so we don't have to recopy the entire thing + # as we slice off pieces to send to the socket. + WRITE_BUFFER_CHUNK_SIZE = 128 * 1024 + if len(data) > WRITE_BUFFER_CHUNK_SIZE: + for i in range(0, len(data), WRITE_BUFFER_CHUNK_SIZE): + self._write_buffer.append(data[i:i+WRITE_BUFFER_CHUNK_SIZE]) else: self._write_buffer.append(data) self._write_callback = stack_context.wrap(callback)