What is the purpose of sync.Pool, and how does it help reduce GC pressure in high-throughput applications?
Last updated
sync.Poolis a cache of reusable temporary objects:Get()one, use it,Put()it back, cutting heap allocations and GC scanning in hot paths.A
Newfunc builds items when empty, and the pool is sharded per-P so it's effectively lock-free.The pool is cleared on GC, so never rely on persistence. Always reset state before reuse, and use it for short-lived, frequently allocated objects like buffers.