当前位置:网站首页>golang 临时对象池优化
golang 临时对象池优化
2022-06-26 15:22:00 【gitxuzan_】
切片对象池优化
package main
import (
"log"
"sync"
)
// gitxuzan
func main() {
pools := NewPool()
arr := pools.Alloc(11) // 容量为20
arr = append(arr, 1, 2, 3, 4)
log.Println(arr, len(arr), cap(arr))
pools.Free(arr) // 用完放回池子
arr = pools.Alloc(3) // 第二次如果 ,如果池子容量5 != 20不一样,会重新创建
log.Println(arr, len(arr), cap(arr))
pools.Free(arr) // 用完放回池子
arr = pools.Alloc(11) // 容量为20 和 第一次容量一样,所以会在池子里面取
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("创建的池子")
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
}
}
}
}
来自: https://blog.cyeam.com/golang/2017/02/08/go-optimize-slice-pool
边栏推荐
- [CEPH] cephfs internal implementation (I): Concept -- undigested
- 【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
- JS之简易deepCopy(简介递归)
- SAP sales data actual shipment data export sales
- 1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
- 【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
- 【ceph】CephFS 内部实现(二):示例--未消化
- vsomeip3 双机通信文件配置
- 通过券商经理的开户链接开股票账户安全吗?还是去证券公司开户安全?
- 粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)
猜你喜欢

【小程序实战系列】小程序框架 页面注册 生命周期 介绍

【TcaplusDB知识库】TcaplusDB运维单据介绍

RestCloud ETL解决shell脚本参数化

English语法_形容词/副词3级 - 原级句型
![[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction

How to handle 2gcsv files that cannot be opened? Use byzer

【TcaplusDB知识库】TcaplusDB常规单据介绍

Using restcloud ETL shell component to schedule dataX offline tasks
![[C language practice - printing hollow upper triangle and its deformation]](/img/56/6a88b3d8de32a3215399f915bba33e.png)
[C language practice - printing hollow upper triangle and its deformation]

【TcaplusDB知识库】TcaplusDB系统用户组介绍
随机推荐
PHP file upload 00 truncation
如何配置使用新的单线激光雷达
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
评价——模糊综合评价
评价——TOPSIS
Evaluate:huggingface评价指标模块入门详细介绍
/etc/profile、/etc/bashrc、~/.bashrc的区别
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
Secure JSON protocol
IDEA本地代理后,无法下载插件
北京房山区专精特新小巨人企业认定条件,补贴50万
Comparative analysis of restcloud ETL and kettle
【ceph】CEPHFS 内部实现(一):概念篇--未消化
SAP GUI 770 Download
【TcaplusDB知识库】TcaplusDB系统管理介绍
【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
MySQL数据库基本SQL语句教程之高级操作