当前位置:网站首页>Golang temporary object pool optimization
Golang temporary object pool optimization
2022-06-26 15:40:00 【gitxuzan_】
Slice object pool optimization
package main
import (
"log"
"sync"
)
// gitxuzan
func main() {
pools := NewPool()
arr := pools.Alloc(11) // Capacity of 20
arr = append(arr, 1, 2, 3, 4)
log.Println(arr, len(arr), cap(arr))
pools.Free(arr) // Put it back in the pool
arr = pools.Alloc(3) // The second time if , If the pool capacity 5 != 20 Dissimilarity , Will recreate
log.Println(arr, len(arr), cap(arr))
pools.Free(arr) // Put it back in the pool
arr = pools.Alloc(11) // Capacity of 20 and Same capacity for the first time , So I will take it from the pool
arr = append(arr, 1, 2, 3, 4, 5)
log.Println(arr, len(arr), cap(arr))
}
var DEFAULT_SYNC_POOL *SyncPool
func NewPool() *SyncPool {
DEFAULT_SYNC_POOL = NewSyncPool(
5,
30000,
2,
)
return DEFAULT_SYNC_POOL
}
func Alloc(size int) []int64 {
return DEFAULT_SYNC_POOL.Alloc(size)
}
func Free(mem []int64) {
DEFAULT_SYNC_POOL.Free(mem)
}
// SyncPool is a sync.Pool base slab allocation memory pool
type SyncPool struct {
classes []sync.Pool
classesSize []int
minSize int
maxSize int
}
func NewSyncPool(minSize, maxSize, factor int) *SyncPool {
n := 0
for chunkSize := minSize; chunkSize <= maxSize; chunkSize *= factor {
n++
}
pool := &SyncPool{
make([]sync.Pool, n),
make([]int, n),
minSize, maxSize,
}
n = 0
for chunkSize := minSize; chunkSize <= maxSize; chunkSize *= factor {
pool.classesSize[n] = chunkSize
pool.classes[n].New = func(size int) func() interface{
} {
return func() interface{
} {
log.Println(" Created pool ")
buf := make([]int64, size)
return &buf
}
}(chunkSize)
n++
}
return pool
}
func (pool *SyncPool) Alloc(size int) []int64 {
if size <= pool.maxSize {
for i := 0; i < len(pool.classesSize); i++ {
if pool.classesSize[i] >= size {
mem := pool.classes[i].Get().(*[]int64)
// return (*mem)[:size]
return (*mem)[:0]
}
}
}
return make([]int64, 0, size)
}
func (pool *SyncPool) Free(mem []int64) {
if size := cap(mem); size <= pool.maxSize {
for i := 0; i < len(pool.classesSize); i++ {
if pool.classesSize[i] >= size {
pool.classes[i].Put(&mem)
return
}
}
}
}
come from : https://blog.cyeam.com/golang/2017/02/08/go-optimize-slice-pool
边栏推荐
- Utilisation d'abortcontroller
- [graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet
- [tcapulusdb knowledge base] Introduction to tcapulusdb system management
- 【C语言练习——打印空心上三角及其变形】
- 音视频学习(二)——帧率、码流和分辨率
- [tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
- nanoPi Duo2连接wifi
- Evaluation - TOPSIS
- CNN优化trick
- Unity C # e-learning (IX) -- wwwfrom
猜你喜欢

1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)

【问题解决】新版webots纹理等资源文件加载/下载时间过长

【ceph】cephfs caps简介

刷题笔记(十九)--二叉树:二叉搜索树的修改与构造

Using restcloud ETL shell component to schedule dataX offline tasks
![[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing](/img/b6/d288065747425863b9af95ec6fd554.png)
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
![[CEPH] Introduction to cephfs caps](/img/41/7d59ecbc1129503c8858dc0304a3f9.png)
[CEPH] Introduction to cephfs caps

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

Database - sequence
![[applet practice series] Introduction to the registration life cycle of the applet framework page](/img/82/5a9219a7c2ee211180cc4b2d138204.png)
[applet practice series] Introduction to the registration life cycle of the applet framework page
随机推荐
nanoPi Duo2连接wifi
Cache page keepalive use in Vue
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
Using restcloud ETL shell component to schedule dataX offline tasks
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
JS events
HR export data Excel VBA
Is it safe to open an account for mobile stock registration? Is there any risk?
HW safety response
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
【微信小程序】事件绑定,你搞懂了吗?
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
High frequency interview 𞓜 Flink Shuangliu join
【TcaplusDB知识库】TcaplusDB系统用户组介绍
【文件】VFS四大struct:file、dentry、inode、super_block 是什么?区别?关系?--编辑中
One click GCC script installation
【问题解决】新版webots纹理等资源文件加载/下载时间过长
为什么图像分割任务中经常用到编码器和解码器结构?
Applet: uniapp solves vendor JS is too large