Go's User-Space Scheduler: 80% Faster Context Switches

Go's runtime manages goroutine scheduling in user space, enabling significantly faster context switches (estimated to be 80-90% quicker) compared to C++ threads. C++ threads rely on the operating system's kernel for scheduling, which involves more expensive kernel-level context switching operations and higher overhead.

The top 3

  1. Kernel Trap vs. User-Space Freedom: The Speed Edge: Go's user-space scheduler avoids costly kernel mode transitions and system calls, which are inherent to OS thread context switches, enabling significantly faster goroutine switching (around 10x faster) entirely within the Go runtime.
  2. M:N Scheduling: Go's Efficient Resource Multiplexing: Go's M:N scheduler effectively maps many lightweight goroutines (M) onto a smaller, fixed number of OS threads (N), typically equal to the number of CPU cores. This model ensures optimal CPU utilization by rapidly switching between goroutines without involving the operating system's scheduler.
  3. Goroutine Stack Size: Why Less Memory Means Faster Switching: Goroutines start with tiny stack sizes, typically around 2-4KB, which dynamically grow and shrink as needed. This is significantly smaller than the fixed 1-8MB stacks of traditional OS threads, leading to reduced memory footprint per concurrent unit and faster context saving and restoring during switches.

Sources

Open the full topic