Zero-copy is a technique that reduces data copying between user space and kernel space. In Linux, zero-copy is implemented via the sendfile(2) system call.
#include <sys/sendfile.h>
ssize_t sendfile(int out_fd, int in_fd, off_t *_Nullable offset,
size_t count);
sendfile
transfers data between two file descriptors without copying it to and from user space (i.e., without using read
and write
). This makes it efficient for serving static resources, proxying network traffic, and similar use cases.
Starting with Go 1.22, copies from a File to a net.UnixConn use the sendfile system call. The implementation can be found in src/os/zero_copy_linux.go
. Generally, developers should use io.Copy
, which attempts to use zero-copy system calls internally.
- Efficient data transfer through zero copy introduces zero-copy in Java.
- golang/go#58808 is the proposal for general zero-copy support from os.File and TCPConn to UnixConn in Go.
- copy_file_range(2) and splice(2) are additional Linux syscalls used for zero-copy.