Golang中sync.Pool用来提高对象复用几率,减少gc的压力,减少内存分配,它是线程安全的,常用来存储并复用临时对象。
原理
为了减小并发中锁的竞争,sync.pool为每个P(对象cpu线程)分配一个子池子poolLocal,每个poolLocal有private对象和shared共享列表对象,private对象只有对应的P可访问,无需加锁, shared共享列表对象可被其它P共享,需要加锁。
Pool结构体
type Pool struct {
noCopy noCopy //该对象不能被copy使用
local unsafe.Pointer // [p]poolLocal,固定长度
localSize uintptr //本地缓冲池poolLocal的数量
New func() interface{} //用户自定义的用于生成对象的方法
}
Get函数
作用:从Pool中获取一个对象,如果获取不到并且New函数不为空,则通过New创建一个对象并返回。否则返回nil
func (p *Pool) Get() interface{} {
if race.Enabled {
race.Disable()
}
//获取当前线程的poolLocal
l := p.pin()
//如果private对象不为空则直接返回,并将其置为nil
x := l.private
l.private = nil
runtime_procUnpin()
if x == nil {
//private不存在则加锁从shared列表中拿
l.Lock()
last := len(l.shared) - 1
if last >= 0 {
x = l.shared[last]
l.shared = l.shared[:last]
}
l.Unlock()
//如果shared对象列表依然没有的话,则从其它P的poolLocal获取
if x == nil {
x = p.getSlow()
}
}
if race.Enabled {
race.Enable()
if x != nil {
race.Acquire(poolRaceAddr(x))
}
}
// 如果存在New func回调函数,则执行
if x == nil && p.New != nil {
x = p.New()
}
return x
}
Put()函数
作用:将对象x放入到Pool中,以便复用该对象
func (p *Pool) Put(x interface{}) {
if x == nil {
return
}
if race.Enabled {
if fastrand()%4 == 0 {
// Randomly drop x on floor.
return
}
race.ReleaseMerge(poolRaceAddr(x))
race.Disable()
}
//如果当前poolLocal的private对象为nil,则直接赋值
l := p.pin()
if l.private == nil {
l.private = x
x = nil
}
runtime_procUnpin()
//否则加到当前poolLocal的shared列表中
if x != nil {
l.Lock()
l.shared = append(l.shared, x)
l.Unlock()
}
if race.Enabled {
race.Enable()
}
}
poolCleanup回收对象
sync.Pool会在gc时回收pool中的对象
func init() {
runtime_registerPoolCleanup(poolCleanup)
}
func indexLocal(l unsafe.Pointer, i int) *poolLocal {
lp := unsafe.Pointer(uintptr(l) + uintptr(i)*unsafe.Sizeof(poolLocal{}))
return (*poolLocal)(lp)
}
// Implemented in runtime.
func runtime_registerPoolCleanup(cleanup func())
func runtime_procPin() int
func runtime_procUnpin()
func poolCleanup() {
// This function is called with the world stopped, at the beginning of a garbage collection.
// It must not allocate and probably should not call any runtime functions.
// Defensively zero out everything, 2 reasons:
// 1. To prevent false retention of whole Pools.
// 2. If GC happens while a goroutine works with l.shared in Put/Get,
// it will retain whole Pool. So next cycle memory consumption would be doubled.
for i, p := range allPools {
allPools[i] = nil
for i := 0; i < int(p.localSize); i++ {
l := indexLocal(p.local, i)
l.private = nil
for j := range l.shared {
l.shared[j] = nil
}
l.shared = nil
}
p.local = nil
p.localSize = 0
}
allPools = []*Pool{}
}
感谢:http://www.mckee.cn/golang/pkg/syncpool
https://segmentfault.com/a/1190000019973632?utm_source=tag-newest
3986



被折叠的 条评论
为什么被折叠?



