golang 解析struct为map_惊!空 struct 地址竟然不相等

Go 语言里的空 struct{} 是一个特殊的结构,因为编译器优化的关系,会导致我们拿空 struct 指针做比较的时候出现一些意外的结果。之前有人提过相关的 issue,不过还是值得我们简单研究一番。

官方 spec 中对此的描述是:"Pointers to distinct zero-size variables may or may not be equal.",来看看实现上具体为什么会是这样的。

package mainimport "fmt"func main() {  a := new(struct{})  b := new(struct{})  println(a, b, a == b)  c := new(struct{})  d := new(struct{})  fmt.Println(c, d, c == d)}

结果可能让你很惊讶:

0xc00007af1f 0xc00007af1f false&{} &{} true

如果之前碰到过给程序加上 fmt.Println 就导致结果变化的情况的同学,可能会想到这里的原因,是和逃逸分析相关。(可以参考这两个 issue :8618 和 31317)

没错:

~/test git:master ❯❯❯ cat -n true.go                                                                              ✱ ◼     1	package main     2     3	import "fmt"     4     5	func main() {     6		a := new(struct{})     7		b := new(struct{})     8		println(a, b, a == b)     9    10		c := new(struct{})    11		d := new(struct{})    12		fmt.Println(c, d, c == d)    13	}~/test git:master ❯❯❯ go run -gcflags="-m" true.go# command-line-arguments./true.go:12:13: inlining call to fmt.Println./true.go:6:10: main new(struct {}) does not escape./true.go:7:10: main new(struct {}) does not escape./true.go:10:10: new(struct {}) escapes to heap./true.go:11:10: new(struct {}) escapes to heap./true.go:12:13: c escapes to heap./true.go:12:13: d escapes to heap./true.go:12:22: c == d escapes to heap./true.go:12:13: main []interface {} literal does not escape./true.go:12:13: io.Writer(os.Stdout) escapes to heap:1: (*File).close .this does not escape0xc00007af1f 0xc00007af1f false&{} &{} true

在 fmt.Println 中单独出现了 c,所以 c = new 的时候就发生了逃逸,d 也同理:

  • 未逃逸:a,b
  • 逃逸:c,d

看过一点 go runtime 代码的同学应该知道,在 runtime 里有这么个东西:

// base address for all 0-byte allocationsvar zerobase uintptr

0 大小的内存分配均会指向该变量地址,所谓的 0 字节,主要就是这种空 struct。不过这里没说清楚的是,只有在堆上分配的 0 大小的 struct 才会指向该地址:

package mainimport "fmt"var a = new(struct{}) // 堆上func main() {  var b = new(struct{})  fmt.Println(b/*这里单独打印了,所以 b escape 到堆上*/, a == b)}

那么空 struct 逃逸之后其实都是同一个变量,地址相等,我们是可以理解了。

没逃逸的 a 和 b 为什么地址不相等呢?这里可以祭出 SSA 大法:

~/test git:master ❯❯❯ sudo GOSSAFUNC=main go build true.go

92f6accb2719de9ce2e38f07e63ffc0f.png

可以看到这个 false,其实是在代码优化阶段(opt)直接被编译后端作为常量优化掉了,直接转成了 false。

所以我们本能地以为 == 是在做指针比较,但是常量优化会替我们做出不那么符合直觉的判断,直接把 a == b rewrite 为 false。

问题到这里也就结束了,既然我们知道这里是优化阶段做的,那是不是我们把优化关闭就会导致输出 true 呢?

确实是的:

~/test git:master ❯❯❯ cat true.gopackage mainimport "fmt"func main() {	a := new(struct{})	b := new(struct{})	println(a, b, a == b)	c := new(struct{})	d := new(struct{})	fmt.Println(c, d, c == d)}~/test git:master ❯❯❯ go run -gcflags="-N -l" true.go0xc00007aece 0xc00007aece true&{} &{} true

我们可以用 Go 附带的 SSA 工具去更深入地认识整个编译阶段干的事情,从而理解官方的所谓“实现细节”和 spec 上的 may or may not 到底是怎么回事。

只要知道怎么使用工具,结论反而也就没那么重要了。

[1] https://github.com/golang/go/issues/8618

[2] https://github.com/golang/go/issues/31317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值