3 ms·
How does MSG_MORE save syscalls, given that it's a flag you pass to syscalls? Do you mean "save packets"?
by nh2 6y ago
How does MSG_MORE save syscalls, given that it's a flag you pass to syscalls? Do you mean "save packets"?
- jlokier 6y agoSystem calls not packets. By using sendmsg() with MSG_MORE instead of write(), you can avoid the setsockopt() with TCP_CORK to cork before the write, and the later setsockopt() with TCP_NODELAY to push despite the cork. You can't give MSG_MORE to sendfile(), though with HTTP you don't need to. But if you need an equivalent of MSG_MORE with sendfile() you can in theory use SPLICE_F_MORE with splice() instead.
- nh2 6y agoAh, I see, you were referring to saving `setsockopt()`, not data-carrying syscalls. Yes that makes sense. But I guess that in most cases where you have control over the `sendmsg()`'s calls flags, you'd also have control over its buffer, so you may be able to build the buffer in userspace in many situations, thus even saving multiple data-carrying syscalls. The `setsockopt()` approach has the benefit that it works even when you have no control over the sending syscalls, e.g. when some library does it for you that you cannot modify or configure.
- jlokier 6y agoMSG_MORE comes in useful for these examples, where you can't use writev() alone, but do control the sending syscalls: - HTTP (unencrypted) serving static files or cache files, to combine sendmsg() for the headers followed by sendfile() for the body. You can't batch using writev() in that case, if you want the benefit of sendfile(). - Transmitting a stream of data that is being forwarded or generated. For example a HTTPS reverse proxy which forwards incoming unencrypted data and formats it into TLS progressively. It can't buffer the whole response as that would add too much delay, so it can send using sendmsg() with MSG_MORE until it reaches the end of the forwarded data.
- nh2 6y agoThat makes sense, thanks!