# Zero Copy ## `sendfile` 一次`sendfile()`有三次内存拷贝: - 磁盘 -> kernel buffer - kernel buffer -> socket发送缓冲区 - socket发送缓冲区 -> NIC 先比`read()`和`write()`的方式, 少了一次内存拷贝, 两次上下文切换. 如果是`Scatter-Gatther NIC`, 那么os缓存区会直接拷贝到网卡, 少两次内存拷贝. ([`TCP Segmentation Offloading`](https://lwn.net/Articles/9123/)) ## `splice` 和`sendfile`的区别是: - 磁盘 -> kernel buffer - kernel buffer -- pipe --> socket buffer - socket buffer -> NIC 这里的`pipe`不做拷贝,只是建立一个引用(指针). > splice() moves data between two file descriptors without copying between kernel address space and user address space. http://man7.org/linux/man-pages/man2/splice.2.html ## 补充参考 - preformance review https://pdfs.semanticscholar.org/6a35/60046cb8d3258669c86072a7cab05e1d2300.pdf - https://www.linuxjournal.com/article/6345