What makes Goroutines highly efficient? ⚡
Goroutines are not OS-level threads but are threads that are managed by the Go runtime as user-level threads. These user-level threads (goroutines) are then scheduled on OS-level threads and then executed.
OS-level threads have a stack size of 2MB while Goroutine starts with a stack size of 2KB, this means it is very lightweight for Go runtime to create multiple goroutines efficiently allowing a large number of goroutines to exist concurrently.
Apart from minimal stack size, Go’s runtime
- is asynchronous in nature and does not block on I/O.
- schedules goroutines such that it maximizes the business of CPU cores.
- passes messages to coordinate goroutines, instead of using shared memory; this reduces lock contention and improves throughput.
⚡ I keep writing and sharing these engineering nuggets, so if you are keen on learning them, follow along.
youtube.com/c/ArpitBhayani