当前位置:网站首页>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
边栏推荐
- Function: crypto JS encryption and decryption
- 【小程序实战系列】小程序框架 页面注册 生命周期 介绍
- [file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- 【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
- 【ceph】CephFS 内部实现(二):示例--未消化
- 夏令营来啦!!!冲冲冲
- RestCloud ETL解决shell脚本参数化
- Comparative analysis of restcloud ETL and kettle
- 一键安装gcc脚本
猜你喜欢

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)

功能:crypto-js加密解密

Evaluation - TOPSIS

Evaluate:huggingface评价指标模块入门详细介绍

Using restcloud ETL shell component to schedule dataX offline tasks

【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化

RestCloud ETL与Kettle对比分析
![[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested](/img/f9/8e2b55a33e1613a49e08e119ae0086.png)
[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested
Advanced operation of MySQL database basic SQL statement tutorial
![[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
随机推荐
数据库-完整性约束
One click GCC script installation
Summer camp is coming!!! Chongchongchong
vsomeip3 双机通信文件配置
AbortController的使用
Redis cluster
Evaluation - TOPSIS
JS events
Database - integrity constraints
RestCloud ETL解决shell脚本参数化
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
评价——模糊综合评价
Restcloud ETL resolves shell script parameterization
在哪个平台买股票开户安全?求指导
[applet practice series] Introduction to the registration life cycle of the applet framework page
Shell script multi process concurrent writing method example (high level cultivation)
/etc/profile、/etc/bashrc、~/.bashrc的区别
el-dialog拖拽,边界问题完全修正,网上版本的bug修复
Analysis of ble packet capturing debugging information