Zero copy 能够减少 2 次拷贝和上下文切换。
Java 提供了 SocketChannel, FileChannel, ByteBuffer, MappedByteBuffer。灵活组合即可实现 zero-copy。目前 tomcat, jetty (服务端), OkHTTP, HTTPClient 实现都不是零拷贝, 只有 Netty 相关的客户端和服务端优雅实现了零拷贝。
https://medium.com/@xunnan.xu/its-all-about-buffers-zero-copy-mmap-and-java-nio-50f2a1bfc05c
hardware supports scatter-n-gather
MappedByteBuffer ← FileChannel.map()
kafka 和 RocketMQ 都是这么操作消息日志的
Mmap allows code to map file to kernel memory and access that directly as if it were in the application user space, thus avoiding the unnecessary copy. As a tradeoff, that will still involve 4 context switches. But since OS maps certain chunk of file into memory, you get all benefits from OS virtual memory management — hot content can be intelligently cached efficiently, and all data are page-aligned thus no buffer copying is needed to write stuff back.
However, nothing comes for free — while mmap does avoid that extra copy, it doesn’t guarantee the code will always be faster — depending on the OS implementation, there may be quite a bit of setup and teardown overhead (since it needs to find the space and maintain it in the TLB and make sure to flush it after unmapping) and page fault gets much more expensive since kernel now needs to read from hardware (like disk) to update the memory space and TLB. Hence, if performance is this critical, benchmark is always needed as abusing mmap() may yield worse performance than simply doing the copy.