Mazzoli: How fast are Linux pipes anyway?
Mazzoli: How fast are Linux pipes anyway?
Posted Jun 4, 2022 14:19 UTC (Sat) by atnot (guest, #124910)In reply to: Mazzoli: How fast are Linux pipes anyway? by martin.langhoff
Parent article: Mazzoli: How fast are Linux pipes anyway?
write() takes the address of a buffer that the user owns. Because it can not make any assumptions about how long that data is going to remain valid after the syscall, it has no choice but to copy. Because the memory was allocated ahead of time by the user, it can't really make any smart decisions about page size either. Then on read, it has to do the same thing again.
I guess for pipes specifically, one could imagine a flag which would make write() block until there is a corresponding read() call on the other side of the pipe, which would eliminate one copy. But with differing buffer sizes, non-blocking IO and other considerations I presume that'd be a lot of complexity that's unlikely to be worth it, especially considering the fact that you apparently can't even compute fizzbuzz fast enough to completely saturate a pipe like this. [1]
In that sense I'd say that the premise is wrong and write() is actually already plenty fast enough by default.
[1] In scenarios where this does matter there are usually already plenty of better, specialized solutions like mmap, XDP, dpdk, netfilter, etc.
