/performant-golang:go-perf-zerocopy-and-syscallsfrom performant-golang
Guides Go zero-copy I/O and syscall reduction: how io.Copy already lowers to sendfile/splice/copy_file_range on Linux via the ReaderFrom/WriterTo fast path on net.TCPConn and os.File, the concrete-type conditions that keep it (bufio wrapping defeats it), net.Buffers batching writes into one writev(2), and mmap caveats for read-mostly files. Fires on "zero copy in Go", "sendfile", "writev", "mmap a file", "reduce syscalls". Routes buffering to go-perf-buffered-io, unsafe conversion to go-perf-strings-bytes-zerocopy.
Ships in performant-golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Whether to reach this low is a methodology call —
go-perf-methodologyand the measure-first loop come first. Most services should usebufio+io.Copyand never touch a raw syscall. Reach here only when a CPU profile orstrace -cshows the cost is inread/write/copy at the OS boundary. The good news: the biggest win —sendfile— is somethingio.Copyalready does for you, if you don't defeat it.
All Go below builds clean under gofmt, go vet, and go test on Go 1.25 / 1.26.
io.Copy Is Already Zero-Copy — If You Keep the Fast Pathio.Copy(dst, src) is not a dumb userspace read/write loop. Its contract: "If src
implements WriterTo, the copy is implemented by calling src.WriteTo(dst). Otherwise, if
dst implements ReaderFrom, the copy is implemented by calling dst.ReadFrom(src)"
(io.Copy). And critically for allocation: in CopyBuffer,
"If either src implements WriterTo or dst implements ReaderFrom, buf will not be used to
perform the copy" (io.CopyBuffer) — the staging buffer is
skipped entirely.
*net.TCPConn and *os.File implement these interfaces with kernel zero-copy on Linux.
The dispatch is explicit in the standard library — (*TCPConn).readFrom tries three paths in
order (src/net/tcpsock_posix.go):
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
if n, err, handled := spliceFrom(c.fd, r); handled { // socket→socket: splice(2)
return n, err
}
if n, err, handled := sendFile(c.fd, r); handled { // file→socket: sendfile(2)
return n, err
}
return genericReadFrom(c, r) // fallback: userspace copy
}
So io.Copy(tcpConn, file) becomes a sendfile(2) syscall — data goes disk → socket inside
the kernel, never round-tripping through your process's memory:
// file → socket: this is sendfile(2). No read()/write() loop, no userspace buffer.
n, err := io.Copy(tcpConn, file) // tcpConn is *net.TCPConn, file is *os.File
sendFile "copies the contents of r to c using the sendfile system call to minimize copies"
and bottoms out in poll.SendFile → syscall.Sendfile (src/net/sendfile.go,
src/internal/poll/sendfile_unix.go).
The conditions that keep the fast path (this is where LLM code fails):
sendFile requires r.(syscall.Conn) to succeed
— an *os.File qualifies (sendfile.go).
Wrap the file in a bufio.Reader and that assertion fails → you fall back to
genericReadFrom, a userspace copy. A bufio.Writer around the conn likewise hides the
*TCPConn, so ReadFrom is never the TCP one.io.LimitedReader is fine — it's unwrapped. sendFile/spliceFrom peel off a
*io.LimitedReader and honor its N, so io.Copy(conn, io.LimitReader(file, n)) still uses
sendfile (sendfile.go).io.MultiReader, io.TeeReader, a custom wrapper, or a []byte source defeat it —
none implement syscall.Conn, so you drop to the generic loop.net/http already does this. http.ServeContent/ServeFile and io.Copy(w, file)
where w is the server's http.ResponseWriter use ReadFrom/sendfile.splice) and file→file (copy_file_range)The same ReaderFrom machinery covers two more kernel fast paths — you keep the concrete
types, you never name the syscall:
splice(2) (the proxy/tunnel hot path). io.Copy(dstConn, srcConn)
between two TCP (or stream Unix) connections goes through spliceFrom → poll.Splice, moving
data via a kernel pipe "to minimize copies of data from and to userspace … src and dst must
both be stream-oriented sockets"; it only engages for *TCPConn/stream *UnixConn sources
(src/internal/poll/splice_linux.go,
src/net/splice_linux.go).copy_file_range(2). io.Copy(dstFile, srcFile) with two *os.Files
goes through (*File).readFrom, which tries copyFileRange first, then spliceToFile
(src/os/zero_copy_linux.go).
copy_file_range is gated on kernel ≥ 5.3 (older kernels are buggy) and falls back
cleanly otherwise (src/internal/poll/copy_file_range_linux.go).(*os.File).WriteTo also lands on sendfile, but only when the
destination is a TCP/Unix stream socket (zero_copy_linux.go).O_APPEND destination files disable the zero-copy path — neither copy_file_range nor
splice supports O_APPEND dsts, so os skips them (zero_copy_linux.go).
// proxy: client conn → backend conn becomes splice(2) in each direction.
go func() { io.Copy(backend, client) }() // both *net.TCPConn
io.Copy(client, backend)
net.Buffers — Batch N Writes Into One writev(2)When you have several discrete byte slices to send (header + body + trailer, or framed
messages), N separate conn.Write calls are N write(2) syscalls. net.Buffers coalesces
them into one writev(2):
"Buffers contains zero or more runs of bytes to write. On certain machines, for certain types of connections, this is optimized into an OS-specific batch write operation (such as 'writev')." — net.Buffers
Buffers is type Buffers [][]byte; its WriteTo implements io.WriterTo
(net.Buffers). On a *net.TCPConn/*net.UnixConn it
reaches (*netFD).writeBuffers → (*poll.FD).Writev, which builds an iovec array and
issues writev — up to 1024 iovecs per syscall (src/net/writev_unix.go,
src/internal/poll/writev.go).
parts := net.Buffers{header, body, trailer}
_, err := parts.WriteTo(conn) // one writev(2), not three write(2) — conn is *net.TCPConn
Caveats from the source: WriteTo "modifies the slice v as well as v[i]" — it consumes
the Buffers as it drains, so don't reuse the value after a partial write without resetting it
(net.Buffers). The fast path only exists for unix stream
connections; elsewhere it degrades to sequential writes. For small slices, a single
bufio.Writer + Flush is simpler and just as few syscalls; net.Buffers wins when the
slices are large and already separate (avoiding the copy into a buffer). Buffer sizing and
Flush discipline → go-perf-buffered-io.
mmap — Random Access to Large Read-Mostly FilesFor a large file you index into randomly (a search index, a memory-mapped database, a column
store), mmap maps the file into your address space so reads become page faults, not
pread syscalls — and the page cache is shared, not duplicated into a Go buffer.
golang.org/x/exp/mmap is the safe wrapper:
r, err := mmap.Open("index.dat") // memory-maps the file for reading
if err != nil { /* ... */ }
defer r.Close()
b := r.At(off) // single byte, no syscall
n, err := r.ReadAt(buf, off) // io.ReaderAt, no syscall on a resident page
_ = r.Len() // file length
Open "memory-maps the named file for reading"; ReaderAt "reads a memory-mapped file"
and implements io.ReaderAt (x/exp/mmap).
Caveats — mmap is not free and not always a win:
ReadAt calls, but it is
not safe to call Close and reading methods concurrently" (mmap).
A read after Close (unmap) is a segfault, not a Go panic — the lifetime is yours.read. For sequential streaming,
bufio + read readahead often beats mmap.mmap/munmap + page-table setup dwarfs a couple of read calls.SIGBUS.For raw control (anonymous maps, MAP_SHARED, madvise), syscall.Mmap / golang.org/x/sys/unix
expose the bytes directly with the same hazards and no wrapper safety.
The unifying principle: a syscall has fixed overhead (mode switch, ~hundreds of ns) that a small payload can't amortize. Levers, cheapest first:
bufio.Writer turns a byte-at-a-time
loop into one write per buffer fill. The default answer → go-perf-buffered-io.net.Buffers/writev (§3) instead of N writes.sendfile/splice/copy_file_range via io.Copy (§1–2) so
the bytes never enter your process at all.syscall / golang.org/x/sys/unix only when the stdlib can't express the
call (readv, recvmmsg, SO_REUSEPORT, O_DIRECT). Last resort: you lose portability and
poller integration and must handle EINTR/EAGAIN yourself. Profile first (strace -c →
go-perf-os-tooling).A typical HTTP/gRPC service spends almost nothing in raw syscalls relative to allocation and
serialization — confirm the cost is real before reaching below bufio + io.Copy.
go-perf-buffered-io — bufio Reader/Writer/Scanner sizing, Flush, io.Copy/
io.CopyBuffer basics, ReaderFrom/WriterTo fast-path overview. The default I/O layer.go-perf-strings-bytes-zerocopy — unsafe.String/unsafe.Slice zero-copy
[]byte↔string conversion and its read-only/lifetime rules. (Different "zero copy.")go-perf-os-tooling — strace -c, perf, flame graphs to prove the cost is in
syscalls/copies before you optimize here.go-perf-methodology — should-I-optimize, the measure-first loop, picking the diagnostic.go-perf-worker-pools-throughput — concurrency around I/O (many conns, fan-in/out).read/write loop to send a file over a socket. io.Copy(conn, file)
is already sendfile(2) (src/net/sendfile.go).*os.File in a bufio.Reader before io.Copy to a conn — the
syscall.Conn assertion fails and you silently lose sendfile (sendfile.go).Write calls for header+body+trailer — use net.Buffers for one writev(2)
(net.Buffers).net.Buffers value after a partial WriteTo without resetting it — it's
consumed in place (net.Buffers).mmap a small or sequentially-streamed file — page-fault and setup overhead lose
to bufio + read; mmap is for large random-access, read-mostly data.ReadAt an mmap.ReaderAt after Close — that's a segfault, not a panic
(mmap).syscall.Mmap/writev/readv before profiling proves syscall cost —
most services never need to ([go-perf-os-tooling]).sendfile/splice/copy_file_range/writev are
Linux/Unix fast paths; on other platforms the same code is correct but falls back to a copy.references/common-mistakes.md — wrong/right zero-copy and syscall patterns with citations.
references/sources.yaml — source provenance for every claim.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ctoth/golang-skills-plugin --plugin performant-golang34 skills in this plugin
/go-perf-allocator-internalsWhat a Go heap allocation costs — the ~70 size classes and rounding waste (unsafe.Sizeof vs real slot), the tiny allocator packing sub-16B pointer-free objects, the lock-free per-P mcache → mcentral → mheap hierarchy, and scannable vs noscan spans (pointer-free objects cut GC cost; "1 alloc/op" hides different costs). Fires on "still slow after removing allocs", "reduce GC pressure", "size class", "how does Go allocate". Routes to go-perf-escape-analysis, go-perf-sync-pool, go-perf-gc-tuning, go-perf-struct-layout./go-perf-atomics-vs-locksGuides the atomics-vs-locks performance tradeoff in Go — typed sync/atomic ops (atomic.Int64/Pointer/Bool, 1.19) are a single CAS-style instruction, cheaper than a mutex uncontended but still cache-line-bouncing when contended; sync.Mutex's fast path is itself a CAS that only parks the goroutine when contended; RWMutex wins only for long read sections and is slower (scales worse) than a plain Mutex for short ones; the atomic.Pointer copy-on-write config swap; when lock-free isn't worth the correctness risk. Fires on "atomic vs mutex", "is RWMutex faster", "lock-free counter", "reduce locking overhead", "atomic.Pointer config". Routes copylock/atomic-API correctness to go-sync-primitives, memory ordering to go-race-and-memory-model, measuring to go-perf-block-mutex-profiles./go-perf-audience-and-tradeoffsGuides framing a Go performance decision by audience — the same change is right for one and wrong for another: the library author who can't profile callers and must not pessimize the common case (offer AppendXxx/[]byte forms, no unsafe in the API), the app developer who can profile and optimizes the proven hot path, the SRE who needs the code diagnosable (pprof labels, metrics, GOMEMLIMIT), the next reader who pays for clever micro-opts; the clarity-vs-speed through-line. Fires on "should a library do this", "is this worth optimizing for my users", "make this diagnosable", "premature optimization vs pessimization", "who am I optimizing for". Routes the policy root to go-idiomatic-discipline, measure-first to go-perf-methodology./go-perf-benchmarking-statisticsGuides trustworthy Go benchmark measurement at the command level — running each side with -count≥10 into old.txt/new.txt and feeding both to benchstat, reading its ± confidence interval, p-value (Mann–Whitney U), geomean, and the "~" no-significant-difference result, choosing the right unit (b.SetBytes for MB/s, b.ReportMetric for custom throughput), grouping a matrix with -col/-row/-filter, building a reproducible environment (perflock's 90% governor + serialization, frequency scaling. Auto-invokes when comparing benchmark runs, running benchstat, writing a perf A/B, or stabilizing a bench machine, and on "is this benchmark result real", "compare these benchmarks", "did this actually get faster", "my benchmark is noisy", "set up a perf regression test". Routes benchmark loop mechanics (for b.Loop, the sink/DCE trap, ResetTimer, RunParallel) to go-testing-advanced and the why-one-run-lies concept to go-perf-methodology./go-perf-block-mutex-profilesGuides Go contention diagnosis at depth — the block, mutex, and goroutine profiles, why block/mutex are OFF by default and read empty until runtime.SetBlockProfileRate (nanoseconds, 1=everything) / runtime.SetMutexProfileFraction (1/n sampled) are set, the overhead tradeoff, reading delay-vs-contention in pprof, and the goroutine profile for leak and pile-up detection. Auto-invokes on "why is this contended", "diagnose lock contention", "goroutine leak", "my mutex is slow", "profile blocking", "goroutines are piling up". Routes the fix to go-perf-contention-and-sharding / go-perf-atomics-vs-locks, sync correctness to go-sync-primitives, CPU/heap to go-perf-pprof-profiling./go-perf-bounds-check-eliminationGuides bounds-check elimination (BCE) in Go — the compiler checks every slice/array index and the SSA pass removes the ones it proves redundant. Covers seeing surviving checks with go build -gcflags="-d=ssa/check_bce/debug=1", prover-friendly idioms (the _ = b[n-1] / b = b[:n] hint, range loops, constant vs variable offsets), the inlining link, and why -B is a footgun not a fix. Fires on "eliminate bounds checks", "why is this loop slow", "bounds check in hot loop", "optimize this numeric loop". Routes should-I to go-perf-methodology, slices to go-perf-slices.