CPU Pipeline Stalls: The Hidden Bottleneck
When your Java code runs slower than expected, it is often because of "Stalls." The CPU pipeline flushes because it couldn't fetch data from the cache in time.
1. Cache Lines (64 Bytes)
The CPU loads 64 bytes at a time. If your object references are scattered across memory (pointer chasing), you incur a 100ns hit to RAM for every dereference.
2. False Sharing
When two threads update unrelated variables that happen to share the same 64-byte cache line, the CPU cores fight for ownership.
- The Fix: Use to pad your objects so they sit on their own cache lines.
3. Optimization
Design data structures that are cache-friendly: use primitive arrays instead of object-lists, and avoid unnecessary branching logic that causes pipeline flushes.
