uvloop icon indicating copy to clipboard operation
uvloop copied to clipboard

Improve performance and reduce latency of Transport.write by attempting to send data immediately if all write buffers are empty

Open tarasko opened this issue 1 year ago • 9 comments

Current implementation of UVStream.write almost never sends data immediately. Instead the data is stored in the buffer and picked up later by uv_check callback. This introduces unnecessary latency and CPU overhead. Despite being a change only in UVStream it also directly benefit _SSLProtocolTransport.write latency since it uses UVStream.write to send ssl frames.

This PR increases RPS rate between echoclient and echoserver by roughly 10%. echoclient --worker 1 --num 200000 echoserver --uvloop --proto

Because of this change a couple of test had to be tweaked.

  • get_write_buffer_size cannot reliably return how much data has been already buffered. It doesn't take into account the socket send buffer (which is in the kernel)
  • writing above limits does not necessarily cause pause_writing because again the data first goes to the socket send buffer

Please note that the current implementation of asyncio does the same thing. It tries to write directly to the socket, and, only if EWOULDBLOCK happens, the data is added to the buffer https://github.com/python/cpython/blob/c13e7d98fb8581014a225b900b1b88ccbfc28097/Lib/asyncio/selector_events.py#L1065

tarasko avatar Aug 16 '24 23:08 tarasko

This also increases aiohttp websocket echo performance by 20-25%

tarasko avatar Aug 20 '24 13:08 tarasko

Hi @fantix, I saw you recently commited something to uvloop. Do you know if someone could take a look at this PR and maybe give a feedback?

tarasko avatar Aug 29 '24 18:08 tarasko

Yeah I'll go through all issues/PRs again and include some in the final 0.21 release, it's just all taking some time unfortunately, thanks for the PR and your kind patience!

fantix avatar Sep 03 '24 15:09 fantix

Yeah I'll go through all issues/PRs again and include some in the final 0.21 release, it's just all taking some time unfortunately, thanks for the PR and your kind patience!

Hi @fantix, sorry to bother, I know it is open source, voluntary work and you're very busy. But would you have some time to look at this (and other my PRs) any time soon?

Also, 0.21.0 is already released :)

tarasko avatar Oct 16 '24 07:10 tarasko

Can someone from the contributors have a look at this PR? It would be nice to improve the performances!

Doch88 avatar Dec 03 '24 12:12 Doch88

@MagicStack guys please finish this CR.

nevmerzhitsky avatar Mar 07 '25 15:03 nevmerzhitsky

Updates on this? @fantix

Doch88 avatar Jul 11 '25 14:07 Doch88

@fantix are you planning to review and release this soon? 🙏

pedroconceicao avatar Aug 29 '25 08:08 pedroconceicao

@1st1 Is there any news about this PR ?

Elyahou avatar Nov 12 '25 08:11 Elyahou