-
Notifications
You must be signed in to change notification settings - Fork 597
Improve performance and reduce latency of Transport.write by attempting to send data immediately if all write buffers are empty #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tarasko
wants to merge
14
commits into
MagicStack:master
Choose a base branch
from
tarasko:optimize_write_latency
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−14
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f4d505c
Attempt to send data immediately if all write buffers are empty
20aa1bb
Fix stale tests
fde18ef
Don't test get_write_buffer_size because it is difficult to predict a…
e360c3a
Rename written to bytes_written
b79a840
Merge branch 'master' into optimize_write_latency
tarasko cfa33ab
Fix comment
96dc94c
Merge remote-tracking branch 'origin/master' into optimize_write_latency
94cbc7f
Merge branch 'master' into optimize_write_latency
tarasko 4f66353
Skip fast path in _initiate_write() if we attempted it in write(). Th…
4e96818
Revert unnecessary check
f6f5094
Revert unnecessary change
ede3f38
Revert unnecessary change
b0ea7f6
Merge remote-tracking branch 'origin/master' into optimize_write_latency
fantix 3dd6c51
Merge branch 'master' into optimize_write_latency
fantix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,11 +421,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 | ||
|
|
@@ -681,8 +683,47 @@ cdef class UVStream(UVBaseTransport): | |
| if self._conn_lost: | ||
| self._conn_lost += 1 | ||
| return | ||
|
|
||
| cdef ssize_t bytes_written | ||
|
|
||
| if self._get_write_buffer_size() == 0: | ||
| bytes_written_ = self._try_write(buf) | ||
|
|
||
| if bytes_written_ is None: | ||
| # A `self._fatal_error` was called. | ||
| # It might not raise an exception under some | ||
| # conditions. | ||
| if not self._closing: | ||
| raise RuntimeError('stream is open after ' | ||
| 'UVStream._try_write returned None') | ||
|
|
||
| return | ||
|
|
||
| bytes_written = bytes_written_ | ||
|
|
||
| if bytes_written == 0: | ||
| # All data was successfully written. | ||
| # on_write will call "maybe_resume_protocol". | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually call |
||
| return | ||
|
|
||
| if bytes_written > 0: | ||
| if UVLOOP_DEBUG: | ||
| if bytes_written == len(buf): | ||
| raise RuntimeError('_try_write sent all data and ' | ||
| 'returned non-zero') | ||
|
|
||
| if PyBytes_CheckExact(buf): | ||
| # Cast bytes to memoryview to avoid copying | ||
| # data that wasn't sent. | ||
| buf = memoryview(buf) | ||
| buf = buf[bytes_written_:] | ||
|
|
||
| # At this point it's either data was sent partially, | ||
| # or an EAGAIN has happened. | ||
| # 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() | ||
|
|
@@ -694,7 +735,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() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also check
self._protocol_pausedandself._closedhere?