• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Go: The Idea Behind Sync.Pool

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

原文:https://medium.com/swlh/go-the-idea-behind-sync-pool-32da5089df72

-----------------------

 

sync.Pool I was able to decrease the allocations and GC workload.

sync package to create a self-managed temporary retrieval object pool.

Why to use sync.Pool?

sync.Pool can cache objects that are not used temporarily and use them directly (without reallocation) when they are needed next time. This can potentially reduce the GC workload and improve the performance.

How to use sync.Pool?

Put methods to retrieve and return objects. Also a Pool must not be copied after first use.

interface{}. So you need to do a type assertion in order to get the concrete object

// A dummy struct
type Person struct {
	Name string
}

// Initializing pool
var personPool = sync.Pool{
	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	New: func() interface{} { return new(Person) },
}

// Main function
func main() {
	// Get hold of an instance
	newPerson := personPool.Get().(*Person)
	// Defer release function
	// After that the same instance is 
	// reusable by another routine
	defer personPool.Put(newPerson)

	// Using the instance
	newPerson.Name = "Jack"
}

  

sync.Pool example

Benchmark

type Person struct {
	Age int
}

var personPool = sync.Pool{
	New: func() interface{} { return new(Person) },
}

func BenchmarkWithoutPool(b *testing.B) {
	var p *Person
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := 0; j < 10000; j++ {
			p = new(Person)
			p.Age = 23
		}
	}
}

func BenchmarkWithPool(b *testing.B) {
	var p *Person
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for j := 0; j < 10000; j++ {
			p = personPool.Get().(*Person)
			p.Age = 23
			personPool.Put(p)
		}
	}
}

  

sync.Pool benchmark

Benchmark result:

BenchmarkWithoutPool
BenchmarkWithoutPool-8 160698 ns/op 80001 B/op 10000 allocs/op
BenchmarkWithPool
BenchmarkWithPool-8 191163 ns/op 0 B/op 0 allocs/op

Trade-off

sync.Pool than simple initialization.

func BenchmarkPool(b *testing.B) {
	var p sync.Pool
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			p.Put(1)
			p.Get()
		}
	})
}

func BenchmarkAllocation(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			i := 0
			i = i
		}
	})
}

  

Benchmarking sync.Pool and simple Allocation

Benchmark result:

BenchmarkPool
BenchmarkPool-8 283395016 4.40 ns/op
BenchmarkAllocation
BenchmarkAllocation-8 1000000000 0.344 ns/op
 

How does sync.Pool work?

sync.Pool has two containers for objects: local pool (active) and victim cache (archived).

registers to the runtime as a method to clean the pools. This method will be triggered by the GC.

func init() {
runtime_registerPoolCleanup(poolCleanup)
}

When the GC is triggered, objects inside the victim cache will be collected and then objects inside the local pool will be moved to the victim cache.

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
}

oldPools, allPools = allPools, nil
}

Get method will take an object from the victim cache in the first place and if the victim cache was empty the object will be taken from the local pool.

 

 

sync.Pool localPool and victimCache

mutex lock and improves the shared access.

Conclusion

sync.Pool.

mutex lock and improves the shared access.

Conclusion

sync.Pool.

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
代理模式-go/java实现发布时间:2022-07-10
下一篇:
学DSP(二):目标芯片28335,GO!发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap