当前位置:网站首页>golang源码分析(10)slice
golang源码分析(10)slice
2022-08-02 16:48:00 【用户9710217】
slice 结构定义
type slice struct {
array unsafe.Pointer
len int
cap int
}
创建slice
// maxSliceCap returns the maximum capacity for a slice.
func maxSliceCap(elemsize uintptr) uintptr {
if elemsize < uintptr(len(maxElems)) {
return maxElems[elemsize]
}
return _MaxMem / elemsize
}
func makeslice(et *_type, len, cap int) slice {
// NOTE: The len > maxElements check here is not strictly necessary,
// but it produces a 'len out of range' error instead of a 'cap out of range' error
// when someone does make([]T, bignumber). 'cap out of range' is true too,
// but since the cap is only being supplied implicitly, saying len is clearer.
// See issue 4085.
// 计算最大可分配长度
maxElements := maxSliceCap(et.size)
if len < 0 || uintptr(len) > maxElements {
panic(errorString("makeslice: len out of range"))
}
if cap < len || uintptr(cap) > maxElements {
panic(errorString("makeslice: cap out of range"))
}
// 分配连续区间
p := mallocgc(et.size*uintptr(cap), et, true)
return slice{p, len, cap}
}
slice 扩容
// cap 目标容量
func growslice(et *_type, old slice, cap int) slice {
if et.size == 0 {
if cap < old.cap {
panic(errorString("growslice: cap out of range"))
}
// append should not create a slice with nil pointer but non-zero len.
// We assume that append doesn't need to preserve old.array in this case.
return slice{unsafe.Pointer(&zerobase), old.len, cap}
}
newcap := old.cap
doublecap := newcap + newcap
if cap > doublecap {
newcap = cap
} else {
// 小于1024,*2扩容
if old.len < 1024 {
newcap = doublecap
} else {
// 大于1024,*1.25
for newcap < cap {
newcap += newcap / 4
}
}
}
var lenmem, newlenmem, capmem uintptr
const ptrSize = unsafe.Sizeof((*byte)(nil))
switch et.size {
case 1:
lenmem = uintptr(old.len)
newlenmem = uintptr(cap)
capmem = roundupsize(uintptr(newcap))
newcap = int(capmem)
case ptrSize:
lenmem = uintptr(old.len) * ptrSize
newlenmem = uintptr(cap) * ptrSize
capmem = roundupsize(uintptr(newcap) * ptrSize)
newcap = int(capmem / ptrSize)
default:
lenmem = uintptr(old.len) * et.size
newlenmem = uintptr(cap) * et.size
capmem = roundupsize(uintptr(newcap) * et.size)
newcap = int(capmem / et.size)
}
if cap < old.cap || uintptr(newcap) > maxSliceCap(et.size) {
panic(errorString("growslice: cap out of range"))
}
var p unsafe.Pointer
if et.kind&kindNoPointers != 0 {
p = mallocgc(capmem, nil, false)
memmove(p, old.array, lenmem)
// The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
// Only clear the part that will not be overwritten.
memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
} else {
// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
p = mallocgc(capmem, et, true)
if !writeBarrier.enabled {
memmove(p, old.array, lenmem)
} else {
for i := uintptr(0); i < lenmem; i += et.size {
typedmemmove(et, add(p, i), add(old.array, i))
}
}
}
// 新slice
return slice{p, old.len, newcap}
}
扩容总结:
1. 小于1024,每次扩容*2
2. 大于1024,每次扩容*1.25
3. 扩容会涉及数组拷贝,产生额外性能开销。
边栏推荐
- 开始使用 NVIDIA Jetson Orin 上的深度学习加速器
- JS数组删除其中一个元素
- 莱斯大学胡侠团队 ICML 2022 杰出论文: 新型图数据增强方法 G-Mixup|附作者对话
- 电烙铁的基础知识
- MySQL常用的日期时间函数
- 小心 transmittable-thread-local 的这个坑
- nacos简单使用
- NIO Cup 2022 Niu Ke Summer Multi-School Training Camp 5 ABCDFGHK
- Timestamp formatting "recommended collection"
- Oracle 11g rac打完补丁,dbca新建数据库还需要执行应用补丁的sql吗?
猜你喜欢
[C Language Brush Questions] Three Questions for Getting Started with Pointers | String Length, String Copy, Two Number Swap
数字孪生园区场景中的坐标知识
小程序实现手写左右翻页和动态修改横向滚动条位置
Antd-ProComponents中的EditableProTable无法在子行继续新增子子行的临时解决方案
低光数据集
实时数仓架构演进及选型
julia系列6:并行计算
Switch 块、Switch 表达式、Switch 模式匹配,越来越好用的 Switch
Real-time data warehouse architecture evolution and selection
nacos集群配置详解
随机推荐
周末看点回顾|亚马逊将于2023年底关闭Amazon Drive网盘服务;千寻位置发布时空智能六大底层自研技术…
[C Language Brush Questions] Three Questions for Getting Started with Pointers | String Length, String Copy, Two Number Swap
蔚来杯2022牛客暑期多校训练营5 ABCDFGHK
融云「 IM 进阶实战高手课」系列直播上线
Nacos面试题
MYSQL一站式学习,看完即学完
Summary of CNN classic models [easy to understand]
Inconsistency between oracle and mysql statement results
红蓝对抗经验分享:CS免杀姿势
Flink SQL builds real-time data warehouse DWD layer
jar包应用的简单启停脚本
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
Numpy那些事
In the idea to create a web project _idea deployment of the web project
js商品总价格、最高价格商品、排除重复商品[初版]
QACTION_QA百科
锁定和并发控制(四)
NAACL 2022 | 具有元重加权的鲁棒自增强命名实体识别技术
一些与开发者体验有关的话题
尚硅谷尚品项目汇笔记(三)