Hasty Briefsbeta

Bilingual

Zero-copy in Go: sendfile, splice, and the cost of io.Copy

4 hours ago
  • #sendfile
  • #zero-copy
  • #Go optimization
  • A middleware change slowed a file service by disabling sendfile(2) optimization in Go.
  • Go's io.Copy uses sendfile(2) for *os.File sources via *net.TCPConn.ReadFrom, avoiding user-space copies.
  • Wrapping *os.File in a custom io.Reader loses sendfile optimization, increasing syscalls and CPU usage.
  • io.LimitReader preserves the fast path as Go recognizes it, unlike opaque wrappers.
  • socket-to-socket proxies use splice(2) for zero-copy transfers between *net.TCPConn instances.
  • strace helps verify fast path usage with sendfile/splice counts versus read/write syscalls.
  • Avoid wrapping io.Reader/Writer between file/socket to maintain zero-copy optimizations in Go.