Learn

Cache Locality: Why Access Order Matters

Cache locality describes how a program reuses recently accessed information and nearby addresses. It helps explain performance differences that operation counts alone cannot predict.

Reusing what is already nearby

A processor does not treat every data access as an independent trip to a single uniform memory. Caches keep selected information closer to execution units. Temporal locality means using the same information again soon. Spatial locality means using information at nearby addresses. Intel explains these concepts and the role of cache lines in its loop-optimization guidance. Locality and loop blocking.

Locality is a property of the access pattern. The same mathematical calculation can visit its inputs in several valid orders. Those orders may place different demands on the memory hierarchy even when the arithmetic result and count are identical.

A small address example

Imagine a hypothetical 4-by-4 table of 32-bit values laid out row by row. Each value occupies four bytes. The first row begins at offsets 0, 4, 8, and 12; the second begins at 16. Reading the first row follows neighboring positions. Reading the first column visits offsets 0, 16, 32, and 48.

Both traversals read four numbers. The distinction is the distance between successive addresses, called the stride. NumPy documents strides as byte steps along array dimensions, which is useful when inspecting actual array layouts rather than assuming them. Array strides.

These tiny dimensions illustrate the address arithmetic, not a measurable performance difference. A real machine may fetch the entire example into a small part of its cache. To study a meaningful effect, an experiment needs a larger declared workload and hardware measurements.

The working set is part of the story

Suppose a calculation repeatedly combines a small lookup table with a stream of records. The table might be reused frequently while each record is used once. Keeping the table’s useful entries available creates temporal reuse. If the records themselves are arranged compactly, reading them sequentially creates spatial reuse.

Now imagine replacing the compact stream with pointers to separately allocated records. The mathematical task is unchanged, but the addresses may be scattered. Each record may bring in surrounding bytes that the calculation never uses. This is one reason representation and traversal should be examined together.

The word “small” is relative to the relevant cache and competing activity. A data structure that fits one level on one system may not fit another. Other threads, instructions, and nearby arrays may also need the same capacity. Total data size alone does not describe every cache conflict.

Blocking a larger calculation

For a large matrix operation, a possible strategy is to work on one smaller rectangular region before moving to another. This is called blocking or tiling. The engineering question is whether values can be reused while they are still available, without increasing overhead so much that the change loses its benefit.

There is no single block size that is best for every processor, array shape, and operation. A block choice can affect vectorization, loop overhead, alignment, and parallel scheduling as well as cache reuse. The correct output must remain unchanged, including any numerical rules that matter to the application.

What to measure

Compare declared layouts and access orders on identical inputs. Include layout-conversion cost when it belongs to the real workload. Record elapsed time, relevant hardware-counter observations if available, and the memory footprint. A lower miss count is useful evidence about one mechanism; it is not a complete explanation of application speed.

Locality matters because it provides a concrete optimization question: can the program use a smaller set of bytes more effectively? Follow sparse data to see another representation choice, and research methods for the evidence a fair comparison needs.