Cache Coherence & False Sharing
What You'll Learn
- What cache coherence is
- How cache line bouncing works
- The false sharing problem
- Why padding helps
Mental Model
In multi-core systems, each core has its own cache. When multiple cores access the same memory location, their caches must stay synchronized. This is cache coherence.
Cache Line Bouncing
Caches operate on cache lines (64 bytes), not individual bytes. If two cores modify different variables that happen to be on the same cache line, the cache line "bounces" between cores, causing expensive coherence traffic.
False Sharing Concept
False sharing occurs when threads access different memory locations that share the same cache line. Even though they're not actually sharing data, the cache coherence protocol treats them as if they are, causing performance degradation.
Why Padding Helps
By padding data structures so that each thread's data is on a different cache line, you eliminate false sharing. The padding ensures cache line boundaries separate the data.
Checklist
- ✓ Understand cache coherence
- ✓ Know what false sharing is
- ✓ Understand why padding helps
- ✓ Ready to measure false sharing effects