網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
使用場(chǎng)景
一句話總結(jié):保存和復(fù)用臨時(shí)對(duì)象,減少內(nèi)存分配,降低GC壓力
sync.Pool
是可伸縮的,也是并發(fā)安全的,其大小僅受限于內(nèi)存大小。sync.Pool
用于存儲(chǔ)那些被分配了但是沒有使用,而未來可能會(huì)使用的值。這樣就可以不用再次經(jīng)過內(nèi)存分配,可直接復(fù)用已有對(duì)象,減輕GC的壓力,從而提升系統(tǒng)性能。
使用方法
聲明對(duì)象池
type Student struct { Name string Age int32 Remark [1024]byte } func main() { var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } }
Get & Put
type Student struct { Name string Age int32 Remark [1024]byte } var buf, _ = json.Marshal(Student{Name: "lxy", Age: 18}) func Unmarsh() { var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } stu := studentPool.Get().(*Student) err := json.Unmarshal(buf, stu) if err != nil { return } studentPool.Put(stu) }
-
Get()
用于從對(duì)象池中獲取對(duì)象,因?yàn)榉祷刂凳?code>interface{},因此需要類型轉(zhuǎn)換 -
Put()
則是在對(duì)象使用完畢之后,返回對(duì)象池
性能測(cè)試
以下是性能測(cè)試的代碼:
package benchmem import ( "encoding/json" "sync" "testing" ) type Student struct { Name string Age int32 Remark [1024]byte } var buf, _ = json.Marshal(Student{Name: "lxy", Age: 18}) var studentPool = sync.Pool{ New: func() interface{} { return new(Student) }, } func BenchmarkUnmarshal(b *testing.B) { for n := 0; n < b.N; n++ { stu := &Student{} json.Unmarshal(buf, stu) } } func BenchmarkUnmarshalWithPool(b *testing.B) { for n := 0; n < b.N; n++ { stu := studentPool.Get().(*Student) json.Unmarshal(buf, stu) studentPool.Put(stu) } }
輸入以下命令:
?go test -bench . -benchmem
以下是性能測(cè)試的結(jié)果:
goos: windows
goarch: amd64 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
pkg: ginTest ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
BenchmarkUnmarshal-8 ? ? ? ? ? ? ? 17004 ? ? ? ? ? ? 74103 ns/op ? ? ? ? ? ?1392 B/op ? ? ? ? ?8 allocs/op
BenchmarkUnmarshalWithPool-8 ? ? ? 17001 ? ? ? ? ? ? 71173 ns/op ? ? ? ? ? ? 240 B/op ? ? ? ? ?7 allocs/op
PASS
ok ? ? ?ginTest 3.923s
在這個(gè)例子中,因?yàn)?Student 結(jié)構(gòu)體內(nèi)存占用較小,內(nèi)存分配幾乎不耗時(shí)間。而標(biāo)準(zhǔn)庫(kù) json 反序列化時(shí)利用了反射,效率是比較低的,占據(jù)了大部分時(shí)間,因此兩種方式最終的執(zhí)行時(shí)間幾乎沒什么變化。但是內(nèi)存占用差了一個(gè)數(shù)量級(jí),使用了 sync.Pool
后,內(nèi)存占用僅為未使用的 240/1392 = 1/6
,對(duì) GC 的影響就很大了。
我們甚至在fmt.Printf
的源碼里面也使用了sync.Pool
進(jìn)行性能優(yōu)化!
原文鏈接:https://blog.csdn.net/qq_61039408/article/details/128498046
相關(guān)推薦
- 2023-05-03 python?re.match函數(shù)的具體使用_python
- 2022-07-26 tensorflow 加載模型時(shí) 報(bào)錯(cuò): ValueError: No variables to s
- 2022-10-15 Python基礎(chǔ)學(xué)習(xí)之函數(shù)和代碼復(fù)用詳解_python
- 2022-12-03 Keepalived如何實(shí)現(xiàn)Nginx高可用_nginx
- 2022-09-15 Android?Jetpack庫(kù)剖析之ViewModel組件篇_Android
- 2022-08-28 Spring Boot整合ElasticSearch
- 2023-01-30 delphi?判斷字符串是否為純數(shù)字組合_Delphi
- 2022-04-16 Python3如何將源目錄中的圖片用MD5命名并可以設(shè)定目標(biāo)目錄_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支