Go: The Idea Behind Sync.Pool
原文:https://medium.com/swlh/go-the-idea-behind-sync-pool-32da5089df72
-----------------------
I encountered a problem in Go Garbage Collection inside a project of mine recently. A massive amount of object were allocated repeatedly and caused a huge workload of GC. Using sync.Pool
I was able to decrease the allocations and GC workload.
What is sync.Pool?
One of the highlights of Go 1.3 release was sync Pool. It is a component under the sync
package to create a self-managed temporary retrieval object pool.
Why to use sync.Pool?
We want to keep the GC overhead as little as possible. Frequent allocation and recycling of memory will cause a heavy burden to GC. 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?
First you need to set the New
function. This function will be used when there is no cached object in the Pool. After that you only need using Get
and Put
methods to retrieve and return objects. Also a Pool must not be copied after first use.
Due to New
function type, which is func() interface{}
, Get
method returns an interface{}
. So you need to do a type assertion in order to get the concrete object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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) } } } |
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
Everything in life is a trade-off. The Pool has also its performance cost. It is much slower to use sync.Pool
than simple initialization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 } }) } |
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).
According to the sync/pool.go
, package init
function 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
}
New objects are put in the local pool. Calling Put
method will put the object into the local pool as well. Calling 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.

For your information, the Go 1.12 sync.Pool implementation uses a mutex
based locking for thread-safe operations from multiple Goroutines. Go 1.13 introduces a doubly-linked list as a shared pool which removes the mutex
lock and improves the shared access.
Conclusion
When there is an expensive object you have to create it frequently, it can be very beneficial to use sync.Pool
.
For your information, the Go 1.12 sync.Pool implementation uses a mutex
based locking for thread-safe operations from multiple Goroutines. Go 1.13 introduces a doubly-linked list as a shared pool which removes the mutex
lock and improves the shared access.
Conclusion
When there is an expensive object you have to create it frequently, it can be very beneficial to use sync.Pool
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-09-30 es6中的import,export浏览器已经支持
2016-09-30 Jquery 源码学习
2016-09-30 php 中的魔术方法-----“事件方法”
2016-09-30 php 语法中有 let 吗?