Context Switching and Performance: What Every Developer Should Know
Understand how context switching affects CPU registers, caches, TLB, and pipeline performance, and learn strategies to mitigate performance penalties
Context switching is known to be one of the most expensive operations performed by the operating system kernel which can kill the performance of many systems. It is a necessary evil on a busy system to keep it responsive, and to allow all the processes to make progress. But what makes it so expensive? This article decodes the hardware and software dynamics underlying context switching.
Contents
Components of a Process’ Context
Registers
Main Memory
The Process of Context Switching
Performance Cost of Context Switching
Performance Impact Due to TLB Contention
Performance Impact Due to Cache Contention
Performance Impact Due to CPU Pipeline Flush
Performance Impact Due to Branch Predictor State
Speculative Execution Vulnerabilities and their Impact on Context Switching
Final Thoughts
Summary Notes
References
Before discussing the cost of context switching, we should first talk about the components of a process’ context, so we have a clearer understanding when analyzing the impact of switching between contexts.
This article is available to the paid subscribers in early access mode. Paid subscriptions are necessary for me to continue to do this work and to provide high quality content. Please consider upgrading to support my work and to get early access to deep content like this.