Go 的栈分裂还是栈复制?

Is Go's stack split or stack copy?

我对 Go 的堆栈管理很感兴趣。我搜索了各种数据,但它让我感到困惑,因为所有数据都不同。我比较好奇的是golang的栈管理到底是stack splitting还是stack copying

正确答案是什么?

golang版本:go1.16.3

堆栈拆分与堆栈复制与其说是一种语言功能 - language spec makes no mention of either strategy—as it is a design choice in implementations of the language, of which there are several. Therefore, as pointed out by Flimzy in ,您应该在您的问题中指定您指的是哪个实现。


Go 1.4 起,“规范”Go 编译器(称为 gc)不再拆分堆栈,现在使用 堆栈复制:

Stacks are now contiguous, reallocated when necessary rather than linking on new "segments"; this release therefore eliminates the notorious "hot stack split" problem.

Brad Fitzpatrick (former member of the Go team) explains some of the problems that stack splitting caused in the Go compiler in his Gophercon India 2016 (at mark 12'50''):

[...] in Go, you have goroutines, which is like a really, really lightweight thread that has a stack that's small and it grows as necessary. The way it used to work is little goroutines with little stacks and, when you ran out of stack space, you would make another stack somewhere else and you would be jumping between these stacks as you called functions and returned. Which was great most of the time until it wasn't, until you were in a tight loop, in something like a JPEG decoder or something, and you were bouncing between stacks and you had really big performance penalties that we're surprising. And then you would move some code somewhere else and your performance characteristics would change a lot.


至于gccgo(Go语言的替代实现),Ian Lance Taylor解释说它在[=中使用了stack splitting 15=].