Go 1.19 stack size optimization

274 views
Skip to first unread message

Elad Gavra

unread,
Feb 2, 2023, 2:53:54 PM2/2/23
to golang-nuts
Hi,
I understand go uses an average metric to predict stack size required for new go routines. Does it have minimum size?
I was wandering if go optimizes "static stack size"? i.e. there are cases where the code is predictable and the compiler can analyze the stack size required for routines. In these cases the compiler can calculate the exact size that will be required by the routine (or its upper bound). The size could be even lower than the minimal (?) 2k.

Thanks.

Keith Randall

unread,
Feb 2, 2023, 8:35:47 PM2/2/23
to golang-nuts
Currently:
- All stack sizes are a power of 2.
- The minimum size is 2KB, hardcoded.
- We reserve ~800 bytes of goroutine stacks for the runtime.

So 1KB stacks are feasible, but just barely. I don't think you could fit much of a goroutine in 200 bytes of stack.

So smaller sizes are possible, but not really practical. We'd have to handle non-power-of-2 sizes, or shrink the runtime reservation, both of which might be a lot of work. (We've been working on the runtime reservation for years, and that number always seems to go in the wrong direction.)

A goroutine is both a stack and a goroutine descriptor. Those descriptors themselves are 392 bytes, so even if the stack was super tiny we still have at least that memory overhead per goroutine.
Reply all
Reply to author
Forward
0 new messages