In windows lets say i'm using the recv
function to receive data from a socket.
I'm curious how big would an optimal buffer be? I could make it 1024
bytes or I could make it 51200
bytes, or bigger. I'm wondering which one would be better for performance.
This doesn't only apply to the recv
function, lets say im reading a large text file, do i want a very large buffer, or a smaller buffer?
THe operating system performs its own buffering, so the size of your buffer does not really matter. the performance penalty lies in the function call: a 1 byte buffer will be inefficient because it will require too much calls to recv()
. a buffer too big is just a waste of space.
an optimal size will be something like twice the size of the data you expect to receive or are able to process in a single recv()
call, with a lower limit of approximately 1 or 2 tcp frame.
i personally use a 4KB buffer, but that's my own preference, and it depends largely on the application i am writing.
The operating system buffers it already, so you might as well just read one byte at the time.
This would depend on the kind of data you are expecting and the protocol you expecting.
UDP for example would give you a whole packet in one go so an optimal buffer size could be 1500.
The real performance impediment would be the function call (recv
in this case) that you will make. To improve performance, you could start with a default large value and then analyze the packet sizes being received. Based on the analysis, you could pass an (almost) "ideal" buffer size.
If you have a server-type situation where you don't know what services are to be run on it, you could use an array of buffer pools of varying sizes, eg [128,1024,4096,16384,65536]. When something connects, use a 128 size and, if all 128 come in, use 1024 next time.. and so on.
On clients or servers with a known protocol/loading, just sorta guess at it, (much as suggested by the other posters :)
Rgds,
Martin