IT Essentials

Why 100% CPU Doesn't Mean What You Think: Memory Latency and Cache-Friendly Design

A 100% CPU reading can mean the core is stalled waiting on memory. This explainer covers the cache hierarchy and cache-friendly algorithms such as radix partitioning, using a vendor benchmark as the starting point.

100% CPU is not the same as busy

On Linux, a CPU is counted as busy whenever a task wants time on it — including the time a core spends frozen waiting for memory. So a core reporting 100% may be doing far less work than another core reporting the same number. That everyday misreading is where Pivot Lake’s post “It’s the memory latency, stupid” (pivotlake.io) begins.

The gap is the problem

A modern CPU retires roughly 4–8 instructions per nanosecond. A trip to DRAM costs 50–100ns. In other words, several hundred instructions could have been done in the time one memory access takes. The cache hierarchy bridges some of that gap: L1 at about 1ns and tens of KB, L2 at 3–4ns and around a megabyte, L3 at 10–15ns stretching to hundreds of MB on high-end parts. Caches are still small, which is why finishing work while data sits in cache is a central concern of performance engineering.

A worked example: GROUP BY with a big hash table

Take a single-core query that counts rows per UserID over the 99-million-row ClickBench dataset. With roughly 17 million distinct keys, the hash table cannot live in cache, so nearly every probe falls through to DRAM. The author’s perf counters show 50.1% of CPU slots stalled on memory, only 16.1% retiring work, and 4.5GB pulled from DRAM.

The fix is radix partitioning: hash each key, use the top 12 bits to scatter rows into 4,096 partitions, then aggregate one partition at a time into a small hash table. The total instruction count goes up (roughly 11.4 billion to 19.3 billion), but each working set now fits in cache. Memory stalls dropped to 23.5%, DRAM traffic to 1.5GB, and runtime from 3.6s to 2.7s. As the author puts it, trading cold DRAM accesses for CPU instructions is “nearly always a good trade.” Note that these are vendor measurements of the company’s own engine; prefetching and throughput-oriented techniques are mentioned but deferred to a follow-up post.

What to keep

First, profile for what is blocking the core, not just for utilization. Stall and retirement counters separate waiting from doing in a way that a single percentage cannot. Second, many real speedups come from reducing memory access rather than reducing arithmetic. Third, get in the habit of sizing working sets — hash tables, indexes, buffers — against cache capacity. The latency gap has not closed as hardware has improved, so the principle outlives any particular product or CPU generation.

Sources

Drafted by AI from the original sources and reviewed by an editor before publishing. Check the sources for the authoritative details.

#memory latency #CPU cache #performance #database #profiling

← IT essentials