gpt4 book ai didi

go - Go的堆栈分割还是堆栈复制?

转载 作者:行者123 更新时间:2023-12-03 08:21:49 24 4
gpt4 key购买 nike

我对 Go 的堆栈管理感兴趣。我搜索了各种数据,但这让我很困惑,因为所有数据都是不同的。我很好奇的是golang的栈管理是栈分割还是栈复制。

正确答案是什么?

golang版本:go1.16.3

最佳答案

堆栈拆分与堆栈复制与其说是一种语言功能,不如说是 language spec没有提及任何一种策略,因为它是语言实现中的设计选择,其中有多种。因此,正如Flimzy在his comment中指出的那样,您应该指定您在问题中引用的实现。


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

Stacks are now contiguous, reallocated when necessary rather thanlinking on new "segments"; this release therefore eliminates thenotorious "hot stack split" problem.

Brad Fitzpatrick (Go 团队前成员)在 Gophercon India 2016 中解释了 Go 编译器中堆栈拆分导致的一些问题(地址 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解释说它在this 2016 thread中使用堆栈分割 ; Keith Randall 还在 another thread 中提供了有关此设计选择的一些见解。 .

关于go - Go的堆栈分割还是堆栈复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67659524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com