当前位置:网站首页>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
边栏推荐
- [file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
- TCP/IP协议竟然有这么多漏洞?
- About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
- 【ceph】cephfs caps简介
- Unity C # e-learning (10) -- unitywebrequest (1)
- shell脚本多进程并发写法实例(高阶修炼)
- RestCloud ETL与Kettle对比分析
- Function: crypto JS encryption and decryption
- Unable to download Plug-in after idea local agent
- Utilisation d'abortcontroller
猜你喜欢

使用RestCloud ETL Shell组件实现定时调度DataX离线任务
![[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

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

Utilisation d'abortcontroller

【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
![[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction

【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍

Function: crypto JS encryption and decryption

Inaccurate data accuracy in ETL process

10 minutes to understand bim+gis fusion, common BIM data formats and characteristics
随机推荐
Restcloud ETL extracting dynamic library table data
评价——模糊综合评价
Lexin AWS IOT expresslink module achieves universal availability
PCIe Capabilities List
Evaluate:huggingface评价指标模块入门详细介绍
ETL过程中数据精度不准确问题
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
【ceph】cephfs的锁 笔记
Analysis of ble packet capturing debugging information
selenium将元素保存为图片
JS events
Summary of students' learning career (2022)
SAP GUI 770 Download
Smoothing data using convolution
PHP file upload 00 truncation
IDEA本地代理后,无法下载插件
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
Comparative analysis of restcloud ETL and kettle
shell脚本多进程并发写法实例(高阶修炼)