go 变量逃逸分析


0. 前言

小白学标准库之 reflect 篇中介绍了反射的三大法则以及变量的逃逸分析。对于逃逸分析的介绍不多,大部分都是引自 Go 逃逸分析。不过后来看反射源码的过程中发现有一种情况 Go 逃逸分析 没讲透。且当时没从底层汇编的角度去看,导致有种似懂非懂的感觉。这里就变量逃逸内容进行介绍。

1. 逃逸分析案例

这里的案例不同于 Go 逃逸分析,当然所属情况是其中概括的几种类型。

1.1 全局变量和局部变量

示例代码:

var a int

func main() {
	x := 10
	a = x
	println(a, x)
}

代码非常简单,定义全局变量 a 和局部变量 x,然后调用 println 打印 a 和 x。

使用 go tool compile 查看编译情况,注意变量逃逸在编译阶段,而不是运行时确定的。所以这里用 go tool compile 是能确定变量逃逸情况的:

// -m 查看变量逃逸情况
$ go tool compile -m escape.go 
escape.go:5:6: can inline main
escape.go:39:6: can inline escapes
escape.go:39:14: leaking param: x

// 使用 -l 关闭函数内联
$ go tool compile -m -l escape.go 
escape.go:39:14: leaking param: x

打印 leaking param: x 表明 x 代码中并未对 x 做任何引用操作,x 是一个泄露参数。不过,对于变量逃逸分析不影响,从结果来看,全局变量和局部变量都是在栈上分配的。

进一步思考,为什么全局变量会在栈上分配呢?
因为对全局变量赋值是传值的,传值就意味着这个值不是原有值,是值的拷贝。所以原有值不需要逃逸到堆上,只需要在栈上做变量拷贝就行。

改写示例代码如下:

var a *int

func main() {
	x := 10
	a = &x
	println(a, x)
}

将全局变量改为全局指针类型变量,指针指向局部变量 x。查看变量逃逸情况:

$ go tool compile -l -m escape.go 
escape.go:7:2: moved to heap: x

可以看到,变量 x 被 moved 到堆中。不难理解,全局变量指向 x,如果 x 不移到堆中,当 x 释放时,其它函数通过全局变量 a 找不到 x 了。事实上这是 c/c++ 语言会出现的情况。

通过汇编代码也能验证这点:

$ go tool compile -N -S -l escape.go 
...
CALL    runtime.newobject(SB)

继续改写上述代码:

func main() {
	x := 10
	a := &x
	println(a, x)
}

这里用一个局部指针类型变量 a 指向 x,查看变量分配情况:

$ go tool compile -l -m escape.go

可以看到,变量 x 和 a 都是在栈上分配的。编译器检查到 a 是个指针类型变量并不会被外部作用域引用,可以将 x 放在栈上分配。

1.2 interface{} 型变量逃逸

go 接口学习笔记 中介绍了接口类型的表示。

对于 interface{} 类型的运行时表示为 runtime.eface:

type eface struct {
	_type *_type
	data  unsafe.Pointer
}

这是空接口的运行时表示,对于编译阶段用于反射的空接口表示是 reflect.emptyInterface:

type emptyInterface struct {
	typ  *rtype
	word unsafe.Pointer
}

知道了 interface{} 的反射表示,我们看示例代码:

func main() {
	var a int = 10
	var ai interface{} = a
	println(ai)
}

逃逸分析:

$ go tool compile -l -m escape.go

改写示例代码:

func main() {
	var a int = 10
	var ai interface{} = &a
	println(ai)
}

逃逸分析:

$ go tool compile -l -m escape.go

可以看到,对于局部变量 interface{} 类型变量转换,不管是赋值还是赴地址都没有变量逃逸。这里发生了什么其实和上一节的局部变量一样,就不过多分析了。

值得提的一点是,给 interface{} 传地址,结构体的 word 将指向地址,而给 interface{} 传值,结构体的 word 是一个指针,将指向值所在的内存地址。这里由于是局部变量,这个变量值 a 是在栈上分配的,结构体 word 指向的是栈上值所在的地址。

