User-Space Scheduling: Goroutines Bypass Kernel Overhead
Goroutines are scheduled in user space by the Go runtime, completely bypassing the costly kernel-level transitions and extensive state saving required for OS thread context switches, significantly reducing overhead.
The top 3
- Goroutines vs. OS Threads: Context Switch Speed Showdown: Goroutine context switches are significantly faster, typically around 50-200 nanoseconds, compared to OS thread context switches which can take 1-2 microseconds or more, making them 10x to 70x quicker due to avoiding kernel mode transitions and saving less state.
- Goroutines' Memory Edge: KBs vs. MBs: Goroutines typically start with stack sizes as small as 2-4KB, dynamically growing, vastly more memory efficient than OS threads which often allocate 1-2MB.
- Go's M:N Scheduler: Maximize CPU via Work-Stealing: Go's runtime uses an M:N scheduler, multiplexing millions of Goroutines (M) onto a smaller number of OS threads (N), employing work-stealing to keep CPUs busy.
Sources
Open the full topic