Skip to content
Ajay Chavda

Go's growable stacks vs. Rust's Box::pin

· 2 min

I have been building on Golang and ofcourse came across the approach in goroutines which made me chuckle.

Explaining simplistically - In Go, every goroutine starts with a stack of about 2 KB, and that stack can move. Each function begins with a tiny check: "is there enough stack left for me?" If not, the runtime allocates a new stack twice the size, copies the whole thing over, and fixes up every pointer that pointed into the old stack, while your code is in the middle of running.

   func depth(n int) int { if n == 0 { return 0 }; return 1 + depth(n-1) }
   done := make(chan int)
   go func() { done <- depth(1_000_000) }() // fine, the goroutine's stack just grows
   fmt.Println(<-done)

What is more nifty is that your function never notices that its local variables now sit at different memory addresses. I am still trying to figure out the nitty gritty of how it all works inside the compiler. The garbage collector also shrinks the stack when the goroutine stops needing the space...

This means you can start 1 million goroutines on your laptop and keep going until it hits 1 GB default limit.

If you have ever programmed in C - of course there is no built-in concurrency, no runtime and no async.

I am learning that Rust took the opposite route. OS threads get fixed stacks (usually 2 to 8 MB) that never move, because Rust lets you hold raw references to stack data anywhere, and moving the stack would break them. The compiler will go write the function into a state machine enum and will compute the size at compile time. That's why recursive async functions in Rust need Box::pin: the state machine would have to contain itself and be infinitely large.

   async fn depth(n: u64) -> u64 {
       if n == 0 { 0 } else { 1 + Box::pin(depth(n - 1)).await } // no Box::pin = compile error
   }   // depth(1_000_000).await -> "thread 'main' has overflowed its stack"

It's also why Rust has "function coloring," where async and sync functions are different. Go has neither problem. Any function can block, recurse, or call anything, because the runtime handles memory on the go (pun intended).

Go moves your stack. Rust makes you Box::pin and still overflows.