Reduce Scheduling Contention

Learn how to reduce scheduling contention.

Overview

Goroutines are relatively cheap, and in some cases it makes sense to create many of them. For example, if the goroutines wait a long time on IO or network response, then the scheduler can execute other goroutines while these are waiting, increasing the efficiency of the program. In our case, the goroutines are mostly CPU-bound, so creating many of them doesn’t improve the efficiency. Instead, creating too many causes scheduling contention. Let’s address this issue by modifying our program to use worker queues. Instead of creating one goroutine per file, we’ll create one goroutine per available CPU. These will be our workers. Another goroutine sends the jobs to be executed by the workers. When no more jobs exist, the workers are done and the program finishes.

We start by adding another package to the import list. The package runtime contains several functions that deal with the Go runtime. We’ll use this package to determine the number of available CPUs:

Get hands-on with 1200+ tech skills courses.