Reputation: 33
If I am writing to a socket file descriptor using write() bytes by bytes,
For example:
write(fd, 'a', 1);
write(fd, 'b', 1);
write(fd, 'c', 1);
Will this be less efficient than say
write (fd, 'abc', 3);
Upvotes: 2
Views: 1020
Reputation: 687
It really depends on the implementation of the TCP/IP stack. It would really depend on the segmentation that is implemented in the OS. Most OSes have a lot of optimization already built in.
If you're looking at a worst case situation, a TCP header is 20 bytes, an IP header is 20 bytes and the size of the frame header (depending on the protocol you're using, probably ethernet), so you could expect that plus your payload. That being said, the majority of the traffic in the internet is dominated by ACKs however, your network stack should combine the payloads.
Upvotes: 0
Reputation: 37930
Adding to John's answer, you can disable Nagle's Algorithm (via TCP_NODELAY
) and then the first version will become slower.
And for the reverse, you can call writev()
instead of write()
, which will cause the first version to perform exactly as the second.
Upvotes: 1
Reputation: 249133
No, not every byte will become a packet. Some may be coalesced due to Nagle's Algorithm and other things. There will be one TCP header per packet, not per byte.
That said, you should avoid calling write/send byte by byte because each one is a system call, which is expensive (on the local machine, not in terms of how it ends up on the network).
Upvotes: 3