Go provides a light and smart goroutines management. Light because the goroutine stack starts at 2Kb only, and smart since goroutines can grow / shrink automatically according to our needs.
Regarding the size of the stack, we can find it in runtime/stack.go
:
1 | // The minimum size of stack used by Go code |
1 | func main() { |
1 | go build -gcflags -S main.go |
There are two instructions that involves the stack changes:
CALL runtime.morestack_noctxt
: this method will increase the size of the stack if it needs more.NOSPLIT
: this instruction means that the stack overflow check is not needed. It is similar to the compiler directive//go:nosplit
.
1 | runtime: newstack sp=0xc00002e6d8 stack=[0xc00002e000, 0xc00002e800] |
The strategy to copy the stack into a bigger space is called contiguous stack as opposed to segmented stack
类似于普通操作系统的segment stacks管理?
rust部分,不采用这种方式,
- stack thrashing的问题,需要事先分配
- FFI,外部code往往都需要large stacks
- LLVM libc optimizations and intrinsic fallbacks
Instead of segmented stacks we’re going to rely on the OS and MMU to help us map pages lazily