Mechanical sympathy is the practice of writing software that works with hardware rather than against it. Four core principles are covered: predictable sequential memory access (which leverages CPU prefetching), cache line awareness and false sharing (padding variables to avoid contention between threads), the single-writer principle (dedicating one thread per resource to eliminate mutex overhead and enable batching), and natural batching (greedily building batches as data arrives rather than waiting for fixed timeouts or sizes). A practical example of an AI text embedding service illustrates how these principles combine to eliminate head-of-line blocking and improve throughput. The article also emphasizes measuring performance before optimizing.
Table of contents
Not-So-Random Memory AccessCache Lines and False SharingThe Single Writer PrincipleNatural BatchingNatural vs. Smart BatchingSort: