Failures and antipatterns · Intermediate

Slowdowns caused by false sharing

Investigate coherence interference between independent fields using a controlled layout comparison and account for padding costs.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Independent variables can still interfere
  2. Two counters sharing a cache line
  3. Compare layouts under controlled conditions
  4. Padding also has a cost
  5. Ask Logeia about this articleCheck your understanding

Independent variables can still interfere

False sharing happens when CPUs operate on logically independent data that occupy the same cache line. Coherence mechanisms track the line rather than an application’s chosen variable boundaries. If at least one CPU writes, ownership transfers or invalidations can disturb another CPU accessing a different field. This differs from true sharing, where the same logical data is intentionally shared. Results can remain fully correct while throughput deteriorates, so a race detector cannot by itself establish that a data layout is free from this performance problem.

Two counters sharing a cache line

Imagine two eight-byte counters placed next to each other. CPU 0 updates only the first and CPU 1 updates only the second. If the machine uses 64-byte cache lines and both counters lie in one line, their updates can interact through coherence despite touching separate objects. Separating them may reduce that interaction. The line size is an assumption of this example, not a portable constant for every processor. Check actual alignment and placement; knowing only the declared field sizes is insufficient to establish which line contains them.

Compare layouts under controlled conditions

A useful comparison holds work volume, thread count, CPU affinity and compiler settings constant while changing only the arrangement of frequently written fields. On supported Linux systems, perf c2c can help locate cache lines and access sites involved in cache-to-cache contention. Event availability and permission restrictions matter. A general slowdown as threads increase could instead reflect bandwidth saturation, locking, NUMA placement or oversubscription. Combine line-level evidence with the controlled layout comparison rather than interpreting one aggregate hardware counter as a complete causal explanation of application latency.

Padding also has a cost

Possible remedies include keeping per-thread counters and combining them later, separating frequently written fields, or isolating update-heavy state from read-mostly data. Padding is a trade-off: larger structures consume more memory and may worsen cache or TLB behavior elsewhere. A layout change that helps one hot object need not help a rarely accessed structure. After identifying the specific line, compare both execution cost and memory footprint under representative workloads. Keep the original correctness invariants intact; changing the layout should not silently change how updates are synchronized or when readers see aggregates.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path