跟煎鱼学 Go
  • Introduction
  • 第1课 杂谈
    • 1.1 聊一聊,Go 的相对路径问题
    • 1.2 Go 的 fake-useragent 了解一下
    • 1.3 用 Go 来了解一下 Redis 通讯协议
    • 1.4 使用 Gomock 进行单元测试
    • 1.5 在 Go 中恰到好处的内存对齐
    • 1.6 来,控制一下 goroutine 的并发数量
    • 1.7 for-loop 与 json.Unmarshal 性能分析概要
    • 1.8 简单围观一下有趣的 //go: 指令
    • 1.9 我要在栈上。不,你应该在堆上
    • 1.10 defer 会有性能损耗,尽量不要用
    • 1.11 从实践到原理,带你参透 gRPC
    • 1.12 Go1.13 defer 的性能是如何提高的?
    • 1.13 Go 应用内存占用太多,让排查?(VSZ篇)
    • 1.14 干货满满的 Go Modules 和 goproxy.cn
  • 第2课 包管理
    • 2.1 Go依赖管理工具dep
    • 2.2 如此,用dep获取私有库
  • 第3课 gin
    • 3.1 Golang 介绍与环境安装
    • 3.2 Gin搭建Blog API's (一)
    • 3.3 Gin搭建Blog API's (二)
    • 3.4 Gin搭建Blog API's (三)
    • 3.5 使用JWT进行身份校验
    • 3.6 编写一个简单的文件日志
    • 3.7 优雅的重启服务
    • 3.8 为它加上Swagger
    • 3.9 将Golang应用部署到Docker
    • 3.10 定制 GORM Callbacks
    • 3.11 Cron定时任务
    • 3.12 优化配置结构及实现图片上传
    • 3.13 优化你的应用结构和实现Redis缓存
    • 3.14 实现导出、导入 Excel
    • 3.15 生成二维码、合并海报
    • 3.16 在图片上绘制文字
    • 3.17 用Nginx部署Go应用
    • 3.18 Golang交叉编译
    • 3.19 请入门 Makefile
  • 第4课 grpc
    • 4.1 gRPC及相关介绍
    • 4.2 gRPC Client and Server
    • 4.3 gRPC Streaming, Client and Server
    • 4.4 TLS 证书认证
    • 4.5 基于 CA 的 TLS 证书认证
    • 4.6 Unary and Stream interceptor
    • 4.7 让你的服务同时提供 HTTP 接口
    • 4.8 对 RPC 方法做自定义认证
    • 4.9 gRPC Deadlines
    • 4.10 分布式链路追踪
  • 第5课 grpc-gateway
    • 5.1 介绍与环境安装
    • 5.2 Hello World
    • 5.3 Swagger了解一下
    • 5.4 能不能不用证书?
  • 第6课 常用关键字
    • 6.1 panic and recover
    • 6.2 defer
  • 第7课 数据结构
    • 7.1 slice
    • 7.2 slice:最大容量大小是怎么来的
    • 7.3 map:初始化和访问元素
    • 7.4 map:赋值和扩容迁移
    • 7.5 map:为什么遍历 map 是无序的
  • 第8课 标准库
    • 8.1 fmt
    • 8.2 log
    • 8.3 unsafe
  • 第9课 工具
    • 9.1 Go 大杀器之性能剖析 PProf
    • 9.2 Go 大杀器之跟踪剖析 trace
    • 9.3 用 GODEBUG 看调度跟踪
    • 9.4 用 GODEBUG 看GC
  • 第10课 爬虫
    • 9.1 爬取豆瓣电影 Top250
    • 9.2 爬取汽车之家 二手车产品库
    • 9.3 了解一下Golang的市场行情
Powered by GitBook
On this page
  • 前言
  • maxSliceCap
  • maxElems
  • ^uintptr(0)
  • uintptr 是什么
  • ^uintptr 做了什么事
  • maxAlloc
  • maxAlloc / typeSize
  • 总结
  • 最后

Was this helpful?

  1. 第7课 数据结构

7.2 slice:最大容量大小是怎么来的

Previous7.1 sliceNext7.3 map:初始化和访问元素

Last updated 5 years ago

Was this helpful?

image

前言

在《深入理解 Go Slice》中,我们提到了 “根据其类型大小去获取能够申请的最大容量大小” 的处理逻辑。今天我们将更深入地去探究一下,底层到底做了什么东西,涉及什么知识点?

Go Slice 对应代码如下:

func makeslice(et *_type, len, cap int) slice {
    maxElements := maxSliceCap(et.size)
    if len < 0 || uintptr(len) > maxElements {
        ...
    }

    if cap < len || uintptr(cap) > maxElements {
        ...
    }

    p := mallocgc(et.size*uintptr(cap), et, true)
    return slice{p, len, cap}
}

根据想要追寻的逻辑,定位到了 maxSliceCap 方法,它会根据当前类型的大小获取到了所允许的最大容量大小来进行阈值判断,也就是安全检查。这是浅层的了解,我们继续追下去看看还做了些什么?

maxSliceCap

func maxSliceCap(elemsize uintptr) uintptr {
    if elemsize < uintptr(len(maxElems)) {
        return maxElems[elemsize]
    }
    return maxAlloc / elemsize
}

maxElems

