To read data from a socket in python, you call socket.recv
, which has this signature:
socket.recv(bufsize[, flags])
The python docs for socket.recv vaguely state:
Note: For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096.
Question: What does "best match with hardware and network realities" mean? What is the actual impact of setting bufsize to a non-power-of-two?
I've seen many other recommendations to make this read a power of 2. I'm also well aware of reasons when it is often useful to have array lengths as powers of two (bitshift/masking operations on the length, optimal FFT array size, etc), but these are application dependent. I just am not seeing the general reason for it with socket.recv
. Certainly not to the point of the specific recommendation in the python documentation. I also don't see any power-of-two optimizations in the underlying python code to make it a python-specific recommendation
For example... if you have a protocol where the incoming packet length is exactly known, it is obviously preferrable to only read "at most" what is needed for the packet you are dealing with, otherwise you could potentially eat into the next packet and that would be irritating. If the packet I'm currently processing only has 42 bytes pending, I'm only going to set bufsize to 42.
What am I missing? When I have to choose an arbitrary buffer/array size I usually (always?) make the length a power of two, just in case. This is just a habit developed over many years. Are the python docs also just a victim of habit?
This isn't exclusive to python, but since I'm specifically referencing the python docs I'll tag it as such.
UPDATE: I just checked the size of the buffer at the kernel level on my system (or at least I think I did... I did cat /proc/sys/net/core/rmem_default
) and it was 124928. Not a power of two. rmem_max
was 131071, also clearly not a power of two.
In looking into this more I really cannot see any benefit in the power of two recommendation(s) yet. I'm about ready to call it as a bogus recommendation...
I also added tcp
and C
tags since they are also relevant.