我們通常用 Golang 來開發(fā)并構建高并發(fā)場景下的服務,但是由于 Golang 內建的GC機制多少會影響服務的性能,因此,為了減少頻繁GC,Golang提供了對象重用的機制,也就是使用sync.Pool構建對象池。
首先sync.Pool是可伸縮的臨時對象池,也是并發(fā)安全的。其可伸縮的大小會受限于內存的大小,可以理解為是一個存放可重用對象的容器。sync.Pool設計的目的就是用于存放已經分配的但是暫時又不用的對象,而且在需要用到的時候,可以直接從該pool中取。
pool中任何存放的值可以在任何時候被刪除而不會收到通知。另外,在高負載下pool對象池可以動態(tài)的擴容,而在不使用或者說并發(fā)量不高時對象池會收縮。關鍵思想就是對象的復用,避免重復創(chuàng)建、銷毀,從而影響性能。
個人覺得它的名字有一定的誤導性,因為 Pool 里裝的對象可以被無通知地被回收,覺得 sync.Cache 的名字更合適sync.Pool的命名。
sync.Pool首先聲明了兩個結構體,如下:
// Local per-P Pool appendix. type poolLocalInternal struct { private interface{} // Can be used only by the respective P. shared poolChain // Local P can pushHead/popHead; any P can popTail. } type poolLocal struct { poolLocalInternal // Prevents false sharing on widespread platforms with // 128 mod (cache line size) = 0 . pad [128 - unsafe.Sizeof(poolLocalInternal{})%128]byte }
為了使得可以在多個goroutine中高效的使用并發(fā),sync.Pool會為每個P(對應CPU,這里有點像GMP模型)都分配一個本地池,當執(zhí)行Get或者Put操作的時候,會先將goroutine和某個P的對象池關聯,再對該池進行操作。
每個P的對象池分為私有對象和共享列表對象,私有對象只能被特定的P訪問,共享列表對象可以被任何P訪問。因為同一時刻一個P只能執(zhí)行一個goroutine,所以無需加鎖,但是對共享列表對象進行操作時,因為可能有多個goroutine同時操作,即并發(fā)操作,所以需要加鎖。
需要注意的是 poolLocal 結構體中有個 pad 成員,其目的是為了防止false sharing。cache使用中常見的一個問題是false sharing。當不同的線程同時讀寫同一個 cache line上不同數據時就可能發(fā)生false sharing。false sharing會導致多核處理器上嚴重的系統性能下降。具體的解釋說明這里就不展開贅述了。
sync.Pool 有兩個公開的方法,一個是Get,另一個是Put。
我們先來看一下Put方法的源碼,如下:
// Put adds x to the 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() } 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() } }
閱讀以上Put方法的源碼可以知道:
我們再來看下Get方法的源碼,如下:
func (p *Pool) Get() interface{} { if race.Enabled { race.Disable() } l, pid := p.pin() x := l.private l.private = nil if x == nil { // Try to pop the head of the local shard. We prefer // the head over the tail for temporal locality of // reuse. 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 }
閱讀以上Get方法的源碼,可以知道:
最后我們來看一下init函數,如下:
func init() { funtime_registerPoolCleanup(poolCleanup) }
可以看到在init的時候注冊了一個PoolCleanup函數,他會清除掉sync.Pool中的所有的緩存的對象,這個注冊函數會在每次GC的時候運行,所以sync.Pool中的值只在兩次GC中間的時段有效。
示例代碼:
package main import ( "fmt" "sync" ) // 定義一個 Person 結構體,有Name和Age變量 type Person struct { Name string Age int } // 初始化sync.Pool,new函數就是創(chuàng)建Person結構體 func initPool() *sync.Pool { return sync.Pool{ New: func() interface{} { fmt.Println("創(chuàng)建一個 person.") return Person{} }, } } // 主函數,入口函數 func main() { pool := initPool() person := pool.Get().(*Person) fmt.Println("首次從sync.Pool中獲取person:", person) person.Name = "Jack" person.Age = 23 pool.Put(person) fmt.Println("設置的對象Name: ", person.Name) fmt.Println("設置的對象Age: ", person.Age) fmt.Println("Pool 中有一個對象,調用Get方法獲?。?, pool.Get().(*Person)) fmt.Println("Pool 中沒有對象了,再次調用Get方法:", pool.Get().(*Person)) }
運行結果如下所示:
創(chuàng)建一個 person.
首次從sync.Pool中獲取person:{ 0}
設置的對象Name: Jack
設置的對象Age: 23
Pool 中有一個對象,調用Get方法獲?。簕Jack 23}
創(chuàng)建一個 person.
Pool 中沒有對象了,再次調用Get方法: { 0}
通過以上的源碼及其示例,我們可以知道:
由此可知,Golang的對象池嚴格意義上來說是一個臨時的對象池,適用于儲存一些會在goroutine間分享的臨時對象。主要作用是減少GC,提高性能。在Golang中最常見的使用場景就是fmt包中的輸出緩沖區(qū)了。
代碼Github歸檔地址: sync.Pool使用示例代碼
到此這篇關于Golang之sync.Pool使用詳解的文章就介紹到這了,更多相關Golang sync.Pool內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!