Permalink
Browse files

Extract constant, add comments

  • Loading branch information...
bdarnell committed May 26, 2012
1 parent 227becd commit 3dcf497f5ac921fc68e58bb8b38329531fb2f391
Showing with 9 additions and 5 deletions.
  1. +9 −5 tornado/iostream.py
View
@@ -189,12 +189,16 @@ def write(self, data, callback=None):
"""
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)

0 comments on commit 3dcf497

Please sign in to comment.