当前位置:网站首页>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
边栏推荐
- Why are encoder and decoder structures often used in image segmentation tasks?
- 【leetcode】48.旋转图像
- Is it safe to open an account for mobile stock registration? Is there any risk?
- Function: crypto JS encryption and decryption
- el-dialog拖拽,边界问题完全修正,网上版本的bug修复
- Secure JSON protocol
- Use of abortcontroller
- Compile configuration in file
- # 粒子滤波 PF——三维匀速运动CV目标跟踪(粒子滤波VS扩展卡尔曼滤波)
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
猜你喜欢

Comparative analysis of restcloud ETL and kettle
MySQL数据库基本SQL语句教程之高级操作

English语法_形容词/副词3级 - 原级句型

粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)

el-dialog拖拽,边界问题完全修正,网上版本的bug修复
Mr. Du said that the website was updated with illustrations

Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
![[tcapulusdb knowledge base] Introduction to tcapulusdb data structure](/img/64/4d7ec393d8469cdadc89078a8cf4b1.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure

Function: crypto JS encryption and decryption

音视频学习(三)——sip协议
随机推荐
刷题笔记(十九)--二叉树:二叉搜索树的修改与构造
【TcaplusDB知识库】TcaplusDB系统用户组介绍
【TcaplusDB知识库】TcaplusDB运维单据介绍
评价——TOPSIS
【leetcode】701. Insert operation in binary search tree
面试高频 | 你追我赶的Flink双流join
Redis cluster
feil_ The working directory on the left of uvission4 disappears
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
[graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet
夏令营来啦!!!冲冲冲
Evaluate:huggingface评价指标模块入门详细介绍
如何配置使用新的单线激光雷达
Summer camp is coming!!! Chongchongchong
selenium chrome 禁用js 禁用图片
[tcapulusdb knowledge base] Introduction to tcapulusdb system management
Unity unitywebrequest download package
Summary of data interface API used in word search and translation applications
Lexin AWS IOT expresslink module achieves universal availability
【文件】VFS四大struct:file、dentry、inode、super_block 是什么?区别?关系?--编辑中