Context Switching
Context switching happens because the operating system does multitasking. It is also used in interrupt handling, because network and I/O operations are orders of magnitude slower than CPU operations.
On a switch, the current stack pointer is saved and a new stack pointer is loaded to execute the new code. Registers, flags, and data segments are pushed onto the old stack and popped off the new stack.
Context switching between threads is faster than between processes, because threads share virtual memory space, so there is no need to flush the TLB.
Scheduler defaults vs HFT
The default CPU task scheduler tries to maintain fairness in CPU resources across all runnable threads and processes, conserve energy, and maximize CPU throughput. For HFT we would rather not conserve energy, and we don’t care about fairness.
Minimizing context switches
Pin threads to CPU cores.
Avoid system calls that lead to pre-emption. Calls that block the thread cause a context switch followed by a kernel interrupt. Kernel bypass avoids network and I/O system calls altogether.