我們通常用 Golang 來(lái)開(kāi)發(fā)并構(gòu)建高并發(fā)場(chǎng)景下的服務(wù),但是由于 Golang 內(nèi)建的GC機(jī)制多少會(huì)影響服務(wù)的性能,因此,為了減少頻繁GC,Golang提供了對(duì)象重用的機(jī)制,也就是使用sync.Pool構(gòu)建對(duì)象池。
首先sync.Pool是可伸縮的臨時(shí)對(duì)象池,也是并發(fā)安全的。其可伸縮的大小會(huì)受限于內(nèi)存的大小,可以理解為是一個(gè)存放可重用對(duì)象的容器。sync.Pool設(shè)計(jì)的目的就是用于存放已經(jīng)分配的但是暫時(shí)又不用的對(duì)象,而且在需要用到的時(shí)候,可以直接從該pool中取。
pool中任何存放的值可以在任何時(shí)候被刪除而不會(huì)收到通知。另外,在高負(fù)載下pool對(duì)象池可以動(dòng)態(tài)的擴(kuò)容,而在不使用或者說(shuō)并發(fā)量不高時(shí)對(duì)象池會(huì)收縮。關(guān)鍵思想就是對(duì)象的復(fù)用,避免重復(fù)創(chuàng)建、銷毀,從而影響性能。
個(gè)人覺(jué)得它的名字有一定的誤導(dǎo)性,因?yàn)?Pool 里裝的對(duì)象可以被無(wú)通知地被回收,覺(jué)得 sync.Cache 的名字更合適sync.Pool的命名。
sync.Pool首先聲明了兩個(gè)結(jié)構(gòu)體,如下:
// 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 }
為了使得可以在多個(gè)goroutine中高效的使用并發(fā),sync.Pool會(huì)為每個(gè)P(對(duì)應(yīng)CPU,這里有點(diǎn)像GMP模型)都分配一個(gè)本地池,當(dāng)執(zhí)行Get或者Put操作的時(shí)候,會(huì)先將goroutine和某個(gè)P的對(duì)象池關(guān)聯(lián),再對(duì)該池進(jìn)行操作。
每個(gè)P的對(duì)象池分為私有對(duì)象和共享列表對(duì)象,私有對(duì)象只能被特定的P訪問(wèn),共享列表對(duì)象可以被任何P訪問(wèn)。因?yàn)橥粫r(shí)刻一個(gè)P只能執(zhí)行一個(gè)goroutine,所以無(wú)需加鎖,但是對(duì)共享列表對(duì)象進(jìn)行操作時(shí),因?yàn)榭赡苡卸鄠€(gè)goroutine同時(shí)操作,即并發(fā)操作,所以需要加鎖。
需要注意的是 poolLocal 結(jié)構(gòu)體中有個(gè) pad 成員,其目的是為了防止false sharing。cache使用中常見(jiàn)的一個(gè)問(wèn)題是false sharing。當(dāng)不同的線程同時(shí)讀寫同一個(gè) cache line上不同數(shù)據(jù)時(shí)就可能發(fā)生false sharing。false sharing會(huì)導(dǎo)致多核處理器上嚴(yán)重的系統(tǒng)性能下降。具體的解釋說(shuō)明這里就不展開(kāi)贅述了。
sync.Pool 有兩個(gè)公開(kāi)的方法,一個(gè)是Get,另一個(gè)是Put。
我們先來(lái)看一下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方法的源碼可以知道:
我們?cè)賮?lái)看下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方法的源碼,可以知道:
最后我們來(lái)看一下init函數(shù),如下:
func init() { funtime_registerPoolCleanup(poolCleanup) }
可以看到在init的時(shí)候注冊(cè)了一個(gè)PoolCleanup函數(shù),他會(huì)清除掉sync.Pool中的所有的緩存的對(duì)象,這個(gè)注冊(cè)函數(shù)會(huì)在每次GC的時(shí)候運(yùn)行,所以sync.Pool中的值只在兩次GC中間的時(shí)段有效。
示例代碼:
package main import ( "fmt" "sync" ) // 定義一個(gè) Person 結(jié)構(gòu)體,有Name和Age變量 type Person struct { Name string Age int } // 初始化sync.Pool,new函數(shù)就是創(chuàng)建Person結(jié)構(gòu)體 func initPool() *sync.Pool { return sync.Pool{ New: func() interface{} { fmt.Println("創(chuàng)建一個(gè) person.") return Person{} }, } } // 主函數(shù),入口函數(shù) 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("設(shè)置的對(duì)象Name: ", person.Name) fmt.Println("設(shè)置的對(duì)象Age: ", person.Age) fmt.Println("Pool 中有一個(gè)對(duì)象,調(diào)用Get方法獲?。?, pool.Get().(*Person)) fmt.Println("Pool 中沒(méi)有對(duì)象了,再次調(diào)用Get方法:", pool.Get().(*Person)) }
運(yùn)行結(jié)果如下所示:
創(chuàng)建一個(gè) person.
首次從sync.Pool中獲取person:{ 0}
設(shè)置的對(duì)象Name: Jack
設(shè)置的對(duì)象Age: 23
Pool 中有一個(gè)對(duì)象,調(diào)用Get方法獲取:{Jack 23}
創(chuàng)建一個(gè) person.
Pool 中沒(méi)有對(duì)象了,再次調(diào)用Get方法: { 0}
通過(guò)以上的源碼及其示例,我們可以知道:
由此可知,Golang的對(duì)象池嚴(yán)格意義上來(lái)說(shuō)是一個(gè)臨時(shí)的對(duì)象池,適用于儲(chǔ)存一些會(huì)在goroutine間分享的臨時(shí)對(duì)象。主要作用是減少GC,提高性能。在Golang中最常見(jiàn)的使用場(chǎng)景就是fmt包中的輸出緩沖區(qū)了。
代碼Github歸檔地址: sync.Pool使用示例代碼
到此這篇關(guān)于Golang之sync.Pool使用詳解的文章就介紹到這了,更多相關(guān)Golang sync.Pool內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:欽州 汕頭 吐魯番 銅川 蘭州 重慶 梅河口 雞西
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang之sync.Pool使用詳解》,本文關(guān)鍵詞 Golang,之,sync.Pool,使用,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。