目录

Golang sync pool 调研

目录

Go 语言的 sync.Pool 经常被调用,理解它的使用对理解GC有帮助,对提高程序性能、减少gc有帮助。本文借助了gemini flash3 模型收集数据提供思路。


sync.Pool 的接口和源码

New Get Put

seaweedfs的应用

https://github.com/search?q=repo%3Aseaweedfs%2Fseaweedfs%20sync.Pool&type=code

// https://github.com/seaweedfs/seaweedfs/blob/5eead9409a03a813b275d2f4ab8a86a17cbb586d/weed/util/mem/slot_pool.go#L7
// weed/util/mem/slot_pool.go
package mem

import (
	"sync"
)

var pools []*sync.Pool

const (
	min_size = 1024
)

func bitCount(size int) (count int) {
	for ; size > min_size; count++ {
		size = (size + 1) >> 1
	}
	return
}

func init() {
	// 1KB ~ 256MB
	pools = make([]*sync.Pool, bitCount(1024*1024*256))
	for i := 0; i < len(pools); i++ {
		slotSize := 1024 << i
		pools[i] = &sync.Pool{
			New: func() interface{} {
				buffer := make([]byte, slotSize)
				return &buffer
			},
		}
	}
}

func getSlotPool(size int) (*sync.Pool, bool) {
	index := bitCount(size)
	if index >= len(pools) {
		return nil, false
	}
	return pools[index], true
}

func Allocate(size int) []byte {
	if pool, found := getSlotPool(size); found {
		slab := *pool.Get().(*[]byte)
		return slab[:size]
	}
	return make([]byte, size)
}

func Free(buf []byte) {
	if pool, found := getSlotPool(cap(buf)); found {
		pool.Put(&buf)
	}
}

最佳实践——什么时候该用

大对象频繁创建的时候,用到它有好处

避坑——什么时候不该用

对象太小的时候,用到它,反而上下文切换开销会比较大