Arrays vs Linked Lists in terms of locality Arrays vs Linked Lists in terms of locality arrays arrays

Arrays vs Linked Lists in terms of locality


Your understanding of the the array case is mostly correct. If an array is accessed sequentially, many processors will not only fetch the block containing the element, but will also prefetch subsequent blocks to minimize cycles spent waiting on cache misses. If you are using an Intel x86 processor, you can find details about this in the Intel x86 optimization manual. Also, if the array elements are small enough, loading a block containing an element means the next element is likely in the same block.

Unfortunately, for linked lists the pattern of loads is unpredictable from the processor's point of view. It doesn't know that when loading an element at address X that the next address is the contents of (X + 8).

As a concrete example, the sequence of load addresses for a sequential array access is nice and predictable.For example, 1000, 1016, 1032, 1064, etc.

For a linked list it will look like:1000, 3048, 5040, 7888, etc. Very hard to predict the next address.