new函数的调用时机和pool的内存释放规则
以下代码调用了四次Get函数,但是并不是每次都会new
- 第一次,是
a := pool.Get().([]byte)
,首次Get,在pool的private私有池没有对象,在共享池也没有对象,不存在victim cache,所以会new。 - 第二次,是
b := pool.Get().([]byte)
,因为a取出后,pool的私有池又成为了空。在共享池也没有对象,不存在victim cache,所以会new。 - 第三次,是
c := pool.Get().([]byte)
,理论上:再次取 a,不会执行new,此时victim cache对象。 但是实际上,此处并不确定,有时victim cache的私有池还保留对象,有时已经为空 - 第四次,一定会执行new,因为经过第一次gc,主缓存清空,第二次gc,victim缓存清空。池中没有对象所以一定会new。
总结:
- 当pool中私有池有对象,不进行new,而是返回私有池对象。
- 当pool中私有池没有对象,共享池有对象,则返回共享池对象的最前一个。
- 当pool中私有池没有对象,共享池也没有对象,则尝试窃取其它P的共享池对象。
- 当窃取也窃取不到,则尝试使用victim缓存,再执行1.2.3.4.步骤
- 当victim缓存也没有时,会执行new。
- pool主缓存中的对象会在GC时移到victim缓存,而此处gc中pool的victim缓存中的对象会在下次gc时被释放。
- Put时,如果私有池已经存在对象,则放到共享池,否则放到私有池中
- 从私有池中取对象是协程安全的,而从共享池取对象需要加锁,这是因为存在其它P来窃取本P的共享池的现象。
func Test_1(t *testing.T) {
pool := sync.Pool{New: func() interface{} {
fmt.Println("new")
return make([]byte, 2 << 10)
}}
fmt.Println("start")
a := pool.Get().([]byte)
gcStats := debug.GCStats{}
runtime.GC()
debug.ReadGCStats(&gcStats)
fmt.Printf("numgc: %d\n", gcStats.NumGC)
for i := range a {
a[i] = 1
}
b := pool.Get().([]byte)
fmt.Println(b[0])
pool.Put(a)
fmt.Println("a == nil :", a==nil)
runtime.GC()
debug.ReadGCStats(&gcStats)
fmt.Printf("numgc: %d\n", gcStats.NumGC)
fmt.Println("a == nil :", a==nil)
c := pool.Get().([]byte)
fmt.Println(c[0])
pool.Put(c)
runtime.GC()
runtime.GC()
debug.ReadGCStats(&gcStats)
fmt.Printf("numgc: %d\n", gcStats.NumGC)
pool.Get()
}
Get源码:
方法从池中选择任意项,然后将其从池移除,并将其返回给调用者。
Get可以选择忽略池并将其视为空的。
调用者不应该假定传递给Put和Get的值之间有任何关系。
如果Get返回nil,而p.New是非nil,则Get返回调用p.New的结果。
func (p *Pool) Get() interface{} {
if race.Enabled {
race.Disable()
}
l, pid := p.pin()
x := l.private
l.private = nil
if x == nil {
x, _ = l.shared.popHead()
if x == nil {
x = p.getSlow(pid)
}
}
runtime_procUnpin()
if race.Enabled {
race.Enable()
if x != nil {
race.Acquire(poolRaceAddr(x))
}
}
if x == nil && p.New != nil {
x = p.New()
}
return x
}
func (p *Pool) getSlow(pid int) interface{} {
size := runtime_LoadAcquintptr(&p.localSize)
locals := p.local
for i := 0; i < int(size); i++ {
l := indexLocal(locals, (pid+i+1)%int(size))
if x, _ := l.shared.popTail(); x != nil {
return x
}
}
size = atomic.LoadUintptr(&p.victimSize)
if uintptr(pid) >= size {
return nil
}
locals = p.victim
l := indexLocal(locals, pid)
if x := l.private; x != nil {
l.private = nil
return x
}
for i := 0; i < int(size); i++ {
l := indexLocal(locals, (pid+i)%int(size))
if x, _ := l.shared.popTail(); x != nil {
return x
}
}
atomic.StoreUintptr(&p.victimSize, 0)
return nil
}
Put源码
func (p *Pool) Put(x interface{}) {
if x == nil {
return
}
if race.Enabled {
if fastrand()%4 == 0 {
return
}
race.ReleaseMerge(poolRaceAddr(x))
race.Disable()
}
l, _ := p.pin()
if l.private == nil {
l.private = x
x = nil
}
if x != nil {
l.shared.pushHead(x)
}
runtime_procUnpin()
if race.Enabled {
race.Enable()
}
}
Gc 清除pool源码
func poolCleanup() {
for _, p := range oldPools {
p.victim = nil
p.victimSize = 0
}
for _, p := range allPools {
p.victim = p.local
p.victimSize = p.localSize
p.local = nil
p.localSize = 0
}
oldPools, allPools = allPools, nil
}
不要认为Put进去的对象就是下次Get到的对象
- 有可能因为GC释放,导致pool清空,会重新new对象
- 有可能本P的池为空,从其它P窃取了对象,而不是本P之前放进去的对象
- 因此,有必要对获取到的对象进行某种初始化赋值或者重置操作
func Test_2(t *testing.T) {
pool := sync.Pool{New: func() interface{} {
fmt.Println("new")
return "default"
}}
wg := sync.WaitGroup{}
for i := 0; i < 2; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
val := pool.Get().(string)
fmt.Printf("init-gorouteine-%d:%s\n", i, val)
newVal := "goroutine-" + strconv.Itoa(i)
pool.Put(newVal)
fmt.Printf("newVal-gorouteine-%d:%s\n", i, newVal)
for j := 0; j < 20; j++ {
val = pool.Get().(string)
fmt.Printf("loop-gorouteine-%d:%s\n", i, val)
if newVal != val {
t.Errorf("不相等,存在错误gorouteine-%d:%s\n", i, val)
runtime.Goexit()
}
pool.Put(val)
runtime.Gosched()
}
}()
}
wg.Wait()
}
pool的意义
提高性能的几个利器,并发,预处理,缓存。而pool就是缓存。pool减少了申请堆内存分配的次数。降低了程序的GC频繁度。以下对比了使用pool和不使用pool的性能:
go test -bench ".*_3" -run '' .\testpool_test.go -v -benchmem
func Benchmark_3(b *testing.B) {
const routineCount = 10
const size = 1 << 20
b.Run("no-pool", func(b *testing.B) {
wg := sync.WaitGroup{}
for i := 0; i < routineCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < b.N; j++ {
b := make([]byte, size)
b[0] = 1
}
}()
}
wg.Wait()
})
b.Run("pool", func(b *testing.B) {
wg := sync.WaitGroup{}
pool := sync.Pool{
New: func() interface{} {
b := make([]byte, size)
return b
},
}
for i := 0; i < routineCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < b.N; j++ {
b := pool.Get().([]byte)
b[0] = 1
pool.Put(b)
}
}()
}
wg.Wait()
})
}
测试结果,使用pool理论达到了每秒1281万次,而不使用pool理论每秒2551次,差距巨大。
使用pool的内存操作仅256B每次,而不使用pool达到了1m多(每次申请的内存就是1m)。申请的对象数量一致。可以预见到使用pool可以复用对象,而不是反复重新分配堆内存和释放堆内存。在高并发场景下,对需要频繁创建对象时使用pool可以大大提高性能。
goos: windows
goarch: amd64
pkg: mytest/testpool
cpu: AMD Ryzen 9 3900X 12-Core Processor
Benchmark_3
Benchmark_3/no-pool
Benchmark_3/no-pool-24 2551 477658 ns/op 10485801 B/op 10 allocs/op
Benchmark_3/pool
Benchmark_3/pool-24 12814890 86.67 ns/op 256 B/op 10 allocs/op
PASS
ok mytest/testpool 3.488s
gin 中的pool
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := engine.pool.Get().(*Context)
c.writermem.reset(w)
c.Request = req
c.reset()
engine.handleHTTPRequest(c)
engine.pool.Put(c)
}
在gin中使用到了pool用来复用Context对象。在并发场景下,其是线程安全的。gin对Get取回来的对象都进行了reset操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)