当前位置:网站首页>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
边栏推荐
- Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
- BLE抓包调试信息分析
- Evaluation - TOPSIS
- Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
- [CEPH] Lock Notes of cephfs
- How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
- Comparative analysis of restcloud ETL and kettle
- /etc/profile、/etc/bashrc、~/. Bashrc differences
- One click GCC script installation
- 【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
猜你喜欢

RestCloud ETL解决shell脚本参数化

评价——模糊综合评价
![[CEPH] MKDIR | mksnap process source code analysis | lock state switching example](/img/4a/0aeb69ae6527c65a67be535828b48a.jpg)
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example

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

HR export data Excel VBA

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

Use of abortcontroller

Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)

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

Restcloud ETL extraction de données de table de base de données dynamique
随机推荐
[CEPH] cephfs internal implementation (I): Concept -- undigested
【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
数据库-序列
【TcaplusDB知识库】TcaplusDB常规单据介绍
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
selenium将元素保存为图片
【leetcode】112. 路径总和 - 113. 路径总和 II
php文件上传00截断
音视频学习(一)——PTZ控制原理
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
【小程序实战系列】小程序框架 页面注册 生命周期 介绍
ETL过程中数据精度不准确问题
小程序:uniapp解决 vendor.js 体积过大的问题
Ansible自动化的运用
Compile configuration in file
One click GCC script installation
Comparative analysis of restcloud ETL and kettle
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
查词翻译类应用使用数据接口api总结
Evaluation - TOPSIS