You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does http.client.HTTPResponse._safe_read use MAXAMOUNT #80231
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-07-29.15:57:11.886>created_at=<Date2019-02-20.12:55:43.858>labels= ['3.8', 'library', 'performance']
title='Why does http.client.HTTPResponse._safe_read use MAXAMOUNT'updated_at=<Date2021-07-29.16:22:53.071>user='https://github.com/bmerry'
While investigating poor HTTP read performance I discovered that reading all the data from a response with a content-length goes via _safe_read, which in turn reads in chunks of at most MAXAMOUNT (1MB) before stitching them together with b"".join. This can really hurt performance for responses larger than MAXAMOUNT, because
(a) the data has to be copied an additional time; and
(b) the join operation doesn't drop the GIL, so this limits multi-threaded scaling.
I'm struggling to see any advantage in doing this chunking - it's not saving memory either (in fact it is wasting it).
To give an idea of the performance impact, changing MAXAMOUNT to a very large value made a multithreaded test of mine go from 800MB/s to 2.5GB/s (which is limited by the network speed).
The 1 MiB limit was added for bpo-1296004; apparently some platforms were overallocating multiple buffers and running out of memory. I suspect the loop in "_safe_read" was inherited from Python 2, which has different kinds of file objects. The comments suggest it does partial reads.
But the Python 3 code calls "socket.makefile" with "buffering" mode enabled by default. I agree it should be safe to read the total length in one go.
_fileobject called socket.recv with remaining size.
Typically, socket can't return MBs at once. So it cause:
Large (at most amt, some MBs) string (bytes) are allocated. (malloc)
recv is called.
_PyString_Resize() (realloc) is called with smaller bytes (typically ~128KiB)
amt -= received
if amt == 0: exit; goto 1.
This might stress malloc and realloc in CRT. It caused fragmentation and MemoryError.
---
For now, almost everything is rewritten.
In case of _pyio, BufferedIOReader calls RawIOBase.read(). It copies from bytearray to bytes. So call only malloc and free. Stress for realloc will be reduced.
Inadasan is right, this is still fixed in 3.10 and 3.11. The 3.9 revert is due to 3.9 supporting OpenSSL older than 1.1.1. 3.10+ requires OpenSSL 1.1.1+ per PEP-644.
Will you accept patches to fix this for 3.9? I'm not clear whether the "bug fixes only" status of 3.9 allows for fixing performance regressions.
bmerry commented on Jul 29, 2021
bmerrymannequin
MannequinAuthor
More actions
Will you accept patches to fix this for 3.9? I'm not clear whether the "bug fixes only" status of 3.9 allows for fixing performance regressions.
Never mind, I see your already answered this on bpo-42853 (as a no). Thanks for taking the time to answer my questions; I'll just have to skip Python 3.9 for this particular application and go straight to 3.10.
Activity
bmerry commentedon Feb 20, 2019
While investigating poor HTTP read performance I discovered that reading all the data from a response with a content-length goes via _safe_read, which in turn reads in chunks of at most MAXAMOUNT (1MB) before stitching them together with b"".join. This can really hurt performance for responses larger than MAXAMOUNT, because
(a) the data has to be copied an additional time; and
(b) the join operation doesn't drop the GIL, so this limits multi-threaded scaling.
I'm struggling to see any advantage in doing this chunking - it's not saving memory either (in fact it is wasting it).
To give an idea of the performance impact, changing MAXAMOUNT to a very large value made a multithreaded test of mine go from 800MB/s to 2.5GB/s (which is limited by the network speed).
vadmium commentedon Feb 25, 2019
The 1 MiB limit was added for bpo-1296004; apparently some platforms were overallocating multiple buffers and running out of memory. I suspect the loop in "_safe_read" was inherited from Python 2, which has different kinds of file objects. The comments suggest it does partial reads.
But the Python 3 code calls "socket.makefile" with "buffering" mode enabled by default. I agree it should be safe to read the total length in one go.
methane commentedon Apr 5, 2019
bpo-1296004 is too old (512MB RAM machine!) and I can not confirm it.
But I think it was caused by inefficient realloc() in CRT.
See
cpython/Lib/socket.py
Lines 298 to 303 in 6c52d76
_fileobject called socket.recv with remaining size.
Typically, socket can't return MBs at once. So it cause:
amt, some MBs) string (bytes) are allocated. (malloc)This might stress malloc and realloc in CRT. It caused fragmentation and MemoryError.
---
For now, almost everything is rewritten.
In case of _pyio, BufferedIOReader calls RawIOBase.read(). It copies from bytearray to bytes. So call only malloc and free. Stress for realloc will be reduced.
cpython/Lib/_pyio.py
Lines 586 to 591 in 50866e9
In case of C _io module, it is more efficient. It allocate PyBytes once and calls SocketIO.read_into directly. No temporary bytes objects are created.
cpython/Modules/_io/bufferedio.c
Line 1632 in 50866e9
cpython/Modules/_io/bufferedio.c
Line 1658 in 50866e9
cpython/Modules/_io/bufferedio.c
Line 1470 in 50866e9
methane commentedon Apr 5, 2019
Additionally, _safe_read calls fp.read() multiple times to handle EINTR.
But EINTR is handled by socket module now (PEP-475).
Now the function can be very simple.
methane commentedon Apr 6, 2019
New changeset d6bf6f2 by Inada Naoki in branch 'master':
bpo-36050: optimize HTTPResponse.read() (GH-12698)
d6bf6f2
ambv commentedon Jul 28, 2021
New changeset 153365d by Inada Naoki in branch '3.9':
[3.9] bpo-42853: Fix http.client fails to download >2GiB data over TLS (GH-27405)
153365d
bmerry commentedon Jul 28, 2021
Re-opening because the patch to fix this has just been reverted due to bpo-42853.
methane commentedon Jul 28, 2021
Note that it was reverted only in 3.9 branch.
ambv commentedon Jul 29, 2021
Inadasan is right, this is still fixed in 3.10 and 3.11. The 3.9 revert is due to 3.9 supporting OpenSSL older than 1.1.1. 3.10+ requires OpenSSL 1.1.1+ per PEP-644.
There is nothing to do here.
bmerry commentedon Jul 29, 2021
Will you accept patches to fix this for 3.9? I'm not clear whether the "bug fixes only" status of 3.9 allows for fixing performance regressions.
bmerry commentedon Jul 29, 2021
Never mind, I see your already answered this on bpo-42853 (as a no). Thanks for taking the time to answer my questions; I'll just have to skip Python 3.9 for this particular application and go straight to 3.10.
serhiy-storchaka commentedon Jan 14, 2026
Chunking is needed for other reason. See #119451.