Expert · Module 14 · Lesson 1 of 4
Caches and False Sharing: Layout for Speed
Keep hot data on its own cache line, split counters per thread, and prove the speedup with the honest benchmark harness.
Pro — members onlyAbout 11 minutes
By the end you can
- Explain cache lines and why two counters can slow each other down
- Use alignas and padding to separate hot fields
- Fix a false-sharing slowdown with per-thread sharding
01 · A question first
Eight threads bump eight counters two hundred million times. The counters are independent, no lock, no atomic contention on paper. Yet the loop takes six times longer than one thread doing all the work. What are the threads fighting over if they never touch each other's data?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Start with the problem
- How the idea works
- Why it is correct
- What it costs
- A sharded counter with separated elements
- Pitfalls and variations
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem