Commit fb319150 authored by Jason Madden's avatar Jason Madden

Use bytearray += instead of bytearray.extend

This compiles to fewer bytecodes and uses built-in slots instead of
method dispatch, so it should be faster.

Benchmarks show a tiny improvement (using the same benchmarks as
issue #1233, so not exactly designed to test this):

RPS

+------+------+-------+
|Size  |master|+=     |
+------+------+-------+
|1     |5.60  | 5.63  |
+------+------+-------+
| 10   |5.49  |  5.53 |
+------+------+-------+
|  100 |4.99  |   5.00|
+------+------+-------+
|  1000|3.53  |   3.57|
+------+------+-------+
parent 45a16fa0
...@@ -733,9 +733,9 @@ class WSGIHandler(object): ...@@ -733,9 +733,9 @@ class WSGIHandler(object):
towrite = _bytearray(header_str) towrite = _bytearray(header_str)
# data # data
towrite.extend(data) towrite += data
# trailer # trailer
towrite.extend(b'\r\n') towrite += b'\r\n'
self._sendall(towrite) self._sendall(towrite)
else: else:
self._sendall(data) self._sendall(data)
...@@ -756,21 +756,20 @@ class WSGIHandler(object): ...@@ -756,21 +756,20 @@ class WSGIHandler(object):
self._write_with_headers(data) self._write_with_headers(data)
def _write_with_headers(self, data): def _write_with_headers(self, data):
towrite = bytearray()
self.headers_sent = True self.headers_sent = True
self.finalize_headers() self.finalize_headers()
# self.response_headers and self.status are already in latin-1, as encoded by self.start_response # self.response_headers and self.status are already in latin-1, as encoded by self.start_response
towrite.extend(b'HTTP/1.1 ') towrite = bytearray(b'HTTP/1.1 ')
towrite.extend(self.status) towrite += self.status
towrite.extend(b'\r\n') towrite += b'\r\n'
for header, value in self.response_headers: for header, value in self.response_headers:
towrite.extend(header) towrite += header
towrite.extend(b': ') towrite += b': '
towrite.extend(value) towrite += value
towrite.extend(b"\r\n") towrite += b"\r\n"
towrite.extend(b'\r\n') towrite += b'\r\n'
self._sendall(towrite) self._sendall(towrite)
# No need to copy the data into towrite; we may make an extra syscall # No need to copy the data into towrite; we may make an extra syscall
# but the copy time could be substantial too, and it reduces the chances # but the copy time could be substantial too, and it reduces the chances
...@@ -914,8 +913,8 @@ class WSGIHandler(object): ...@@ -914,8 +913,8 @@ class WSGIHandler(object):
# Trigger the flush of the headers. # Trigger the flush of the headers.
self.write(b'') self.write(b'')
if self.response_use_chunked: if self.response_use_chunked:
self.socket.sendall(b'0\r\n\r\n') self._sendall(b'0\r\n\r\n')
self.response_length += 5
def run_application(self): def run_application(self):
assert self.result is None assert self.result is None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment