您好,登录后才能下订单哦!
这篇文章主要讲解了“GoLang sync.Pool怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“GoLang sync.Pool怎么使用”吧!
一句话总结:保存和复用临时对象,减少内存分配,降低GC压力
sync.Pool是可伸缩的,也是并发安全的,其大小仅受限于内存大小。sync.Pool用于存储那些被分配了但是没有使用,而未来可能会使用的值。这样就可以不用再次经过内存分配,可直接复用已有对象,减轻GC的压力,从而提升系统性能。
type Student struct {
Name string
Age int32
Remark [1024]byte
}
func main() {
var studentPool = sync.Pool{
New: func() interface{} {
return new(Student)
},
}
}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()用于从对象池中获取对象,因为返回值是interface{},因此需要类型转换
Put()则是在对象使用完毕之后,返回对象池
以下是性能测试的代码:
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
以下是性能测试的结果:
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
在这个例子中,因为 Student 结构体内存占用较小,内存分配几乎不耗时间。而标准库 json 反序列化时利用了反射,效率是比较低的,占据了大部分时间,因此两种方式最终的执行时间几乎没什么变化。但是内存占用差了一个数量级,使用了 sync.Pool 后,内存占用仅为未使用的 240/1392 = 1/6,对 GC 的影响就很大了。我们甚至在fmt.Printf的源码里面也使用了sync.Pool进行性能优化!
感谢各位的阅读,以上就是“GoLang sync.Pool怎么使用”的内容了,经过本文的学习后,相信大家对GoLang sync.Pool怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。