Skip to content

Commit 2e8947f

Browse files
committed
streams: Another streams.write optimization
Try bytes/bytearray fastpath in _try_write before trying buffer protocol.
1 parent e1d32b2 commit 2e8947f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

uvloop/handles/stream.pyx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,20 @@ cdef class UVStream(UVBaseTransport):
184184
Py_buffer py_buf
185185

186186
fd = self._fileno()
187-
PyObject_GetBuffer(data, &py_buf, PyBUF_SIMPLE)
188-
written = system.send(fd, <char*>py_buf.buf, py_buf.len, 0)
189-
PyBuffer_Release(&py_buf)
187+
188+
if PyBytes_CheckExact(data):
189+
written = system.send(fd, PyBytes_AS_STRING(data),
190+
Py_SIZE(data), 0)
191+
192+
elif PyByteArray_CheckExact(data):
193+
written = system.send(fd, PyByteArray_AS_STRING(data),
194+
Py_SIZE(data), 0)
195+
196+
else:
197+
PyObject_GetBuffer(data, &py_buf, PyBUF_SIMPLE)
198+
written = system.send(fd, <char*>py_buf.buf, py_buf.len, 0)
199+
PyBuffer_Release(&py_buf)
200+
190201
if written < 0:
191202
if errno.errno not in (system.EINTR, system.EAGAIN,
192203
system.EWOULDBLOCK, system.EINPROGRESS,

0 commit comments

Comments
 (0)