About sync.Pool and what changed in go1.13
Huy Do
Software Engineer, Observability Team / LINE Corp
Huy Do
Software Engineer, Observability Team / LINE Corp
type Pool struct { New func() interface{} } func (p *Pool) Put(x interface{}) {} func (p *Pool) Get() interface{} {}
func (p *Pool) Get() interface{} func (p *Pool) Put(v interface{}) bool func (p *Pool) SetCapacity(local, global int) func (p *Pool) Drain() []interface{}
Finally
groups.google.com/forum/#!searchin/golang-dev/pool/golang-dev/QtSIOFsNFrQ/N89_5dZWsdIJ
9type Pool struct { list []interface{} // offset known to runtime mu Mutex // guards list } func runtime_registerPool(*Pool) func (p *Pool) Put(x interface{}) { ... p.mu.Lock() p.list = append(p.list, x) p.mu.Unlock() } func (p *Pool) Get() interface{} { ... p.mu.Lock() var x interface{} x = p.list[n-1] p.list = p.list[:n-1] p.mu.Unlock() ... return x }
// pool.go func init() { runtime_registerPoolCleanup(poolCleanup) } func clearpools() { poolcleanup() } func poolCleanup() { // assign pool to nil, size to 0 } // mgc.go func gcStart(trigger gcTrigger) { [...] // clearpools before we start the GC clearpools() [...] }
github.com/golang/go/commit/f8e0057bb71cded5bb2d0b09c6292b13c59b5748
go-review.googlesource.com/c/go/+/166960
go-review.googlesource.com/c/go/+/166957/7
go-review.googlesource.com/c/go/+/166958/8
20
go-review.googlesource.com/c/go/+/166961/10
23
> Currently, every Pool is cleared completely at the start of each GC.
This is a problem for heavy users of Pool because it causes an
allocation spike immediately after Pools are clear, which impacts both
throughput and latency.
func poolCleanup() { [...] // Drop victim caches from all pools. for _, p := range oldPools { p.victim = nil p.victimSize = 0 } // Move primary cache to victim cache. for _, p := range allPools { p.victim = p.local p.victimSize = p.localSize p.local = nil p.localSize = 0 } // The pools with non-empty primary caches now have non-empty // victim caches and no pools have primary caches. oldPools, allPools = allPools, nil [...] }