Are recv () calls repeated expensively?

I have a question about a situation that I encounter quite often. From time to time I have to implement various TCP based protocols. Most of them define variable-length data packets that start with a common header ([packet identifier, length, payload] or something really similar). Obviously, there can be two approaches to reading these packages:

  • Reading the header (since the length of the header is usually fixed), extracting the length of the payload, reading the payload
  • Read all available data and save it in the buffer; parse buffer after

Obviously, the first approach is simple, but requires two calls read()(or perhaps more). The second one is a bit more complicated, but requires fewer challenges.

The question is, does the first approach affect performance so badly to worry about it?

+5
source share
5 answers

The best way to get your answer is to measure. The program straceis suitable for measuring the time of a system call. Using this adds a lot of overhead in itself, but if you just compare the cost of one recvfor this purpose compared to the cost of two, it should be reasonably significant. Use the option -ttto get the time. Or you can use the option -cto get an overview of the time spent separately, with which syscall it was spent.

, , oprofile.

, , , fdopen stdio, . , / , , select/poll - .

+2

, , . , x86, RISC (, mips,...).

, , .

, , . , . , , memcpy, , , syscall .

+8

, , , , - recv(), .

, recv() , , , recv() - . , recv() , , . - . recv(), , , . recv() , .

, , . , recv(). , , 16- 2 . , , recv() , , .

, ( , , ), , recv(). , recv(), , . , , .

+2

, " " , (, ) , recv() - 2.

+1

, , read/recv . , recv() , . recv() , 4k, 4k-. header + data. , . mem- , , , .

-2

All Articles