Kernel Switches: 10x Slower Than User-Space

Kernel context switches, typically taking 1-5 microseconds, are significantly slower—often by an order of magnitude or more—compared to lightweight user-space context switches, such as those for Goroutines (around 170 nanoseconds), highlighting the performance benefits of user-level threading models.

The top 3

  1. Kernel Switch Latency: Microseconds vs. Nanoseconds: Kernel context switches typically incur a latency ranging from 1 to 5 microseconds (µs), a stark contrast to highly optimized user-space mechanisms like BEAM processes, which can achieve switch times in tens of nanoseconds (e.g., 20ns), making them orders of magnitude faster.
  2. Hidden Costs: TLB Flushes & Cache Pollution: Beyond the direct costs of saving CPU registers and switching page tables, kernel context switches incur significant "indirect costs." These include Translation Lookaside Buffer (TLB) flushes, CPU cache pollution (L1-L3), CPU pipeline flushes, and branch predictor resets, which can collectively add substantial overhead and sometimes even exceed the direct costs.
  3. User-Space's Bypass: Avoiding Kernel Overhead: Lightweight user-space concurrency models, such as those used by the BEAM virtual machine (Erlang/Elixir), gain their speed advantage by completely bypassing the expensive transition to kernel mode. This eliminates the overheads associated with privilege level changes, complex OS scheduler involvement, and the virtual memory and cache invalidation penalties inherent in kernel-level context switches.

Sources

Open the full topic