chan struct{} 相关的特殊优化问题
来源:1-9 神奇的内置数据结构
SilverXia
2021-05-26 22:58:45
上课原问题:
相关代码:
https://github.com/golang/go/blob/bc9c580409b61af6b29f0cbd9d45bec63dbe2ccb/src/runtime/chan.go#L93-L97
case mem == 0:
// Queue or element size is zero.
c = (*hchan)(mallocgc(hchanSize, nil, true))
// Race detector uses this location for synchronization.
c.buf = c.raceaddr()switch {
和不执行handoff
相关代码:
https://github.com/golang/go/blob/bc9c580409b61af6b29f0cbd9d45bec63dbe2ccb/src/runtime/chan.go#L295-L298
if sg.elem != nil {
sendDirect(c.elemtype, sg, ep)
sg.elem = nil
}
但是所谓的不执行handoff依赖的是在recv的时候忽略收到的值(<-ch),而不是对chan struct{}的特殊优化?
1回答
你这个文档是在哪里看到的呀
我实际调试了一下,chan 元素是 struct{} 的时候,sendDirect 还是会进去的,只是在 memmove 的时候,t.size == 0
相似问题
回答 1
回答 1