当前位置:网站首页>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
边栏推荐
- Inaccurate data accuracy in ETL process
- 学习内存屏障
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- 【leetcode】331. 验证二叉树的前序序列化
- Is it safe to open an account for mobile stock registration? Is there any risk?
- 效率超级加倍!pycharm十个小技巧就是这么神
- 买股票通过券商经理的开户二维码开户资金是否安全?想开户炒股
- 为什么图像分割任务中经常用到编码器和解码器结构?
- HW安全响应
- Summary of data interface API used in word search and translation applications
猜你喜欢
Advanced operation of MySQL database basic SQL statement tutorial

Ansible自动化的运用

Don't remove custom line breaks on reformat
![[graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet](/img/c8/f3f31a8e53c5918abc719603811cc7.png)
[graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet

Redis cluster

SQLite loads CSV files and performs data analysis

【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍

数据库-完整性约束

IDEA本地代理后,无法下载插件

Analysis of ble packet capturing debugging information
随机推荐
Evaluate:huggingface评价指标模块入门详细介绍
Use of abortcontroller
Unity C # e-learning (IX) -- wwwfrom
Learning memory barrier
English语法_形容词/副词3级 - 原级句型
vsomeip3 双机通信文件配置
【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
10 minutes to understand bim+gis fusion, common BIM data formats and characteristics
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
PHP file upload 00 truncation
JS handwritten bind, apply, call
Why are encoder and decoder structures often used in image segmentation tasks?
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
【leetcode】701. Insert operation in binary search tree
[tcapulusdb knowledge base] Introduction to tcapulusdb system management
SAP GUI 770 Download
JS之事件
评价——TOPSIS