当前位置:网站首页>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. 扩容会涉及数组拷贝,产生额外性能开销。
边栏推荐
猜你喜欢
随机推荐
Kubernetes:(七)优化大法(江湖失传已久的武林秘籍)
navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题
Nacos配置中心工作原理(超简单)
sql2008数据库置疑的解决方法_sqlserver2008数据库可疑
Summary of CNN classic models [easy to understand]
安装TimeGen波形绘图软件
Navicat premium download and install 15 detailed tutorial
executeScript异步执行的时候没有返回值的原因
融云「 IM 进阶实战高手课」系列直播上线
MYSQL下载及安装完整教程
Numpy那些事
常用软件静默安装参数
ActiveMQ漫谈(一)
分类实验报告作业
图像质量评价指标
[300+ selected big factory interview questions continue to share] Big data operation and maintenance sharp knife interview questions column (10)
QACTION_QA百科
MySQL常见函数
接入网学习笔记
编写一个油猴脚本