var maxElems = [...]uintptr{
    ^uintptr(0),
    maxAlloc / 1, maxAlloc / 2, maxAlloc / 3, maxAlloc / 4,
    maxAlloc / 5, maxAlloc / 6, maxAlloc / 7, maxAlloc / 8,
    maxAlloc / 9, maxAlloc / 10, maxAlloc / 11, maxAlloc / 12,
    maxAlloc / 13, maxAlloc / 14, maxAlloc / 15, maxAlloc / 16,
    maxAlloc / 17, maxAlloc / 18, maxAlloc / 19, maxAlloc / 20,
    maxAlloc / 21, maxAlloc / 22, maxAlloc / 23, maxAlloc / 24,
    maxAlloc / 25, maxAlloc / 26, maxAlloc / 27, maxAlloc / 28,
    maxAlloc / 29, maxAlloc / 30, maxAlloc / 31, maxAlloc / 32,
}

maxElems 是包含一些预定义的切片最大容量值的查找表,索引是切片元素的类型大小。而值看起来 “奇奇怪怪” 不大眼熟,都是些什么呢。主要是以下三个核心点:

  • ^uintptr(0)

  • maxAlloc

  • maxAlloc / typeSize

^uintptr(0)

func main() {
    log.Printf("uintptr: %v\n", uintptr(0))
    log.Printf("^uintptr: %v\n", ^uintptr(0))
}

输出结果:

2019/01/05 17:51:52 uintptr: 0
2019/01/05 17:51:52 ^uintptr: 18446744073709551615

我们留意一下输出结果,比较神奇。取反之后为什么是 18446744073709551615 呢?

uintptr 是什么

在分析之前,我们要知道 uintptr 的本质(真面目),也就是它的类型是什么,如下:

type uintptr uintptr

uintptr 的类型是自定义类型,接着找它的真面目,如下:

#ifdef _64BIT
typedef    uint64        uintptr;
#else
typedef    uint32        uintptr;
#endif

通过对以上代码的分析,可得出以下结论:

  • 在 32 位系统下,uintptr 为 uint32 类型,占用大小为 4 个字节

  • 在 64 位系统下,uintptr 为 uint64 类型,占用大小为 8 个字节

^uintptr 做了什么事

^ 位运算符的作用是按位异或,如下:

func main() {
    log.Println(^1)
    log.Println(^uint64(0))
}

输出结果:

2019/01/05 20:44:49 -2
2019/01/05 20:44:49 18446744073709551615

接下来我们分析一下,这两段代码都做了什么事情呢

^1

二进制:0001

按位取反:1110

该数为有符号整数,最高位为符号位。低三位为表示数值。按位取反后为 1110,根据先前的说明,最高位为 1,因此表示为 -。取反后 110 对应十进制 -2

^uint64(0)

二进制:0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

按位取反:1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111

该数为无符号整数,该位取反后得到十进制值为:18446744073709551615

这个值是不是看起来很眼熟呢?没错,就是 ^uintptr(0) 的值。也印证了其底层数据类型为 uint64 的事实 (本机为 64 位)。同时它又代表如下:

  • math.MaxUint64

  • 2 的 64 次方减 1

maxAlloc

const GoarchMips = 0
const GoarchMipsle = 0
const GoarchWasm = 0

...

_64bit = 1 << (^uintptr(0) >> 63) / 2

heapAddrBits = (_64bit*(1-sys.GoarchWasm))*48 + (1-_64bit+sys.GoarchWasm)*(32-(sys.GoarchMips+sys.GoarchMipsle))

maxAlloc = (1 << heapAddrBits) - (1-_64bit)*1

maxAlloc 是允许用户分配的最大虚拟内存空间。在 64 位,理论上可分配最大 1 << heapAddrBits 字节。在 32 位,最大可分配小于 1 << 32 字节

在本文,仅需了解它承载的是什么就好了。具体的在以后内存管理的文章再讲述

注:该变量在 go 10.1 为 _MaxMem,go 11.4 已改为 maxAlloc。相关的 heapAddrBits 计算方式也有所改变

maxAlloc / typeSize

我们再次回顾 maxSliceCap 的逻辑代码,这次重点放在控制逻辑,如下:

// func makeslice
maxElements := maxSliceCap(et.size)

...

// func maxSliceCap
if elemsize < uintptr(len(maxElems)) {
    return maxElems[elemsize]
}
return maxAlloc / elemsize

通过这段代码和 Slice 上下文逻辑,可得知在想得到该类型的最大容量大小时。会根据对应的类型大小去查找表查找索引(索引为类型大小,摆放顺序是有考虑原因的)。“迫不得已的情况下” 才会手动的计算它的值,最终计算得到的内存字节大小都为该类型大小的整数倍

查找表的设置,更像是一个优化逻辑。减少常用的计算开销 :)

总结

通过本文的分析,可得出 Slice 所允许申请的最大容量大小,与当前值类型和当前平台位数有直接关系

最后

短短的一句话其实蕴含着不少知识点,希望这篇文章恰恰好可以帮你解惑

注:本文 Go 代码基于版本 11.4

本文与一同属于的关联章节。如果你在阅读源码时,对这些片段有疑惑。记得想尽办法深究下去,搞懂它

《有点不安全却又一亮的 Go unsafe.Pointer》
《深入理解 Go Slice》