Skip to content

Commit

Permalink
Skip fast path in _initiate_write() if we attempted it in write(). Th…
Browse files Browse the repository at this point in the history
…is removes unnecessary syscall
  • Loading branch information
taras committed Sep 10, 2024
1 parent 94cbc7f commit 4f66353
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion uvloop/handles/stream.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cdef class UVStream(UVBaseTransport):
# and then call _initiate_write() to start writing either immediately or in
# the next iteration (loop._queue_write()).
cdef inline _buffer_write(self, object data)
cdef inline _initiate_write(self)
cdef inline _initiate_write(self, bint skip_fast_path)

# _exec_write() is the method that does the actual send, and _try_write()
# is a fast-path used in _exec_write() to send a single chunk.
Expand Down
18 changes: 9 additions & 9 deletions uvloop/handles/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,13 @@ cdef class UVStream(UVBaseTransport):
self._buffer_size += dlen
self._buffer.append(data)

cdef inline _initiate_write(self):
if (not self._protocol_paused and
(<uv.uv_stream_t*>self._handle).write_queue_size == 0 and
self._buffer_size > self._high_water):
cdef inline _initiate_write(self, bint skip_fast_path):
if (not skip_fast_path and
not self._protocol_paused and
(<uv.uv_stream_t*>self._handle).write_queue_size == 0 and
self._buffer_size > self._high_water):
# Fast-path. If:
# - the caller hasn't tried fast path itself
# - the protocol isn't yet paused,
# - there is no data in libuv buffers for this stream,
# - the protocol will be paused if we continue to buffer data
Expand Down Expand Up @@ -687,9 +689,7 @@ cdef class UVStream(UVBaseTransport):

cdef ssize_t bytes_written

if (not self._protocol_paused and self._buffer_size == 0 and
(<uv.uv_stream_t*>self._handle).write_queue_size == 0):

if self._get_write_buffer_size() == 0:
bytes_written_ = self._try_write(buf)

if bytes_written_ is None:
Expand Down Expand Up @@ -726,7 +726,7 @@ cdef class UVStream(UVBaseTransport):
# buffer remaining data and send it later

self._buffer_write(buf)
self._initiate_write()
self._initiate_write(True) # skip fast path in _initiate_write

def writelines(self, bufs):
self._ensure_alive()
Expand All @@ -738,7 +738,7 @@ cdef class UVStream(UVBaseTransport):
return
for buf in bufs:
self._buffer_write(buf)
self._initiate_write()
self._initiate_write(False) # try fast path in _initiate_write

def write_eof(self):
self._ensure_alive()
Expand Down

0 comments on commit 4f66353

Please sign in to comment.