2

In python docs https://docs.python.org/3/library/socket.html it says about socket.recv(bufsize[, flags]) the following:

Note

For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096.

Why exactly so? Wouldn't it be better to set it arbitrarily large (like 1000000000) so that we receive the most of what we can receive? I assume that the message would still be probably cut off after some KBs. But if we set buff_size to 4096, we risk the situation when we get e.g. 8192 Bytes, and have to perform one more iteration of reading from the stream, while we could get everything at once with buff_size=100000000

  • 1
    *"Wouldn't it be better to set it arbitrarily large (like 1000000000) so that we receive the most of what we can receive?"* - this would mean that this amount of memory would need to be allocated by the application first even if it is not needed at the end. – Steffen Ullrich Mar 13 '22 at 19:27
  • "Wouldn't it be better to set it arbitrarily large (like 1000000000)": no it wouldn't, because that much data will never be available at a time. – user207421 Mar 14 '22 at 00:22

0 Answers0