Uh oh!
There was an error while loading. Please reload this page.
Fix #338 os.writev() instead of os.write(b.join()) and sendmsg() instead of send() - #339
Fix #338 os.writev() instead of os.write(b.join()) and sendmsg() instead of send()#339socketpair wants to merge 4 commits into
Conversation
gvanrossum
commented
May 2, 2016
Please don't submit a PR that fails the tests and has typos in it. At the very least run it against your test program with and without drain(). |
@gvanrossum I have fix typo, and checked spped: Before my patch: After my patch: P.S. Thinking how to fix tests. Please point me.... |
gvanrossum
commented
May 2, 2016
I guess you have to mock os.writev too. Time to learn something! |
gvanrossum
commented
May 2, 2016
BTW great news on the speed! |
@gvanrossum |
| # @mock.patch('os.writev') stores internal reference to _buffer, which is MODIFIED | ||
| # during this call. So, makeing deep copy :( | ||
| # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| n = os.writev(self._fileno, self._buffer.copy()) |
There was a problem hiding this comment.
There must be a better way here. Adding a copy just for convenient testing isn't a good idea. Please also remove all exclamation marks, smiles, and fix typos.
There was a problem hiding this comment.
@1st1
I consider you don't ever think to merge PR with such hack, so I added these comments to bring your attention. PR is not ready for merge. But I must point to this bad place.
If you know 'better way' please point me. I will fix immediatelly.
There was a problem hiding this comment.
OK ;) Please try to find a saner approach. I'll myself try to take a look later.
There was a problem hiding this comment.
Has removed exclamation marks and other trash. But I stil don't know how to overcome that annoying bug in unittest.mock
gvanrossum
commented
May 2, 2016
I don't follow. Does the real |
socketpair
commented
May 2, 2016
@gvanrossum, in short code is: Note, that |
| self._buffer.append(data) # Try again later. | ||
| self._loop.remove_writer(self._fileno) | ||
| self._maybe_resume_protocol() # May append to buffer. |
There was a problem hiding this comment.
Seems another bug. in sockets, self._maybe_resume_protocol() is called before if self._buffer. So, who is right ?
There was a problem hiding this comment.
I would trust the socket code more. So I'd remove line 544-545 above and see how it fares.
There was a problem hiding this comment.
Fixed and make behaviour as in sockets.
1st1
commented
May 2, 2016
@socketpair Here's my attempt to tame the mock: 1st1@5b8e2dd |
socketpair
commented
May 2, 2016
@1st1 I think the most convenient way is to merge my PR in current state, and after that, merge you PR. Is it suitable workflow ? |
1st1
commented
May 2, 2016
@socketpair You can just incorporate my change into your PR. Please add a comment to |
Sorry, please don't merge PR. I have checked performance. Using just expanding bytearray is much faster. C is not Python. :( This is reproduced both for sockets and for pipes. In theory, writev() / sendmsg() should be faster. In reality - is not. I can not believe that this is true. I will investigate why this happen. Please don't merge PR until problem resolved. Program used to benchmark: |
This is very strange. Using |
gvanrossum
commented
May 2, 2016
via email
Honestly I'm not that surprised that bytearray performs better. Resizing a
byte array is one of the fastest things you can do in CPython (because
`realloc()` handles the copy for you). |
1st1
commented
May 13, 2016
Hi @socketpair! Any progress on this PR? |
socketpair
commented
May 13, 2016
I'm online, will continue soon. I spent last week in another country, now I returned back to home, and today is a first working day at my job. |
1st1
commented
May 13, 2016
Sure, no rush. 3.5.2 will happen relatively soon, would be nice to have this reviewed and merged before that. |
socketpair
commented
May 13, 2016
great news! Now I know why bytearray was faster. Now, my code is even faster than bytearray variant! since it true zerocopy! WOOHOO! |
socketpair
commented
May 13, 2016
|
| n -= len(chunk) | ||
| if n < 0: | ||
| # only part of chunk was written, so push unread part of it back to _buffer | ||
| # memoryview is required to eliminate memory copying while slicing. |
There was a problem hiding this comment.
This was a cause of slow throughput
1st1
commented
May 13, 2016
Cool! Could you please fix the code style to 79 cars per line? |
| def get_write_buffer_size(self): | ||
| return len(self._buffer) | ||
| return sum(len(data) for data in self._buffer) |
There was a problem hiding this comment.
Maybe instead of running sum each time, we can have an internal _buffer_length attribute?
There was a problem hiding this comment.
maybe, but I copy-pasted it from another place. Yes I can fix that (and perform benchmarking).
There was a problem hiding this comment.
I suspect most of the time the data queue won't be large, so you really need to overwhelm the write buffer in your benchmarks to see the benefits of caching.
There was a problem hiding this comment.
Sorry, I will not fix that. Code become too complex and error prone. Especially when exception occurs in some specific places. I tried.
There was a problem hiding this comment.
So, I have fixed anyway. I also added many asserts.
1st1
commented
May 13, 2016
I've left some feedback. |
socketpair
commented
May 13, 2016
Also, Python3.5 does not support |
| return | ||
| try: | ||
| n = self._sock.send(self._buffer) | ||
| if compat.HAS_SENDMSG: |
1st1
commented
Jul 5, 2016
Sorry, Mark, I'm busy with other things. This is quite an intrusive change and it has to be merged with extra care. And we have plenty of time before 3.6. |
socketpair
commented
Jul 6, 2016
@1st1 I can split PR to small parts. After each part, everything will be consistent. Should I do that ? |
1st1
commented
Jul 6, 2016
How do you want to split it? Actually, I was just looking at this PR again, and I think it's ready. I need to spend another hour playing with the code, but I'd say it's almost there. |
methane
commented
Jul 22, 2016
I think |
@arthurdarcet can you benchmark asyncio streams with this patch applied, using included tool (in this PR) on Mac OS X ? This patch should speedup pipes significantly on that platform too. |
arthurdarcet
commented
Jul 22, 2016
@socketpair sure, do you have some code i could run? |
methane
commented
Jul 22, 2016
@socketpair I did it. |
I have an idea: add to write functions parameters |
methane
commented
Jul 25, 2016
I feel it's a premature optimization. |
methane
commented
Jul 25, 2016
I've benchmarked on Ubuntu 16.04, with slightly modified master branch: socketpair:writev branch: |
cons:
cons:
@gvanrossum@1st1@methane |
methane
commented
Jul 25, 2016
Another cons of Checking "bytes-like" object without copy is very hard in pure Python. mv=memoryview(data)
ifmv.itemsize!=1ornotmv.contiguous:
raiseTypeError
|
methane
commented
Jul 25, 2016
My preference is, use bytearray() in pure Python. But make it replacable. Someone can implement more smart buffer like below in C. def__init__(self):
self.queue=deque()
def__iadd__(self, data):
iftype(data) isbytesandlen(data) >200:
self.queue.append(data)
returnifnotself.queueortype(self.queue[-1]) isbytes:
self.queue.append(bytearray())
self.queue[-1] +=data |
socketpair
commented
Aug 1, 2016
Copy-pasting here: small data: large data: |
socketpair
commented
Aug 1, 2016
@gvanrossum@1st1 What do you think about all that ? |
I understand, that simplicity of code is important thing. In real use cases (i.e. small amount of data and small count of chunks) both implementations give the same performance. |
For pipes, use os.writev() instead of os.write(b.join()). For sockets, use sendmsg() instead of send(b''.join()). Also change plain list of buffers to deque() of buffers. This greatly improve performance on large buffers or on many stream.write() calls.
1st1
commented
Oct 5, 2016
Mark, I'm closing this PR. I think we'd want to have vectorized writes in 3.7, so when you have a new patch please open a new PR. |
No description provided.