| name | cpu-cache-opt |
| description | CPU cache optimization skill for C/C++ and Rust. Use when diagnosing cache misses, improving data layout for cache efficiency, using perf stat cache counters, understanding false sharing, prefetching, or structuring AoS vs SoA data layouts. Activates on queries about cache misses, cache lines, false sharing, perf cache counters, data layout optimization, prefetch, AoS vs SoA, or L1/L2/L3 cache performance. |
CPU Cache Optimization
Purpose
Guide agents through cache-aware programming: diagnosing cache misses with perf, data layout transformations (AoS→SoA), false sharing detection and fixes, prefetching, and cache-friendly algorithm design.
Triggers
- "My program has high cache miss rates — how do I fix it?"
- "What is false sharing and how do I detect it?"
- "Should I use AoS or SoA data layout?"
- "How do I measure cache performance with perf?"
- "How do I use __builtin_prefetch?"
- "My multithreaded program is slower than single-threaded due to cache"
Workflow
1. Measure cache performance
perf stat -e cache-references,cache-misses,cycles,instructions ./prog
perf stat -e \
L1-dcache-load-misses,\
L1-dcache-loads,\
L2-dcache-load-misses,\
LLC-load-misses,\
LLC-loads \
./prog
perf stat -e \
machine_clears.memory_ordering,\
mem_load_l3_hit_retired.xsnp_hitm \
./prog
2. Cache line basics
- Cache line size: 64 bytes on x86-64, ARM (most platforms)
- L1 cache: 32–64 KB, ~4 cycles latency
- L2 cache: 256 KB–1 MB, ~12 cycles latency
- L3 cache: 6–64 MB, ~40 cycles latency
- Main memory: ~200–300 cycles latency
long cache_line = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
struct alignas(64) HotData {
int counter;
};
typedef struct {
int x;
} __attribute__((aligned(64))) AlignedData;
3. AoS vs SoA data layout
struct Particle {
float x, y, z;
float vx, vy, vz;
float mass;
int flags;
};
Particle particles[N];
struct ParticlesSoA {
float *x, *y, *z;
float *vx, *vy, *vz;
float *mass;
int *flags;
};
4. Common cache-unfriendly patterns
Node *node = head;
while (node) {
process(node->data);
node = node->next;
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
sum += matrix[j][i];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
sum += matrix[i][j];
struct Record {
int id;
char name[128];
int value;
char desc[256];
};
struct RecordHot { int id; int value; };
struct RecordCold { char name[128]; char desc[256]; };
RecordHot hot_data[N];
RecordCold cold_data[N];
5. False sharing
False sharing occurs when two threads write to different variables that share a cache line, causing constant cache-line invalidations.
int counter_a;
int counter_b;
struct alignas(64) PaddedCounter {
int value;
char padding[60];
};
PaddedCounter counters[NUM_THREADS];
struct alignas(std::hardware_destructive_interference_size) PaddedCounter {
int value;
};
6. Prefetching
Manual prefetch hints to hide memory latency:
#include <immintrin.h>
__builtin_prefetch(ptr, 0, 3);
__builtin_prefetch(ptr, 1, 3);
_mm_prefetch((char*)ptr, _MM_HINT_T0);
_mm_prefetch((char*)ptr, _MM_HINT_T1);
_mm_prefetch((char*)ptr, _MM_HINT_T2);
_mm_prefetch((char*)ptr, _MM_HINT_NTA);
#define PREFETCH_DIST 8
for (int i = 0; i < N; i++) {
if (i + PREFETCH_DIST < N)
__builtin_prefetch(&data[i + PREFETCH_DIST], 0, 3);
process(data[i]);
}
Prefetching rules:
- Prefetch too early = cache evicted before use
- Prefetch too late = no benefit
- Prefetch distance = memory latency / time per iteration (typically 8–32 elements)
7. Cache-friendly algorithm design
#define BLOCK 64
void matrix_mult_blocked(float *C, float *A, float *B, int N) {
for (int i = 0; i < N; i += BLOCK)
for (int k = 0; k < N; k += BLOCK)
for (int j = 0; j < N; j += BLOCK)
for (int ii = i; ii < i + BLOCK && ii < N; ii++)
for (int kk = k; kk < k + BLOCK && kk < N; kk++)
for (int jj = j; jj < j + BLOCK && jj < N; jj++)
C[ii*N+jj] += A[ii*N+kk] * B[kk*N+jj];
}
For perf cache event reference and false sharing detection patterns, see references/cache-counters.md.
Related skills
- Use
skills/profilers/linux-perf for perf stat and perf record cache measurements
- Use
skills/profilers/valgrind — cachegrind simulates cache behaviour
- Use
skills/low-level-programming/simd-intrinsics — SoA layout pairs with SIMD vectorization
- Use
skills/low-level-programming/memory-model for false sharing in concurrent contexts