Performance of writing large responses to socket #1013
Replies: 3 comments
-
I did some further investigation and found that this performance issue only occurs when a timeout is used. If no timeout is set, the underlying socket operates in blocking mode and the entire payload is written at once. When a timeout is used, I found a benchmarking script for running
There is some variation on the upload speed between testbench runs, and I don't know the reason for those. However, it is clear that the unnecessary copying of the buffer causes slowdown in httpcore when the socket accepts buffer contents only partially. I can upload the benchmark and create PR if necessary. |
Beta Was this translation helpful? Give feedback.
-
Is there anything I can do to help move this forward? This is a significant performance issue, especially when using UNIX sockets. |
Beta Was this translation helpful? Give feedback.
-
FYI, here's a workaround for httpx: Wrap the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm evaluating
httpx
as a potential replacement forrequests
, and I discovered a performance issue that appears to originate inhttpcore
.I created a simple benchmark to compare upload performance between
requests
andhttpx
:(Ignore the irregular peaks — they’re artifacts of my test harness.)
It turns out
urllib3
(used byrequests
) sends large binary payloads withsendall()
, whilehttpcore
usessend()
in a loop. That in itself is not an issue, buthttpcore
appears to be doing an unnecessary copy on each iteration of the loop, as seen here: https://github.com/encode/httpcore/blob/master/httpcore/_backends/sync.py#L136.I made the following change to avoid repeated slicing of the
bytes
object:With this patch, upload performance for
httpx
becomes comparable to requests. The problem was especially noticeable when using Unix domain sockets, which have smaller default buffer sizes.I’m not sure if this change is universally safe, but all test cases pass locally with the patch applied.
Beta Was this translation helpful? Give feedback.
All reactions