Go's M:N Scheduler: Stack Efficiency

Go's runtime employs an M:N scheduler to multiplex numerous lightweight goroutines onto a smaller pool of OS threads, enabling their compact 2KB initial stack size and efficient user-space management.

The top 3

  1. Go's Dynamic Stack Growth: Copying vs. Segmented: Go goroutines employ a 'stack copying' mechanism for dynamic resizing, starting with a minimal 2KB stack and allocating larger ones (typically 2x) when needed. This approach superseded older 'segmented stacks' to avoid performance penalties like the 'hot split problem', making stack shrinking 'free' and growth more efficient.
  2. Goroutine Memory Efficiency: Millions of Concurrent Tasks: Go's goroutines start with a minimal 2KB stack, a stark contrast to traditional OS threads that often reserve 1MB or more. This lightweight memory footprint allows Go to efficiently support hundreds of thousands to millions of concurrent operations, significantly reducing memory overhead compared to OS-level threading.
  3. M:N Scheduler's Context Switch: Nanoseconds vs. Microseconds: Go's M:N scheduler manages goroutines in user space, resulting in context switching times significantly faster than traditional OS threads. Goroutine switches take approximately 100-200 nanoseconds, compared to 1-10 microseconds for OS thread switches, which require kernel intervention and saving a larger register set. This speed difference dramatically improves throughput.

Sources

Open the full topic