再改写示例代码 1:

var ai interface{}

func main() {
	var a int = 10
	ai = a
	println(ai)
}

逃逸分析:

$ go tool compile -l -m escape.go 
escape.go:9:5: a escapes to heap

示例代码 2:

var ai interface{}

func main() {
	var a int = 10
	ai = &a
	println(ai)
}

逃逸分析:

$ go tool compile -l -m escape.go 
escape.go:8:6: moved to heap: a

可以看到,对于全局变量 ai 不管是传值还是传地址,变量 a 都将逃逸到堆中。为什么会这样也好理解:interface{} 反射的结构体表示是指针 data: unsafe.Pointer

通过汇编代码看传值的例子:

$ go tool compile -N -S -l escape.go 
CALL    runtime.convT64(SB)

重点看 runtime.convT64 函数,该函数会在堆上分配内存。详细看这里,不在展开了。

1.3 反射

示例代码如下:

func main() {
	var a int = 10
	var ai interface{} = a
	fmt.Println(ai)
}

逃逸分析:

$ go tool compile -l -m escape.go 
escape.go:11:13: ... argument does not escape
escape.go:11:13: a escapes to heap

这里代码除了 fmt.Println 改动基本和 1.2 节代码一样,为什么这时候 a 就逃到堆上了呢?

原因肯定在于 fmt.Println 函数,查看函数我们发现代码会走到 escapes(i) 这里,escapes 的函数实现是:

// Dummy annotation marking that the value x escapes,
// for use in cases where the reflect code is so clever that
// the compiler cannot follow.
func escapes(x interface{}) {
	if dummy.b {
		dummy.x = x
	}
}

var dummy struct {
	b bool
	x interface{}
}

再解释这段实现之前,先看下为什么要用 escapes(i) 函数:

// TODO: Maybe allow contents of a Value to live on the stack.
// For now we make the contents always escape to the heap. It
// makes life easier in a few places (see chanrecv/mapassign
// comment below).

comment:
Note: some of the noescape annotations below are technically a lie, 
but safe in the context of this package. Functions like chansend 
and mapassign don't escape the referent, but may escape anything 
the referent points to (they do shallow copies of the referent).
It is safe in this package because the referent may only point 
to something a Value may point to, and that is always in the 
heap (due to the escapes() call in ValueOf).

说白了,不用 escapes() 会让编译器很麻烦,这里涉及到 noescape,详细了解可看这里

escapes 实际上是一种欺骗行为,欺骗编译器使得编译器将变量逃逸到堆中。怎么欺骗的呢?其实和结合上两节分析,基本能看出来了。

在变量 escapes(i) 到 escapes(x interface{}) 时发生了类型转换,将 i 转换为 interface{} 类型,实际做的就是 1.2 节描述的行为。然后,重点在 dummy.x == x,全局变量 dummy.x 会引用转换的接口 x,由于 dummy.x 是一个 interface{} 类型,其实质是一个指针,所以编译器会将 interface x 中 data 指向的变量 i 分配到堆中。这里注意 i 可以是值也可以是地址,如果是地址,编译器会将地址指向的值分配到堆中。

可能描述起来较为复杂,复杂的原因是 interface{} 做了好几层包装。我们拆开包装,用一种简化方式看代码的欺骗行为:

var a *int

func main() {
	x := 10

	var f bool
	if f {
		a = &x
	}
}

逃逸分析:

$ go tool compile -l -m escape.go 
escape.go:33:2: moved to heap: x

可以看到,骗过了编译器使得变量 x 逃逸到了堆上,虽然 a = &x 不会执行。

1.4 总结

本篇文章通过几个逃逸分析案例重点分析 escapes 函数是如果做到欺骗编译器实现变量逃逸的。


posted @ 2022-03-31 15:22  lubanseven  阅读(289)  评论(0编辑  收藏  举报