/memory-hierarchy-and-caches
Memory hierarchy skill for caches, coherence, and locality. Use when explaining cache levels, associativity, false sharing, prefetching, or MESI coherence. Activates on queries about cache hierarchy, L1 L2 L3, false sharing, cache line, prefetch, or cache coherence.
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/memory-hierarchy-and-caches
Context preview
The summary Claude sees to decide when to auto-load this skill.
Memory hierarchy skill for caches, coherence, and locality. Use when explaining cache levels, associativity, false sharing, prefetching, or MESI coherence. Activates on queries about cache hierarchy, L1 L2 L3, false sharing, cache line, prefetch, or cache coherence.
SKILL.md
memory-hierarchy-and-caches.SKILL.mdname: memory-hierarchy-and-caches
description: Memory hierarchy skill for caches, coherence, and locality. Use when explaining cache levels, associativity, false sharing, prefetching, or MESI coherence. Activates on queries about cache hierarchy, L1 L2 L3, false sharing, cache line, prefetch, or cache coherence.
Memory Hierarchy and Caches
Purpose
Teach CPU memory hierarchy: cache levels, associativity, line size, coherence protocols, false sharing, and prefetch behavior — architectural depth complementing optimization practice in `skills/low-level-programming/cpu-cache-opt`.
When to Use
- Explaining why padding fixes scalability
- Choosing struct layout for multicore
- Interpreting cache miss counters
- Understanding DMA vs CPU cache on embedded SoCs
Workflow
1. Hierarchy (typical desktop/ server)
Registers
├── L1d / L1i (per core, ~32 KiB, ~4 cycles)
├── L2 (per core, ~256 KiB – 1 MiB)
├── L3 (shared last-level, MiB – tens of MiB)
├── DRAM (hundreds of cycles)
└── Storage / NUMA remote (much slower)
Embedded MCUs may have only tightly-coupled memory (no L2/L3).
2. Cache line and associativity
- Line size: commonly **64 bytes** on x86/ARM64 (verify with `getconf LEVEL1_DCACHE_LINESIZE`)
- Set-associative: line maps to one set, competes within ways
- Conflict misses: many aliases same set
3. False sharing
/* Bad — two atomics on same cache line */
struct {
atomic_int counter_a;
atomic_int counter_b;
} stats;
/* Good — pad to cache line */
struct alignas(64) {
atomic_int counter_a;
char pad[64 - sizeof(atomic_int)];
atomic_int counter_b;
} stats;4. Coherence (multicore)
MESI states: Modified, Exclusive, Shared, Invalid. Writes invalidate other cores' copies of the line — why atomics and locks ping cache lines.
5. Prefetching
#ifdef __builtin_prefetch
for (int i = 0; i < n; i++) {
__builtin_prefetch(&data[i + 8], 0, 3);
process(data[i]);
}
#endifHardware stride prefetchers detect sequential access; random access misses.
6. Measurement
perf stat -e cache-references,cache-misses,L1-dcache-load-misses ./app
7. Agent usage
/memory-hierarchy-and-caches Diagnose false sharing in this per-thread stats array
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | Scaling collapses | False sharing | Line-align per-thread data | | High LLC misses | Working set > cache | Block algorithms; NUMA pin | | DMA incoherence | CPU cache vs device | Flush/invalidate on MCU; dma_sync on Linux | | Prefetch hurt | irregular access | Remove manual prefetch | | Huge struct copies | AoS cold lines | SoA layout — see cpu-cache-opt |
Related Skills
- `skills/low-level-programming/cpu-cache-opt` — practical optimization
- `skills/computer-architecture/cpu-pipelines-and-hazards` — load-use stalls
- `skills/computer-architecture/virtual-memory-paging-and-tlb` — TLB misses
- `skills/allocators/numa-programming` — remote memory latency
- `skills/profilers/hardware-counters` — perf counter events
Read more
name: memory-hierarchy-and-caches description: Memory hierarchy skill for caches, coherence, and locality. Use when explaining cache levels, associativity, false sharing, prefetching, or MESI coherence. Activates on queries about cache hierarchy, L1 L2 L3, false sharing, cache line, prefetch, or cache coherence.
Memory Hierarchy and Caches
Purpose
Teach CPU memory hierarchy: cache levels, associativity, line size, coherence protocols, false sharing, and prefetch behavior — architectural depth complementing optimization practice in `skills/low-level-programming/cpu-cache-opt`.
When to Use
- Explaining why padding fixes scalability
- Choosing struct layout for multicore
- Interpreting cache miss counters
- Understanding DMA vs CPU cache on embedded SoCs
Workflow
1. Hierarchy (typical desktop/ server)
Registers ├── L1d / L1i (per core, ~32 KiB, ~4 cycles) ├── L2 (per core, ~256 KiB – 1 MiB) ├── L3 (shared last-level, MiB – tens of MiB) ├── DRAM (hundreds of cycles) └── Storage / NUMA remote (much slower)
Embedded MCUs may have only tightly-coupled memory (no L2/L3).
2. Cache line and associativity
- Line size: commonly **64 bytes** on x86/ARM64 (verify with `getconf LEVEL1_DCACHE_LINESIZE`)
- Set-associative: line maps to one set, competes within ways
- Conflict misses: many aliases same set
3. False sharing
/* Bad — two atomics on same cache line */
struct {
atomic_int counter_a;
atomic_int counter_b;
} stats;
/* Good — pad to cache line */
struct alignas(64) {
atomic_int counter_a;
char pad[64 - sizeof(atomic_int)];
atomic_int counter_b;
} stats;4. Coherence (multicore)
MESI states: Modified, Exclusive, Shared, Invalid. Writes invalidate other cores' copies of the line — why atomics and locks ping cache lines.
5. Prefetching
#ifdef __builtin_prefetch
for (int i = 0; i < n; i++) {
__builtin_prefetch(&data[i + 8], 0, 3);
process(data[i]);
}
#endifHardware stride prefetchers detect sequential access; random access misses.
6. Measurement
perf stat -e cache-references,cache-misses,L1-dcache-load-misses ./app
7. Agent usage
/memory-hierarchy-and-caches Diagnose false sharing in this per-thread stats array
Common Problems
| Symptom | Cause | Fix | |---------|-------|-----| | Scaling collapses | False sharing | Line-align per-thread data | | High LLC misses | Working set > cache | Block algorithms; NUMA pin | | DMA incoherence | CPU cache vs device | Flush/invalidate on MCU; dma_sync on Linux | | Prefetch hurt | irregular access | Remove manual prefetch | | Huge struct copies | AoS cold lines | SoA layout — see cpu-cache-opt |
Related Skills
- `skills/low-level-programming/cpu-cache-opt` — practical optimization
- `skills/computer-architecture/cpu-pipelines-and-hazards` — load-use stalls
- `skills/computer-architecture/virtual-memory-paging-and-tlb` — TLB misses
- `skills/allocators/numa-programming` — remote memory latency
- `skills/profilers/hardware-counters` — perf counter events
A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.
Repo: mohitmishra786/low-level-dev-skills
Other skills on low-level-dev-skills.
- /custom-allocators
Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator,
Open skill - /numa-programming
NUMA programming skill for multi-socket memory locality. Use when detecting NUMA topology, binding processes with numactl, using libnuma API, building NUMA-aware data structures, or measuring remote access penalties. Activates on queries about numactl, libnuma, NUMA topology,
Open skill - /af-xdp
AF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
Open skill - /dpdk
DPDK skill for userspace packet I/O. Use when initializing EAL, configuring PMD drivers, using mbuf pools and rte_ring, setting up huge pages, RSS, or testpmd validation. Activates on queries about DPDK, EAL, rte_eth_rx_burst, hugepages, PMD, or testpmd.
Open skill - /io-uring
io_uring skill for Linux async I/O. Use when building high-performance servers with liburing, multi-shot operations, provided buffers, fixed files, zero-copy send, or tokio-uring. Activates on queries about io_uring, SQE/CQE, liburing, IORING_OP_PROVIDE_BUFFERS, or io_uring vs
Open skill - /adc-dac-baremetal
Bare-metal ADC and DAC skill. Use when configuring analog sampling, DMA-driven ADC, calibration, or DAC output on MCUs. Activates on queries about ADC bare-metal, sampling time, DMA ADC, or DAC channel setup.
Open skill

